티스토리 뷰
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42586
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
프로그래머스 코딩테스트 고득점 Kit의 스택/큐 Level 2 문제다.
어떤 기능이 완료될 때까지의 일수는 다음과 같다.
(100 - 작업진도) / (개발 속도)
단위가 일수이기 때문에 딱 나누어 떨이지지 않을 경우 반올림 한다. (Math.ceil())
각 기능마다 완료될 때까지의 일수를 계산하여 큐에 넣은 후,
큐의 값을 하나씩 꺼내면서 이전 값과 비교하며 한번에 배포할 수 있는 기능의 수를 카운트한다.
import java.util.Queue;
import java.util.LinkedList;
import java.util.ArrayList;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
Queue<Integer> q = new LinkedList<>();
for(int i=0;i<progresses.length;i++) {
int day = (int)Math.ceil((double)(100 - progresses[i]) / speeds[i]);
q.offer(day);
}
ArrayList<Integer> result = new ArrayList<>();
int cnt = 1;
int pre = q.poll();
while(!q.isEmpty()) {
int now = q.poll();
if(pre >= now)
cnt++;
else {
result.add(cnt);
cnt = 1;
pre = now;
}
}
result.add(cnt);
int[] ans = new int[result.size()];
for(int i=0;i<ans.length;i++)
ans[i] = result.get(i);
return ans;
}
}
728x90
'algorithm > programmers' 카테고리의 다른 글
[프로그래머스/자바] 주식가격 풀이 (0) | 2022.11.14 |
---|---|
[프로그래머스/자바] 프린터 풀이 (0) | 2022.11.14 |
[프로그래머스/자바] 올바른 괄호 풀이 (0) | 2022.11.14 |
[프로그래머스/자바] 같은 숫자는 싫어 풀이 (0) | 2022.11.14 |
[프로그래머스/자바] 베스트 앨범 풀이 (1) | 2022.11.14 |