programmers_마법의 엘리베이터_java
2023. 5. 14. 17:57ㆍAlgorithm/Programmers
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/148653#
import java.util.*;
class Solution {
public int solution(int storey) {
int answer = 0;
answer = bfs(storey);
return answer;
}
public int bfs(int storey){
int min = Integer.MAX_VALUE;
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{storey,0}); // num, ans
while (!queue.isEmpty()){
int size = queue.size();
for (int i = 0; i<size; i++){
int[] now = queue.poll();
if(now[0] == 0){
min = Math.min(min, now[1]);
continue;
}
int num = now[0]%10;
if (num > 5){
queue.offer(new int[]{(now[0]+10)/10, now[1] + (10 - num)});
}else if(num == 5){
queue.offer(new int[]{(now[0]+10)/10, now[1] + (10 - num)});
queue.offer(new int[]{now[0]/10, now[1] + num});
}else if(num < 5){
queue.offer(new int[]{now[0]/10, now[1] + num});
}
}
}
return min;
}
}
'Algorithm > Programmers' 카테고리의 다른 글
programmers_하노이의 탑_java (0) | 2023.05.15 |
---|---|
programmers_문자열 압축_java (0) | 2023.05.15 |
programmers_호텔 대실_java (0) | 2023.05.12 |
programmers_멀쩡한 사각형_java (0) | 2023.05.12 |
programmers_점 찍기_java (0) | 2023.05.11 |