[프로그래머스] N개의 최소공배수

2020. 4. 12. 16:56프로그래머스/LEVEL 2

import java.util.*;

class Solution {
  public int solution(int[] arr) {
      int answer = 1;
      
      Arrays.sort(arr);
      
      List<Integer> value = new ArrayList<>();
      int max = arr[arr.length-1];
      
      for(int i=2; i<=max; i++){
          boolean flag = false;
              for(int j=0; j<arr.length; j++){
                  if(arr[j]%i == 0){
                      arr[j] = arr[j]/i;
                      flag = true;
                  }
              }
          if(flag ==true){
              value.add(i);
              i--;
          }
      }
      
      for(int i=0; i<value.size(); i++){
          answer *= value.get(i);
      }
      return answer;
  }
}