https://school.programmers.co.kr/learn/courses/30/lessons/42586
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
import java.util.*;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
List<Integer> ans=new ArrayList<>();
Queue<Integer> qu=new ArrayDeque<>();
for(int i=0;i<progresses.length;i++){
qu.offer((int)Math.ceil((100-progresses[i])/(double)speeds[i]));
}
int temp = 0, count = 0;
while(!qu.isEmpty()) {
temp = qu.poll();
count = 1;
while(!qu.isEmpty() && (temp >= qu.peek()) ) {
qu.poll();
count++;
}
ans.add(count);
}
int[] answer = new int[ans.size()];
for(int i=0;i<ans.size();i++){
answer[i]=ans.get(i);
}
return answer;
}
}
풀이
List<Integer> ans=new ArrayList<>();
Queue<Integer> qu=new ArrayDeque<>();
for(int i=0;i<progresses.length;i++){
qu.offer((int)Math.ceil((100-progresses[i])/(double)speeds[i]));
}
우선 큐를 하나 선언하고, i번 기능이 모두 완료될 때 까지 걸리는 일수를 계산하여 넣는다.
int temp = 0, count = 0;
while(!qu.isEmpty()) {
temp = qu.poll();
count = 1;
while(!qu.isEmpty() && (temp >= qu.peek()) ) {
qu.poll();
count++;
}
ans.add(count);
}
이후 한 번에 배포할 수 있는 기능의 개수를 계산한다. 이전 기능을 완성하는 데 걸리는 시간보다 다음 기능을 완성하는 데 걸리는 시간이 같거나 작아야 한 번에 배포가 가능하므로 첫 기능을 임시 저장하고, 큐의 front와 첫 기능 완성 시간을 비교하면서 더 작다면 count를 추가하여 해당 기능을 한 번에 배포하도록 한다.
다음 기능이 처음 기능보다 완성에 오래 걸린다면 그 기능은 다음 배포로 미루고, 지금까지의 기능의 개수를 정답 리스트에 추가한다.
모두 계산한 후 이를 반환하면 정답을 구할 수 있다.
'Coding Test' 카테고리의 다른 글
Leetcode 1277 Count Square Submatrices with All Ones (Java) (1) | 2024.11.01 |
---|---|
LeetCode 2684 Maximum Number of Moves in a Grid (Java) (0) | 2024.10.31 |
Leetcode 921 Minimum Add to Make Parentheses Valid (Java) (1) | 2024.10.09 |
백준 6497 전력난 (Java) (0) | 2024.10.06 |
백준 2229 조 짜기 (C++) (0) | 2024.10.04 |