본문 바로가기

개발지/코테

[프로그래머스] 코테연습 - 기능개발 (lv.2)

- 내 방법 풀이

  • 처음엔 배열로 풀 수 있을 거 같아, 배열로 풀어볼라 했으나 큐문제이므로 큐를 이용하기로 마음먹었다.
  • 어제 풀은 다리를 지나는 트럭에서 생각을 전환해서 풀었다. 큐를 2개 생성하고, day를 설정해 while문을 돌렸다.
  • 작업큐와 속도큐에서 peek하여 작업이 완료되었다면 cnt를 증가시키며 진행률이 100%이하인 작업이 나올 때까지 poll한다. 해당일에 배포 가능한 작업(cnt > 0)이 있었다면 arrList에 추가.
  • 작업큐가 빌 때까지 while문 반복 후 종료. answer에 arrList값 추가하여 리턴
import java.util.*;

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        ArrayList<Integer> arrList = new ArrayList<>();

        int day = 0, cnt = 0;
        Queue<Integer> progressQueue = new LinkedList<>();
        Queue<Integer> speedQueue = new LinkedList<>();
        
        for (int i = 0; i < progresses.length; i++) {
            progressQueue.add(progresses[i]);
            speedQueue.add(speeds[i]);
        }
        
        while (!progressQueue.isEmpty()) {
            cnt = 0;
            while (true) {
                if (progressQueue.isEmpty() || progressQueue.peek() + (speedQueue.peek() * day) < 100) break;
                progressQueue.poll();
                speedQueue.poll();
                cnt++;
            }
            if (cnt > 0) arrList.add(cnt);
            day++;
        }
        
        int[] answer = new int[arrList.size()];
        for (int i = 0; i < answer.length; i++) {
            answer[i] = arrList.get(i);
        }
        
        return answer;
    }
}

- 다른 사람 방법 풀이

  • 배열로 푼 사람의 풀이를 찾아 보았다.
  • 완료에 걸리는 작업일 수를 기능마다 계산하여 저장한 배열을 생성한다.
  • 필요작업일수 배열과 비교하며 같은 날에 배포 가능 기능의 갯수를 계산하여 list에 추가.
  • 직관적으로 코드가 보이지 않는 것 같다.
import java.util.*;

class Solution {
        public int[] solution(int[] progresses, int[] speeds) {

        List<Integer> list = new ArrayList<>();
        int[] works = new int[progresses.length];
        
      
        for(int i = 0; i < speeds.length; i++){
            works[i]  = (100 - progresses[i]) / speeds[i];
            if ((100 - progresses[i]) % speeds[i] != 0){
                works[i]  += 1;
            }
        }
        
        int x = works[0];
        int count=1;
        for(int i=1;i<progresses.length;i++){

            if(x>=works[i]){
                count+=1;
            }else{
                list.add(count);
                count =1;
                x= works[i];
            }
        }
        list.add(count);
        
        int[] answer = new int[list.size()];

        for (int i = 0; i <list.size(); i++) {
            answer[i] = list.get(i);
        }

        return answer;
    }
}