문제
https://programmers.co.kr/learn/courses/30/lessons/42586
내 코드
class Program{
int progress;
int speed;
public Program(int progress, int speed) {
this.progress = progress;
this.speed = speed;
}
}
public List<Integer> solution(int[] progresses, int[] speeds) {
List<Integer> rlt = new ArrayList<>();
//2
Queue<Program> queue = new LinkedList<>();
for(int i=0; i<progresses.length; i++) {
queue.offer(new Program(progresses[i], speeds[i]));
}
//3
while(!queue.isEmpty()){
int count = 0;
//4
for(int i=0; i<queue.size(); i++) {
Program temp = queue.poll();
temp.progress += temp.speed;
queue.offer(temp);
}
//5
while(queue.size()>0) {
if(queue.peek().progress >= 100) {
queue.poll();
count++;
}else {
break;
}
}
//6
if(count >0)
rlt.add(count);
}
return rlt;
}
풀이
- 작업진도와 작업속도를 생성자 파라미터로 받는 Program class 정의
- 반복문을 돌면서 큐에 program 객체를 담는다.
- 큐가 비워질 때까지 반복문 수행
- 프로그램의 진도를 반복문을 돌면서 작업속도 만큼 올려준다.
- 작업이 완료된 프로그램은 큐에서 빼주고 count를 증가시킨다.
- 증가된 count 수 만큼 결과 list에 입력해준다.