이건 찾아봐도 획기적인 풀이법은 없는 것 같아 반 노가다로 푼 문제 ..
Queue나 Stack을 이용하지 않고 ArrayList만으로도 사실 풀 수 있다.
package OneDayOneCodingChallenge;
import static org.junit.Assert.assertArrayEquals;
import java.util.LinkedList;
import java.util.Queue;
import org.junit.Test;
/**
* @author seoyeon on 2020/09/08
* @project AlgorithmTest
*/
public class Day5 {
public int[] solution(int[] progresses, int[] speeds) {
int[] temp = {};
Queue<Integer> queue = new LinkedList<>();
Queue<Integer> answerTemp = new LinkedList<>();
for (int i = 0; i < progresses.length; i++) {
queue.add((int) Math.ceil((double) (100 - progresses[i]) / speeds[i]));
}
int standard = queue.poll();
int cnt = 1;
while (!queue.isEmpty()) {
if (standard >= queue.peek()) {
queue.poll();
cnt++;
if (queue.isEmpty()) {
answerTemp.add(cnt);
}
} else {
answerTemp.add(cnt);
standard = queue.poll();
cnt = 1;
if (queue.isEmpty()) {
answerTemp.add(1);
}
}
}
int[] answer = new int[answerTemp.size()];
for (int i = 0; i < answer.length; i++) {
answer[i] = answerTemp.poll();
}
return answer;
}
@Test
public void 정답() {
// assertArrayEquals(new int[]{2, 1}, solution(new int[]{93, 30, 55}, new int[]{1, 30, 5}));
assertArrayEquals(new int[]{1, 3, 2},
solution(new int[]{95, 90, 99, 99, 80, 99}, new int[]{1, 1, 1, 1, 1, 1}));
}
}
'Algorithm' 카테고리의 다른 글
[프로그래머스]위장 (0) | 2020.09.14 |
---|---|
[프로그래머스]문자열 내 마음대로 정렬하기 (0) | 2020.09.11 |
[프로그래머스]다리 위를 지나는 트럭 (0) | 2020.09.10 |
[프로그래머스]라면공장 문제풀이(우선순위큐) (0) | 2020.09.07 |
[프로그래머스]네트워크 문제풀이(DFS와 BFS) (0) | 2020.09.05 |