Computer Sciences/Problem Solve
[Programmers] N개의 최소공배수
jeidiiy
2023. 8. 8. 14:27
https://school.programmers.co.kr/learn/courses/30/lessons/12953
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 설명
간단한 최소공배수를 구하는 문제이다. for 문을 돌면서 최소공배수를 갱신해나가면 된다.
코드
class Solution {
public int solution(int[] arr) {
int result = arr[0];
for (int i = 1; i < arr.length; i++) {
result = lcm(result, arr[i]);
}
return result;
}
public int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
}