본문 바로가기

Algorithm

[프로그래머스]기능개발

이건 찾아봐도 획기적인 풀이법은 없는 것 같아 반 노가다로 푼 문제 ..

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}));
  }

}