๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/155651
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก์ Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
< ํธํ ๋์ค >
๋ฌธ์ ํ์ด (Java)
import java.util.*;
class Solution {
public int solution(String[][] book_time) {
int answer = 0;
Arrays.sort(book_time, new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
int h1 = Integer.parseInt(o1[0].split(":")[0]);
int m1 = Integer.parseInt(o1[0].split(":")[1]);
int h2 = Integer.parseInt(o2[0].split(":")[0]);
int m2 = Integer.parseInt(o2[0].split(":")[1]);
if (h1 == h2) {
return m1 - m2;
}
return h1 - h2;
}
});
PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[0] == o2[0]) {
return o1[1] - o2[1];
}
return o1[0] - o2[0];
}
});
for (int i = 0; i < book_time.length; i++) {
int h1 = Integer.parseInt(book_time[i][0].split(":")[0]);
int m1 = Integer.parseInt(book_time[i][0].split(":")[1]);
int h2 = Integer.parseInt(book_time[i][1].split(":")[0]);
int m2 = Integer.parseInt(book_time[i][1].split(":")[1]);
if (!queue.isEmpty()) {
if (queue.peek()[0] == h1) {
if (queue.peek()[1] <= m1) {
queue.poll();
}
} else if (queue.peek()[0] < h1) {
queue.poll();
}
}
m2 += 10;
if (m2 >= 60) {
m2 -= 60;
h2 += 1;
}
queue.add(new int[] { h2, m2 });
answer = Math.max(answer, queue.size());
}
return answer;
}
}
๋จผ์ ์์ฝ ์๊ฐ์ ๋์ค ์์ ์๊ฐ์ด ๋น ๋ฅธ ์๋๋ก ์ ๋ ฌํ๋ค. ๊ทธ ํ PriorityQueue๋ฅผ ์ข ๋ฃ ์๊ฐ์ด ๋น ๋ฅธ ์๋๋ก ์ ๋ ฌํ๋๋ก ์ ์ธํ๋ค. ์์ฝ ์๊ฐ์ ํ์ํ๋ฉฐ PriorityQueue์ ๊ฐ์ด ์๋ค๋ฉด ์ฒซ ๋ฒ์งธ ๊ฐ์ ์ข ๋ฃ ์๊ฐ๊ณผ ์์ฝ ์๊ฐ์ ์์ ์๊ฐ์ ๋น๊ตํด ์์ ์๊ฐ์ด ์ข ๋ฃ ์๊ฐ๋ณด๋ค ๋ค๋ผ๋ฉด pollํ ํ ์ถ๊ฐํ๋ค. ์์ ์๊ฐ์ด ์ข ๋ฃ ์๊ฐ๋ณด๋ค ์์ด๊ฑฐ๋ PriorityQueue๊ฐ ๋น์ด์๋ค๋ฉด ๊ทธ๋ฅ ์ถ๊ฐํ๋ค. ์ด๋, PriorityQueue์ ํฌ๊ธฐ์ ์ต๋๊ฐ์ answer๋ก ์ ๋ฐ์ดํธํ๋ค.
์ต์ข answer์ ๋ฐํํ๋ค.
* ๋ค ํ๊ณ ๋ณด๋ ์๊ฐ์ ๊ณ์ํด์ h, m์ผ๋ก ๋๋๋ ๊ฒ๋ณด๋ค m์ผ๋ก ํต์ผํด์ ํ๋ฉด ์ข์๊ฒ ๋ค๋ ์๊ฐ์ด ๋ค์๋ค.

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