
-Code
import java.util.*;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
List<Integer> answer = new ArrayList<>();
// 걸리는 시간 덱에 저장
Deque<Integer> dq = new ArrayDeque<>();
for (int i = 0; i < progresses.length; i++) {
double time = (double) (100 - progresses[i]) / speeds[i];
// 올림으로 처리
int ceil = (int) Math.ceil(time);
dq.addLast(ceil);
}
// 현재 걸리는 시간 poll
int now = dq.pollFirst();
int cnt = 1;
// dq가 비기 전까지 반복
while (!dq.isEmpty()) {
// 현재 시간보다 적으면 cnt++과 poll
if (now >= dq.peekFirst()) {
cnt++;
dq.pollFirst();
} else {
// 현재 시간보다 크면 now 변경 및 cnt 저장 및 초기화
answer.add(cnt);
now = dq.pollFirst();
cnt = 1;
}
}
// 마지막 cnt는 저장 안되므로 저장
answer.add(cnt);
return answer.stream().mapToInt(Integer::intValue).toArray();
}
}
처음 덱에 저장 시 Math.round로 해서 틀리고 그 이후에는 조건문의 오류가 있었습니다. 해당 부분을 수정하여 해결했습니다.
'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
| [백준/Java] 24444번 알고리즘 수업 - 너비 우선 탐색 1 (0) | 2026.01.02 |
|---|---|
| [백준/Java] 24480번 알고리즘 수업 - 깊이 우선 탐색 2 (0) | 2026.01.02 |
| [프로그래머스/Java] 할인 행사 (0) | 2026.01.02 |
| [프로그래머스/Java] 연속 부분 수열 합의 개수 (0) | 2026.01.02 |
| [프로그래머스/Java] 점프와 순간 이동 (0) | 2026.01.02 |