๐โโ๏ธ ๋ค์ด๊ฐ๋ฉฐ
์ด๋ฒ ์๊ฐ์๋ PriorityQueue๋ฅผ ๋ฐฐ์๋ณด์
Q๋ FIFO์ธ๋ฐ, ๊ทธ์ ๋ฌ๋ฆฌ PQ๋ ์ ๋ ฌ์ ํตํด ์ค๋ฆ์ฐจ์, ๋ด๋ฆผ์ฐจ์์ผ๋ก๋ ๊ฐ๋ฅํ๋ค
heap ๊ธฐ๋ฐ์ผ๋ก ์๊ฐ๋ณต์ก๋๋ ๋ค์๊ณผ ๊ฐ๋ค
1
2
3
| ์ฝ์
(offer) -> O(log N)
์ญ์ (poll) -> O(log N)
์กฐํ (peek) -> O(1)
|
โ
์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PriorityQueue<int[]> pq = new PriorityQueue<> (
(a,b) -> a[0] - b[0]
);
pq.offer(new int[] {3, 100});
pq.offer(new int[] {1, 200});
pq.offer(new int[] {2, 50});
while (!pq.isEmpty()) {
int[] cur = pq.poll();
System.out.println(Arrays.toString(cur));
}
}
}
|
์ถ๋ ฅ๊ฐ
1
2
3
| [1, 200]
[2, 50]
[3, 100]
|
์์ฒ๋ผ a[0] - b[0] ๊ฐ ์๋ ์ฝ๊ฐ์ ๋ณํ๋ฅผ ์ฃผ์
์ถ๋ ฅ๊ฐ
1
2
3
| [2, 50]
[3, 100]
[1, 200]
|
๊ทธ๋ ๋ค๋ฉด ์ด๋ฒ์๋ ๋ด๋ฆผ์ฐจ์์ ๋ณผ๊น?
์์๋ฆฌ๋ง ๋ฐ๊ฟ๋ณด์
์ถ๋ ฅ๊ฐ
1
2
3
| [3, 100]
[2, 50]
[1, 200]
|
์ด๋ฒ์๋ ๋ท์๋ฆฌ๋ฅผ ๋ฐ๊ฟ๋ณด์
์ถ๋ ฅ๊ฐ
1
2
3
| [1, 200]
[3, 100]
[2, 50]
|