๐ก ๋ฌธ์ ์์ฝ
- ํธ์ถ๋ ํ๋ก๊ทธ๋จ ์ค ์ ์๊ฐ ๋ฎ์ ๊ฒ๋ถํฐ ์คํ
- ์ ์๊ฐ ๊ฐ๋ค๋ฉด ๋จผ์ ํธ์ถ๋ ๊ฒ๋ถํฐ ์คํ
- ์คํ ๊ฐ๋ฅํ ํ๋ก๊ทธ๋จ์ด ์์ผ๋ฉด ๊ฐ์ฅ ๊ฐ๊น์ด ํธ์ถ ์๊ฐ์ผ๋ก ์ ํ
- ์คํ ๋์ค์๋ ๋ค๋ฅธ ํ๋ก๊ทธ๋จ์ด ์๋ ํ์ ๋ฃ๊ธฐ๋ง ํ๊ณ ๋ฌด์
- ๊ฐ ์ ์๋ณ ๋๊ธฐ ์๊ฐ ๋์ ํฉ๊ณผ ์ ์ฒด ์คํ ์๋ฃ ์๊ฐ์ ๋ฐํ
๐ง ๋ฌธ์ ๋ฐฐ๊ฒฝ
- OS ์ค์ผ์ค๋ง, ์ด๋ฒคํธ ๋ฃจํ, ๋๊ธฐ์ด ์ฒ๋ฆฌ ๋ฑ์์ ์ฌ์ฉ๋๋ ์ฐ์ ์์ ๊ธฐ๋ฐ ์์
์คํ ์๋ฎฌ๋ ์ด์
- ์ ์๋ ์ฐ์ ์์, ํธ์ถ ์๊ฐ์ ์ ๋ ฌ ๊ธฐ์ค, ์คํ ์๊ฐ์ ์ ์ ์๊ฐ์ผ๋ก ์์ฉ
๐ ์๊ณ ๋ฆฌ์ฆ ์ค๊ณ
1๏ธโฃ ์
๋ ฅ ์ ๋ ฌ
- ํธ์ถ ์๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
2๏ธโฃ ์ฐ์ ์์ ํ ์ฌ์ฉ
score ์ค๋ฆ์ฐจ์ โ ํธ์ถ ์ ์ธ๋ฑ์ค ์ค๋ฆ์ฐจ์์ผ๋ก ์คํํ ์์
์ ๊ด๋ฆฌ
3๏ธโฃ ์คํ ํ๋ฆ
- ํ์ฌ ์๊ฐ ๊ธฐ์ค์ผ๋ก ์คํ ๊ฐ๋ฅํ ์์
์ ํ์ ์ถ๊ฐ
- ํ๊ฐ ๋น์ด์์ผ๋ฉด ๋ค์ ํธ์ถ ์๊ฐ์ผ๋ก ์ ํ
- ํ์์ ๊ฐ์ฅ ์ฐ์ ์์ ๋์ ์์
์ ๊บผ๋ด ์คํ
- ์คํ ์๊ฐ๋งํผ ํ์ฌ ์๊ฐ ์ฆ๊ฐ + ๋๊ธฐ ์๊ฐ ๋์
๐ป ํต์ฌ ๊ตฌํ ์ฝ๋
Arrays.sort(program, Comparator.comparingInt(p -> p[1]));
PriorityQueue<int[]> waitQ = new PriorityQueue<>(
Comparator.comparingInt((int[] a) -> a[0]) // ์ ์
.thenComparingInt(a -> a[3]) // ํธ์ถ ์
);
int time = 0;
int idx = 0;
while (idx < program.length || !waitQ.isEmpty()) {
while (idx < program.length && program[idx][1] <= time) {
waitQ.offer(new int[]{program[idx][0], program[idx][1], program[idx][2], idx});
idx++;
}
if (waitQ.isEmpty()) {
time = program[idx][1];
continue;
}
int[] cur = waitQ.poll();
int score = cur[0];
int calledTime = cur[1];
int runTime = cur[2];
answer[score] += time - calledTime;
time += runTime;
}
โ
์๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋จ๊ณ |
๋ณต์ก๋ |
| ์ ๋ ฌ |
O(N log N) |
| ํ ์ฝ์
/์ญ์ |
O(log N) |
| ์ ์ฒด ๋ฃจํ |
O(N log N) |
| ์ดํฉ |
O(N log N) |
๐ ์ ๋ฆฌ
score โ ํธ์ถ ์ ๋ณตํฉ ์ฐ์ ์์๋ PriorityQueue๋ก ์ฝ๊ฒ ๊ตฌํ ๊ฐ๋ฅ
- ์๊ฐ ์ ํ ์ฒ๋ฆฌ๋ฅผ ํตํด ๋ถํ์ํ ์๊ฐ ๋ญ๋น ์์ด ์๋ฎฌ๋ ์ด์
๊ตฌํ
- ๊ฐ ์ ์๋ณ ๋๊ธฐ ์๊ฐ์ ๋ฐ๋ก ๋ฐฐ์ด์ ์ ์ฅํ์ฌ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ