문제 설명
https://school.programmers.co.kr/learn/courses/30/lessons/120878

제한 사항

입출력 예

풀이
import java.util.ArrayList;
class Solution {
public int solution(int a, int b) {
ArrayList<Integer> resultA = new ArrayList<>();
ArrayList<Integer> resultB = new ArrayList<>();
int i = 2;
while (a != 1) {
if (a % i == 0) {
resultA.add(i);
a /= i;
}
else i++;
}
i = 2;
while (b != 1) {
if (b % i == 0) {
resultB.add(i);
b /= i;
}
else i++;
}
for (Integer num : resultA) {
if (resultB.contains(num)) resultB.remove(num);
}
while (resultB.contains(2)) {
resultB.remove((Integer)2);
}
while (resultB.contains(5)) {
resultB.remove((Integer)5);
}
return resultB.size() == 0 ? 1 : 2;
}
}
후기
문제 밑에 힌트가 있었는데 못 봤다... 나는 그냥 두 수를 소인수 분해하고 공통된 것 제거 후 분모에서 2와 5를 모두 제거하여 그 길이를 판별했다.
'코딩테스트 (프로그래머스) > Java' 카테고리의 다른 글
| [프로그래머스][JAVA][Lv. 2] 땅따먹기 (1) | 2023.10.27 |
|---|---|
| [프로그래머스][JAVA][Lv. 0] 대소문자 바꿔서 출력하기 (0) | 2023.10.25 |
| [프로그래머스][Java][Lv. 3] 네트워크 (0) | 2023.10.11 |
| [프로그래머스][JAVA][Lv. 0] 저주의 숫자 3 (0) | 2023.10.10 |
| [프로그래머스][JAVA][Lv. 2] 방문 길이 (0) | 2023.10.09 |