https://school.programmers.co.kr/learn/courses/30/lessons/154538
문제
제한사항
코드
import java.util.*;
public class Solution {
public int solution(int x, int y, int n) {
int answer = 0;
HashSet<Integer> cur = new HashSet<>();
cur.add(x);
while(!cur.isEmpty()) {
if(cur.contains(y)) {
return answer;
}
HashSet<Integer> next = new HashSet<>();
for(int num : cur) {
if(num + n <= y) {
next.add(num+n);
}
if(num*2 <= y) {
next.add(num*2);
}
if(num*3 <= y) {
next.add(num*3);
}
}
cur = next;
answer++;
}
return -1;
}
}
풀이