์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- ๋ชจ๋๋ฆฌํฑ ์ํคํ ์ฒ
- webrtc
- node์์กด์ฑ์ฃผ์
- ๊ฐ์ฒด์งํฅsolid
- ์๋ฐ์คํฌ๋ฆฝํธreduce
- ์๋ฐ๋ฐฐ์ด์ ๋ ฌ
- ์๋ฐ ๊ฐ๋น์ง์ปฌ๋ ์
- ์๋ฐfilter
- ์๋ฐforeach
- ๊ฐ์ฒด์งํฅ์ถ์ํ
- ์๋ฐComparable
- ํ๋ก๊ทธ๋๋จธ์ค๊ฐ์ฅํฐ์
- ์๋ฐ์คํฌ๋ฆฝํธfilter
- Programmers๊ฐ์ฅํฐ์
- ์คํ๋งํต์ฌ์์
- ํ๋ก์ ํธํ๊ณ
- ์๋ฐ์คํฌ๋ฆฝํธforeach
- ์๋ฐ์คํฌ๋ฆฝํธmap
- ๊ฐ์ฒด์งํฅ๋คํ์ฑ
- ํ๋ก๊ทธ๋๋จธ์ค๋๋์ง
- ์ฐ์ ์์ํ์๋ฐ
- ์๋ฐstream
- ์๋ฐComparator
- Programmers๋๋์ง
- ์๋ฐreduce
- openvidu
- ํด๋ผ์ฐ๋ํ์
- ์๋ฐ๋ฆฌ์คํธ์ ๋ ฌ
- ๋๋์ง์๋ฐ
- ํํ๋ก์ ํธ
- Today
- Total
I'm pine thank you and you?
ํ๋ก๊ทธ๋๋จธ์ค ์ด์ค์ฐ์ ์์ํ (Java, PriorityQueue) ๋ณธ๋ฌธ
๐ ๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/42628
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
๐ง PriorityQueue(์ฐ์ ์์ ํ) in Java
์๋ฃ๊ตฌ์กฐ ์ค ํ๋
์ผ๋ฐ์ ์ธ ํ์ฒ๋ผ ์ ์ ์ ์ถ(FIFO)์ธ๋ฐ, ์ฌ๊ธฐ์ ์ฐ์ ์์ ์กฐ๊ฑด์ด ์ถ๊ฐ๋ ์๋ฃ๊ตฌ์กฐ์ด๋ค.
์ฆ, ๋ฐ์ดํฐ๊ฐ ๋ค์ด์ฌ๋๋ง๋ค ์ฐ์ ์์์ ๋ง๊ฒ ์์์ ์ ๋ ฌ๋๊ณ ์ฐ์ ์์๊ฐ ๋์ ๊ฒ์ ๋จผ์ ๋ณด๋ด๋ ๊ตฌ์กฐ!
๋ฐ๋ผ์, ์ฐ์ ์์ ํ๋ฅผ ๊ตฌํํ ๋๋ ๋ฐ๋์ Comparator ๋๋ Comparable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํ ํด์ผ ํ๋ค.
Compatator๊ณผ Comparable์ ์ฐจ์ด๋ ์ด๊ณณ์ ํด๋ฆญ
(๊ตฌํํ์ง ์์ผ๋ฉด ๋ฎ์์ซ์ ์ฐ์ ์์๊ฐ default)
import java.util.PriorityQueue;
import java.util.Collections;
// ๋ฎ์์ซ์ ์ฐ์ ์์
PriorityQueue<Integer> priorityQueueDefault = new PriorityQueue<>();
// ๋์์ซ์ ์ฐ์ ์์
PriorityQueue<Integer> priorityQueueHighest = new PriorityQueue<>(Collections.reverseOrder());
// Comparable์ ์ด์ฉํ ์ฌ์ฉ์ ์ ์ (๋๋ค์)
PriorityQueue<Integer> priorityQueueComparable = new PriorityQueue<>((a, b) -> {
int first = Math.abs(a);
int second = Math.abs(b);
if(first == second) return (a > b ? 1 : -1);
else return first - second;
});
// Compatator์ ์ด์ฉํ ์ฌ์ฉ์ ์ ์
PriorityQueue<Integer> priorityQueueComparator = new PriorityQueue<>(new Comparator<Student>() {
@Override
public int compare(int a, int b) {
return a - b; // ์ค๋ฆ์ฐจ์
}
});
๐ ํ์ด
- ๋ฌธ์ ์ด๋ฆ ์ฒ๋ผ ์ฐ์ ์์ํ๋ฅผ ๋๊ฐ ์ ์ธํ์ฌ ํ๋ฉด ๋๋ค.
- ๋ฎ์ ์ซ์ ์ฐ์ ์์ํ(pqLow), ๋์ ์ซ์ ์ฐ์ ์์ํ(pqHigh)
- ์ฐ์ฐ ์กฐ๊ฑด์ ๋ฐ๋ผ ์ต์๊ฐ๊ณผ ์ต๋๊ฐ์ ์ญ์ ํ๋ค.
- ์ฐ์ฐ์ด ๋๋ ํ, ์ฐ์ ์์ํ ๋ ์ค์ ํ๋๋ผ๋ ๋น์ด์์ผ๋ฉด {0, 0}์, ์๋๋ฉด {์ต๋๊ฐ, ์ต์๊ฐ}์ ๋ฐํํ๋ค.
import java.util.Arrays;
import java.util.Collections;
import java.util.PriorityQueue;
class Solution {
public int[] solution(String[] operations) {
int[] answer = {};
PriorityQueue pqLow = new PriorityQueue<>();
PriorityQueue pqHigh = new PriorityQueue<>(Collections.reverseOrder());
for (String operation : operations) {
String o = operation.split(" ")[0];
int n = Integer.parseInt(operation.split(" ")[1]);
if (o.equals("I")) {
pqLow.add(n);
pqHigh.add(n);
} else if (o.equals("D")) {
if (pqLow.isEmpty() || pqHigh.isEmpty())
continue;
if (n < 0) {
int min = pqLow.poll();
pqHigh.remove(min);
} else {
int max = pqHigh.poll();
pqLow.remove(max);
}
}
}
if (pqLow.isEmpty() || pqHigh.isEmpty()) { // ๋์ค์ ํ๋๋ง ๋น์ด์๋์ง ํ์ธํด๋ ์๊ด์์ง๋ง ๋๋ค ์ ์ด์ฃผ์๋ค.
answer = new int[] {0, 0};
} else {
answer = new int[] {pqHigh.poll(), pqLow.poll()};
}
return answer;
}
}
}
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค ๋๋์ง (Java) (1) | 2023.03.28 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค ๊ฐ์ฅ ํฐ ์ (Java) (0) | 2023.03.20 |
ํต์ ๋ ฌ(Quick Sort) ์ฝ๊ฒ ์๊ธฐ (Java) (0) | 2022.12.13 |