알고리즘

프로그래머스 N개의 최소공배수 (JAVA)

박카스마시며코딩 2023. 1. 11. 14:18

https://school.programmers.co.kr/learn/courses/30/lessons/12953

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

import java.util.*;

class Solution {
    public int solution(int[] arr) {
        int answer = 1;
        int size = arr.length;
        int max = 0;
        for(int i = 0 ; i < size ; i++){
            max = Math.max(max,arr[i]);
        }
        for(int i = 2 ; i <= max; i++){
            int maxCnt = 0;
            for(int j = 0 ; j < size ; j++){
                int cnt = 0;
                while(arr[j] % i == 0){
                    cnt++;
                    arr[j] /= i;
                }
                maxCnt = Math.max(maxCnt,cnt);
            }
            for(int j = 0 ; j < maxCnt ; j++){
                answer *= i;
            }
        }
        System.out.println(Arrays.toString(arr));
        return answer;
    }
}