티스토리 뷰
처음
int index = 0;
int target = queue.poll();
list.add(1);
while(queue.size() >= 1){
int next = queue.peek();
if(target >= next){
list.set(index, list.get(index)+1);
queue.remove();
}
else{
index += 1;
list.add(index, 1);
target = next;
queue.remove();
}
}
처음 코드를 짤 때 큐에 모든 작업을 추가한 뒤
위와 같이 poll()과 peek()를 사용하여 처음과 다음을 비교하면서 진행했었는데
이렇게 하면 같이 배포 될 수 있는 작업을 구하기 위해 불필요한 변수와 연산이 생겨났다.
자바 코드
import java.util.*;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
Queue<Integer> queue = new LinkedList<>();
ArrayList<Integer> list = new ArrayList<>();
for(int i=0; i<progresses.length; i++){
double re = (100-progresses[i])/(double) speeds[i];
int day = (int) Math.ceil(re);
if(!queue.isEmpty() && queue.peek() < day){
list.add(queue.size());
queue.clear();
}
queue.add(day);
}
list.add(queue.size());
int[] answer = new int[list.size()];
for(int i=0; i<list.size(); i++){
answer[i] = list.get(i);
}
return answer;
}
}
ArrayList를 사용하는 이유는 크기가 정해져 있지 않기 때문이고 이를 다시 int 배열로 바꿔준다.
Math.ceil() 함수를 통해 올림을 하여 해당하는 날짜를 계산한다.
큐의 헤드 값이 해당 날짜보다 작다면 같이 배포될 수 없으므로 list에 값을 추가하고 큐를 비워준다.
이후 큐에 해당 날짜를 다시 더한다.
핵심은 list에 큐의 사이즈만큼 더해줌으로써 같이 배포될 수 있는 작업을 계산하는 것이다.
'Algorithm > java' 카테고리의 다른 글
[Java][프로그래머스] 방문 길이 (0) | 2023.05.08 |
---|---|
[Java][프로그래머스] 스킬트리 (0) | 2023.05.04 |
[Java][프로그래머스] 연속 부분 수열 합의 개수 (0) | 2023.05.04 |
[Java][프로그래머스] n^2 배열 자르기 (0) | 2023.05.04 |
[Java][프로그래머스] 할인 행사 (0) | 2023.05.03 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- swiper
- Java
- 완전탐색
- 스타일
- 슬라이드
- level2
- 스킬트리
- 방문 길이
- input
- 할인행사
- 알고리즘
- 기능개발
- 연속 부분 수열 합의 개수
- 브루트포스
- 정규식
- 프로그래머스
- 비밀번호 일치
- 회원가입
- 이메일
- 자바
- 자동 하이푼
- 커스텀훅
- 배열 자르기
- react
- Enter키
- 모음 사전
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
글 보관함