๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/176962
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก์ Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
< ๊ณผ์ ์งํํ๊ธฐ >
๋ฌธ์ ํ์ด (Java)
import java.util.*;
class Solution {
static class Plan {
private String name;
private int start;
private int playtime;
public Plan(String name, int start, int playtime) {
this.name = name;
this.start = start;
this.playtime = playtime;
}
}
public String[] solution(String[][] plans) {
String[] answer = new String[plans.length];
PriorityQueue<Plan> queue = new PriorityQueue<>(new Comparator<Plan>() {
@Override
public int compare(Plan o1, Plan o2) {
return o1.start - o2.start;
}
});
for (int i = 0; i < plans.length; i++) {
int start = Integer.parseInt(plans[i][1].split(":")[0]) * 60 + Integer.parseInt(plans[i][1].split(":")[1]);
queue.add(new Plan(plans[i][0], start, Integer.parseInt(plans[i][2])));
}
Stack<Plan> stack = new Stack<>();
int idx = 0, now = 0;
while (!queue.isEmpty()) {
if (stack.isEmpty()) {
stack.add(queue.poll());
now = stack.peek().start;
} else if (now + stack.peek().playtime > queue.peek().start) {
stack.peek().playtime -= (queue.peek().start - now);
stack.add(queue.poll());
now = stack.peek().start;
} else {
now += stack.peek().playtime;
answer[idx++] = stack.pop().name;
}
}
while (!stack.isEmpty()) {
answer[idx++] = stack.pop().name;
}
return answer;
}
}
๊ณผ์ ์ด๋ฆ, ๊ณผ์ ์์ ์๊ฐ์ ๋ถ์ผ๋ก ํต์ผํ ์๊ฐ, ๊ณผ์ ๋ง์น๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ํ๋๋ก ๊ฐ์ง๋ ํด๋์ค๋ฅผ ์ ์ธํ๋ค. ์ฐ์ ์์ ํ์ ๊ณผ์ ๊ณํ์ ๊ฐ์ฒด๋ก ์ ์ฅํ์ฌ ๊ณผ์ ์์ ์๊ฐ์ด ๋น ๋ฅธ ์์ผ๋ก ์ ๋ ฌํ๋ค.
์ฐ์ ์์ ํ๊ฐ ๋น ๋๊น์ง ๋ค์ ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
1) stack์ด ๋น์ด์๋ค๋ฉด queue์์ poll ํ ๊ฐ์ ์ ์ฅ ๋ฐ ํ์ฌ ์๊ฐ์ ๊ณผ์ ์์ ์๊ฐ์ผ๋ก ์ ๋ฐ์ดํธํ๋ค.
2) ์๋ก์ด ๊ณผ์ ๋ฅผ ์์ํ ์๊ฐ์ด ๋์๋ค๋ฉด ๊ธฐ์กด์ ์งํ ์ค์ด๋ ๊ณผ์ ๋ฅผ ๋ฉ์ถ๊ณ ์๋ก์ด ๊ณผ์ ๋ฅผ ์์ํ๋ค. ์ด๋, ๊ณผ์ ๋ฅผ ์งํํ ๋งํผ ๊ณผ์ ๋ฅผ ๋ง์น๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ์ ๋ฐ์ดํธํ๋ค. ํ์ฌ ์๊ฐ๋ ์๋ก์ด ๊ณผ์ ์์ ์๊ฐ์ผ๋ก ์ ๋ฐ์ดํธํ๋ค.
3) ์งํ ์ค์ด๋ ๊ณผ์ ๋ฅผ ๋๋๋ค๋ฉด ํ์ฌ ์๊ฐ์ ๊ณผ์ ๋๋ ์๊ฐ์ผ๋ก ์ ๋ฐ์ดํธํ๊ณ answer์ stack์์ pop ํ ๊ฐ์ ๊ณผ์ ์ด๋ฆ์ ์ ์ฅํ๋ค.
stack์ด ๋น ๋๊น์ง pop ํ์ฌ ๊ณผ์ ์ด๋ฆ์ answer์ ์์๋๋ก ์ ์ฅํ๋ค. ์ต์ข answer์ ๋ฐํํ๋ค.

์ถ์ฒ: ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉ ํ ์คํธ ์ฐ์ต,
https://school.programmers.co.kr/learn/challenges
'๐Algorithm > ๐ฅprogrammers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [programmers] ๋ ๋ฐ๋จน๊ธฐ (0) | 2026.06.01 |
|---|---|
| [programmers] ๊ณต์ ์ฐ์ฑ (0) | 2026.05.28 |
| [programmers] ๋ฌด์ธ๋ ์ฌํ (0) | 2026.05.27 |
| [programmers] ๋ฐํํ๋ฉด ์ ๋ฆฌ (0) | 2026.05.26 |
| [programmers] ํธํ ๋์ค (0) | 2026.05.22 |