먼저 배포되어야 하는 순서대로 작업의 진도가 적힌 정수 배열 progresses와 각 작업의 개발 속도가 적힌 정수 배열 speeds가 주어질 때 각 배포마다 몇 개의 기능이 배포되는지 찾는 문제
- 원소의 합이 같게 만들 수 없으면 -1 반환
- long 타입 고려할 것
풀이 순서
Math.ceil((100.0 - progresses[i]) / 개발 속도[i]) = 올림 처리한 작업 일수
순차적으로 꺼내기 위해 queue
올림 처리를 위해 실수로 계산, 마지막 타입 고려할 것
while(!isEmpty)
cnt = 1;
cur = poll()
while(!isEmpty() && 현재 작업이 ≥ 다음 작업) {
cnt++;
제거
}
list.add(cnt);
풀이 시작 20:40
풀이 종료 20:57
다시 풀이
내 풀이
import java.util.*;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
int[] answer = {};
Deque<Integer> q = new ArrayDeque<>();
for(int i=0; i<progresses.length; i++) {
q.add((int) Math.ceil((100.0- progresses[i]) / speeds[i]));
}
List<Integer> list = new ArrayList<>();
int cnt = 0;
while(!q.isEmpty()) {
cnt = 1;
int cur = q.remove();
while(!q.isEmpty() && cur >= q.peek()) {
cnt++;
q.remove();
}
list.add(cnt);
}
return list.stream()
.mapToInt(Integer::intValue)
.toArray();
}
}
다른 사람의 풀이 참고
import java.util.*;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
int[] answer = {};
Deque<Integer> q = new ArrayDeque<>();
for(int i=0; i<progresses.length; i++) {
q.add((int) Math.ceil((100.0- progresses[i]) / speeds[i]));
}
List<Integer> list = new ArrayList<>();
int cnt = 0;
while(!q.isEmpty()) {
cnt = 1;
int cur = q.remove();
while(!q.isEmpty() && cur >= q.peek()) {
cnt++;
q.remove();
}
list.add(cnt);
}
return list.stream()
.mapToInt(Integer::intValue)
.toArray();
}
}
새로 알게된 사실
까먹지 말자!
stream() list 안의 데이터를 Stream으로 변환, 데이터를 한 줄로 흘려보내 가공할 수 있게 함.
mapToInt(): Stream 안의 Integer 객체를 기본형 int 값으로 변환
Integer: Java의 참조형(Wrapper) 클래스
toArray() 변환된 int 값들을 배열로 모아 반환
'준비 > 알고리즘 공부' 카테고리의 다른 글
| [완전탐색] Combinations (0) | 2025.09.10 |
|---|---|
| [완전탐색] permutations (0) | 2025.09.10 |
| [스택/큐] 주식가격 (0) | 2025.09.08 |
| [스택/큐] 괄호 회전하기 (0) | 2025.09.08 |
| [스택/큐] 두 큐 합 같게 만들기 (0) | 2025.09.08 |