[프로그래머스/큐] Level 2 기능개발 (JAVA)

Jiwoo Kim·2021년 2월 4일
0

알고리즘 정복하기

목록 보기
17/85
post-thumbnail

문제


풀이 (2021.02.04)

  1. Function 객체를 생성해서 Queue에 차례로 넣는다.
  2. Queue의 헤드가 완료된 기능이면 count를 증가시킨다. 완료되지 않은 기능이 나올 때까지 해당 daycount를 체크한다.
  3. Queue의 헤드가 완료되지 않은 기능이면 그 어떤 기능도 완료될 수 없기 때문에 day만 증가시킨다.
  4. 결과 ArrayList를 Array로 변환시켜 리턴한다.

코드

import java.util.*;

class Solution {
    
    public int[] solution(int[] progresses, int[] speeds) {
        Queue<Function> functions = new LinkedList<>();
        for (int i = 0; i < progresses.length; i++) functions.offer(new Function(progresses[i], speeds[i]));

        int day = 0, count = 0;
        List<Integer> answer = new ArrayList<>();
        while (!functions.isEmpty()) {
            while (functions.peek().progress + day * functions.peek().speed >= 100) {
                functions.poll();
                count++;
                if (functions.isEmpty()) break;
            }
            if (count != 0) {
                answer.add(count);
                count = 0;
            }
            day++;
        }
        return answer.stream().mapToInt(Integer::intValue).toArray();
    }
}

class Function {
    int progress;
    int speed;

    public Function(int progress, int speed) {
        this.progress = progress;
        this.speed = speed;
    }
}

풀이 (2021.08.24)

코드

import java.util.*;

class Solution {
    
    private static final int DONE = 100;
    
    private int[] progresses;
    private int[] speeds;
    private int total;
    
    public int[] solution(int[] progresses, int[] speeds) {
        this.progresses = progresses;
        this.speeds = speeds;
        this.total = progresses.length;
        
        List<Integer> answer = new ArrayList<>();
        
        int work = 0;
        while (work < total) {
            int day = workDay(work);
            int finished = 1;
            
            work++;
            
            while (work < total) {
                if (workDay(work) > day) {
                    break;
                }
                
                finished++;
                work++;
            }
            
            answer.add(finished);
       }
        
        return answer.stream().mapToInt(Integer::valueOf).toArray();
    }
    
    private int workDay(int target) {
        int remain = DONE - progresses[target];
        int speed = speeds[target];
        
        if (remain % speed == 0) {
            return remain / speed;
        }
        return remain / speed + 1;
    }
}

0개의 댓글