https://school.programmers.co.kr/learn/courses/30/lessons/42586
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
알고리즘 고득점 키트 풀고있는데
어떤 자료구조를 사용해야하는지 나와있으니까 자꾸 생각을 자유롭게 못해서 더 어려운 길을 가는 것 같다..
progresses는 현재 진행률을 의미하고, speeds는 하루에 진행 가능한 양을 의미한다.
뒷 순서에 있는 작업이 끝났더라도 앞 순서의 작업이 끝나지 않았으면 배포할 수 없다.
이 문제는 각 배포마다 몇개의 작업이 배포되는지를 배열로 리턴하는 문제이다.
나는 progresses와 speeds를 사용하여 각 작업마다 필요한 일수를 계산하여 이를 담는 배열을 따로 만들었다.
그 다음 이를 큐에 넣고, 차례로 빼냈다. 만약 큐를 peek 했는데 지금 나온 수보다 작으면 해당 요소도 같이 배포하는 것이다.(뒷 작업인데 이미 끝난 작업이므로)
import java.io.*;
import java.util.*;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
int[] day = new int[progresses.length];
Queue<Integer> q = new LinkedList<>();
for(int i = 0;i<progresses.length;i++){
int a = (100-progresses[i])/speeds[i];
int b = (100-progresses[i])%speeds[i];
if(b != 0) a++;
q.add(a);
day[i] = a;
}
LinkedList<Integer> answ = new LinkedList<>();
while(!q.isEmpty()){
int num = 1;
int k = q.poll();
while(true){
if(q.isEmpty() || k < q.peek()) break;
q.poll();
num++;
}
answ.add(num);
}
int[] ans = new int[answ.size()];
for(int i = 0;i<answ.size();i++){
ans[i] = answ.get(i);
}
return ans;
}
}
'알고리즘 > 프로그래머스 문제풀이' 카테고리의 다른 글
[프로그래머스 JAVA] 완주하지 못한 선수 (1) | 2025.05.15 |
---|