프로그래머스 - JAVA/Level 2
[JAVA] 프로그래머스 Lv.2 : N개의 최소공배수
새우버거♬
2021. 6. 8. 11:32
예제:
arr | result |
[2, 6, 8, 14] | 168 |
[1, 2, 3] | 6 |
Solution #1
class Solution {
public int solution(int[] arr) {
int answer = arr[0];
for (int i = 1; i < arr.length; i++) {
int gcd = gcd(answer, arr[i]);
answer = answer * arr[i] / gcd;
}
return answer;
}
private int gcd(int a, int b) {
while (b != 0) {
int r = a % b;
a = b;
b = r;
}
return a;
}
}
Result #1
More Algorithm!
👇👇
github.com/ggujangi/ggu.programmers
ggujangi/ggu.programmers
프로그래머스 알고리즘, JAVA. Contribute to ggujangi/ggu.programmers development by creating an account on GitHub.
github.com
출처 : 프로그래머스