๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/42627
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก์ Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
< ๋์คํฌ ์ปจํธ๋กค๋ฌ >
๋ฌธ์ ํ์ด (Java)
import java.util.*;
class Solution {
public int solution(int[][] jobs) {
int answer = 0;
Arrays.sort(jobs, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[2] == o2[2]) {
if (o1[1] == o2[1]) {
return o1[0] - o2[0];
}
return o1[1] - o2[1];
}
return o1[2] - o2[2];
}
});
int idx = 0, time = 0;
while (idx < jobs.length) {
while (idx < jobs.length && time >= jobs[idx][0]) {
queue.add(new int[] { idx, jobs[idx][0], jobs[idx][1] });
idx += 1;
}
if (queue.isEmpty()) {
time = jobs[idx][0];
while (idx < jobs.length && time >= jobs[idx][0]) {
queue.add(new int[] { idx, jobs[idx][0], jobs[idx][1] });
idx += 1;
}
}
int job[] = queue.poll();
if (time < job[1]) {
time = job[1] + job[2];
} else {
time += job[2];
}
answer += time - job[1];
}
while (!queue.isEmpty()) {
int job[] = queue.poll();
if (time < job[1]) {
time = job[1] + job[2];
} else {
time += job[2];
}
answer += time - job[1];
}
answer /= jobs.length;
return answer;
}
}
jobs๋ฅผ ์์ ์ ์์ฒญ ์๊ฐ์ด ๋น ๋ฅธ ์์ผ๋ก ์ ๋ ฌํ๋ค. ์ฐ์ ์์ ํ๋ฅผ ์์ ์ ์์ ์๊ฐ์ด ์งง์ ๊ฒ, ์์ ์ ์์ฒญ ์๊ฐ์ด ๋น ๋ฅธ ๊ฒ, ์์ ์ ๋ฒํธ๊ฐ ์์ ๊ฒ ์์ผ๋ก ์ ๋ ฌํ๋๋ก ์ ์ธํ๋ค. ๋ชจ๋ ์์ ์ด ์ํ๋๋๋ก ๋ค์ ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
1. ํ์ฌ ์๊ฐ๋ณด๋ค ์์ ์์ฒญ ์๊ฐ์ด ๋น ๋ฅธ ์์ ๋ค์ ์ฐ์ ์์ ํ์ ์ถ๊ฐ
2. ํ์ฌ ์๊ฐ์ ์์ฒญ๋ ์์ ์ด ์์ด ์ฐ์ ์์ ํ๊ฐ ๋น์ด์๋ค๋ฉด ํ์ฌ ์๊ฐ ์ ๋ฐ์ดํธ ๋ฐ ์ฐ์ ์์ ํ์ ์ถ๊ฐ
3. queue poll
4. ํ์ฌ ์๊ฐ ์ ๋ฐ์ดํธ ๋ฐ ์์ฒญ ์์ ์ ๋ฐํ ์๊ฐ์ ๊ตฌํด answer์ ๋ํ๊ธฐ
๋ง์ฝ ์ฐ์ ์์ ํ์ ์์ ์ด ๋จ์์๋ค๋ฉด ์์ 3~4๋ฒ์ ๋ฐ๋ณตํด ์ค๋ค. ์ต์ข answer์ ๊ฐ์ jobs์ ๊ธธ์ด๋ก ๋๋ ๊ฐ์ ๋ฐํํ๋ค.

์ถ์ฒ: ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉ ํ ์คํธ ์ฐ์ต,
https://school.programmers.co.kr/learn/challenges
'๐Algorithm > ๐ฅprogrammers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [programmers] ์ด์ค์ฐ์ ์์ํ (0) | 2026.07.27 |
|---|---|
| [programmers] ๋ฒ ์คํธ์จ๋ฒ (0) | 2026.07.24 |
| [programmers] ์ ์ ์ผ๊ฐํ (0) | 2026.07.22 |
| [programmers] ๊ฐ์ฅ ๋จผ ๋ ธ๋ (0) | 2026.07.21 |
| [programmers] ๋จ์ด ๋ณํ (0) | 2026.07.20 |