Tags
- Python
- stack
- ์๋ฃ๊ตฌ์กฐ
- ๊ทธ๋ํ ํ์
- BOJ
- ๊ตฌํ
- Dynamic Programming
- CodingTest
- ๊ทธ๋ํ ์ด๋ก
- ์ ๋ ฌ
- ๋ฌธ์์ด
- sort
- ๋ฐฑํธ๋ํน
- dfs
- ์๋ฎฌ๋ ์ด์
- ๊ต์ฌ
- greedy
- PGM
- queue
- Study
- SpringBoot
- ์ ์๋ก
- ์ํ
- BFS
- ๋๋น ์ฐ์ ํ์
- Brute Force Algorithm
- DP
- Java
- LV2
- ๊น์ด ์ฐ์ ํ์
Archives
๊ธฐ๋ก๋ฐฉ
BOJ_11279 : ์ต๋ ํ ๋ณธ๋ฌธ
๐ธ ๋ฌธ์ ๋ถ์ ๐ธ
- ๋ฐฐ์ด์ ์์ฐ์๋ฅผ ๋ฃ๊ฑฐ๋, ๋ฐฐ์ด์ ์ต๋๊ฐ์ ๊บผ๋ด์ถ๋ ฅํ๋ ์ฐ์ฐ์ด 100,000๋ฒ๊น์ง ์ฃผ์ด์ง๋ค.
- ์ต๋๊ฐ์ ๋น ๋ฅด๊ฒ ๊ตฌํ๊ธฐ ์ํด, ๋งค๋ฒ ์ ๋ ฌํ๋ ๊ฒ์ด ์๋๋ผ ์ต๋ํ์ ๊ตฌํํด์ผ ํ๋ค.
๐ธ ์ฝ๋ ๐ธ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
int x = Integer.parseInt(br.readLine());
if (x == 0) {
if (pq.isEmpty()) sb.append(0).append('\n');
else sb.append(pq.poll()).append('\n');
} else {
pq.add(x);
}
}
System.out.print(sb);
}
}
๐ธ ์ฝ๋ ํด์ ๐ธ
- java์ PriorityQueue(์ฐ์ ์์ ํ)๋ ์ต์ํ์ผ๋ก ๊ตฌํ๋์ด ์์ผ๋ฏ๋ก, Collections.reverseOrder()๋ฅผ ๋ฃ์ด์ ์ต๋ ํ์ผ๋ก ๋ง๋ ๋ค.
๐ธ end ๐ธ
- ์ฐ์ ์์ ํ ์ด๊ธฐ๊ฐ์ด ์ต์ํ์ธ์ง ์ต๋ํ์ธ์ง ํท๊น๋ ธ๋ค.
- ๊ธฐ๋ณธ ๊ฐ์ ์ต์ํ์ด๊ณ , ์์๊ฒ ์๋ก ๊ฐ์ ํผ๋ผ๋ฏธ๋/ํธ๋ฆฌ ๊ฐ์ ํํ๊ฐ ๋๋ค๊ณ ๊ธฐ์ตํด์ผ๊ฒ ๋ค.
728x90
'CodingTest > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ_1806 : ๋ถ๋ถํฉ (0) | 2023.04.18 |
---|---|
BOJ_2589 : ๋ณด๋ฌผ์ฌ (0) | 2023.04.18 |
Lv.1 : ํฌ๊ธฐ๊ฐ ์์ ๋ถ๋ถ ๋ฌธ์์ด (0) | 2023.04.18 |
Lv.1 : ๊ฐ์ฅ ๊ฐ๊น์ด ๊ฐ์ ๊ธ์ (0) | 2023.04.18 |
Lv.2 : ๋ฌธ์์ด ๋๋๊ธฐ (0) | 2023.04.17 |