<aside> πŸ›  λͺ©μ°¨

</aside>

1. λ¬Έμ œμ„€λͺ…

두 수의 μ΅œμ†Œκ³΅λ°°μˆ˜(Least Common Multiple)λž€ μž…λ ₯된 두 수의 배수 쀑 곡톡이 λ˜λŠ” κ°€μž₯ μž‘μ€ 숫자λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄ 2와 7의 μ΅œμ†Œκ³΅λ°°μˆ˜λŠ” 14κ°€ λ©λ‹ˆλ‹€. μ •μ˜λ₯Ό ν™•μž₯ν•΄μ„œ, n개의 수의 μ΅œμ†Œκ³΅λ°°μˆ˜λŠ” n 개의 μˆ˜λ“€μ˜ 배수 쀑 곡톡이 λ˜λŠ” κ°€μž₯ μž‘μ€ μˆ«μžκ°€ λ©λ‹ˆλ‹€. n개의 숫자λ₯Ό 담은 λ°°μ—΄ arr이 μž…λ ₯λ˜μ—ˆμ„ λ•Œ 이 μˆ˜λ“€μ˜ μ΅œμ†Œκ³΅λ°°μˆ˜λ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜, solution을 μ™„μ„±ν•΄ μ£Όμ„Έμš”.

μ œν•œμ‚¬ν•­

μž…μΆœλ ₯ 예

arr result
[2,6,8,14] 168
[1,2,3] 6

2. λ¬Έμ œλΆ„μ„(사전)

3. JAVAμ½”λ“œ

public class Solution {

    // ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ LEVEL2 N개의 μ΅œμ†Œκ³΅λ°°μˆ˜
    // 문제 URL : <https://programmers.co.kr/learn/courses/30/lessons/12953>

    public int solution(int[] arr) {
        // κ°€μž₯ μž‘μ€ 값인 1둜 μ΄ˆκΈ°ν™”
        int answer = 1;
        for (int i = 0; i < arr.length; i++) {
            answer = (answer * arr[i]) / this.gcd(answer, arr[i]);
        }
        return answer;
    }

    // μ΅œλŒ€κ³΅μ•½μˆ˜ κ΅¬ν•˜λŠ” λ©”μ„œλ“œ
    public int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }
}

4. μ½”λ“œν•΄μ„

<aside> <img src="https://raw.githubusercontent.com/eirikmadland/notion-icons/master/v5/icon3/ul-github.svg" alt="https://raw.githubusercontent.com/eirikmadland/notion-icons/master/v5/icon3/ul-github.svg" width="40px" /> μ°Έμ‘° Github Code

</aside>

// μ΅œλŒ€κ³΅μ•½μˆ˜ κ΅¬ν•˜λŠ” λ©”μ„œλ“œ
public int gcd(int a, int b) {
    if (b == 0) {
        return a;
    }
    return gcd(b, a % b);
}
public int gcd(int a, int b) {
    int x = Math.min(a, b);
    int y = Math.max(a, b);
    int temp = 0;
    while(x != 0) {
        temp = y;
        y = x;
        x = temp % x;
    }
    return y;
}