본문 바로가기

코딩테스트/프로그래머스

[프로그래머스 - Level2] 기능개발 / 2022.07.09

 

https://school.programmers.co.kr/learn/courses/30/lessons/42586

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

import java.util.*;

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        Queue<Integer> queue = new LinkedList<>();
        for(int i=0; i<progresses.length; i++){ // 배포하기 까지 얼마나 걸리나
            float p = progresses[i];
            float s = speeds[i];
            // 100에서 progresses 만큼 빼고 속도로 나누어주기
            // 소수점은 올림
            int days = (int)Math.ceil((100-p) / s);
            queue.offer(days); // 큐에 저장
        }
        
        // answer 큐 생성
        Queue<Integer> answer = new LinkedList<>();
        int d = queue.poll(); // 맨 앞의 숫자 poll
        int count = 1; // 한 번에 배포할 수 있는 수
        while(!queue.isEmpty()){ // 큐가 빌 때까지 반복
            int n = queue.poll(); 
            // 맨 앞의 큐를 꺼내서 직전에 꺼낸 수와 비교
            if(d >= n) {
                count ++;
                continue;
            }
            // 위의 if문에 안걸린다면 실행
            answer.offer(count);
            count = 1;
            d = n;
        }
        // 맨 마지막 count 값을 offer해줘야 함
        answer.offer(count);
        // answer 큐를 배열로 바꿔서 return
        return answer.stream().mapToInt(Integer::intValue).toArray();
    }
}