Key Idea
- 진도(progress) 와 개발속도(speed) 정보를 가진
Work class
생성- Work 객체들을 담는
Queue
를 만들어서 삽입- 반복문을 돌면서 Queue 의 모든 Work 의 progress 에 speed 를 더해준다
- Queue 의 맨 앞의 값을 꺼내지 않고 progress 값이 100 이상인지를 확인하여
- 100 이 넘는 다면 뒤에서 progress 값이 100이 넘는것들을 확인하여 count 해준다
- 위를 Queue 가 empty 가 아닐때 까지 반복
for (int i = 0; i < progresses.length; i++)
queue.add(new Work(progresses[i], speeds[i]));
while (!queue.isEmpty()) {
int count = 0;
develop(queue);
Work first = queue.peek();
if (first.progress >= 100){
queue.poll();
count++;
while (!queue.isEmpty()) {
if(queue.peek().progress < 100)
break;
queue.poll();
count++;
}
}
if(count > 0)
cntAry.add(count);
}
import java.util.*;
class Work {
int progress;
int speed;
public Work(int progress, int speed) {
this.progress = progress;
this.speed = speed;
}
}
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
Queue<Work> queue = new LinkedList<>();
ArrayList<Integer> cntAry = new ArrayList<>();
for (int i = 0; i < progresses.length; i++)
queue.add(new Work(progresses[i], speeds[i]));
while (!queue.isEmpty()) {
int count = 0;
develop(queue);
Work first = queue.peek();
if (first.progress >= 100){
queue.poll();
count++;
while (!queue.isEmpty()) {
if(queue.peek().progress < 100)
break;
queue.poll();
count++;
}
}
if(count > 0)
cntAry.add(count);
}
return changeToIntAry(cntAry);
}
private void develop(Queue<Work> queue) {
for (Work work : queue)
work.progress += work.speed;
}
private int[] changeToIntAry(ArrayList<Integer> cntAry) {
int[] answer = new int[cntAry.size()];
for (int i = 0; i < cntAry.size(); i++)
answer[i] = cntAry.get(i);
return answer;
}
}
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class SolutionTest {
Solution solution;
@BeforeEach
public void setSol(){
solution = new Solution();
}
@Test
public void solution_1(){
int[] result = solution.solution(new int[]{93, 30, 55}, new int[]{1, 30, 5});
assertArrayEquals(new int[]{2, 1}, result);
}
@Test
public void solution_2(){
int[] result = solution.solution(new int[]{95, 90, 99, 99, 80, 99}, new int[]{1, 1, 1, 1, 1, 1});
assertArrayEquals(new int[]{1, 3, 2}, result);
}
}