๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/42628
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก์ Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
์ด ๋ฌธ์ ๋ฅผ ํ๋ฉด์ ์์ ์ ํ์๋ ๋ฌธ์ ๊ฐ ๊ธฐ์ต์ด ์ ๋์ ์ฐธ๊ณ ํ๋ค
https://melody-coding.tistory.com/318
[Baekjoon] 7662_์ด์ค ์ฐ์ ์์ ํ
Gold IV๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/7662) ๋ฌธ์ ํ์ด & ์๊ฐ ์ฒ์์๋ ์ต๋๊ฐ๊ณผ ์ต์๊ฐ์ ๊ด๋ฆฌํ๊ธฐ ์ํด ์ฐ์ ์์ ํ๋ฅผ 2๊ฐ ์ ์ธํด์ ์ค๋ฆ์ฐจ์, ๋ด๋ฆผ์ฐจ์ ์์ผ๋ก ์ ๋ ฌํด์ ์ฌ์ฉํ๋ค. ์๊ฐ์
melody-coding.tistory.com
https://melody-coding.tistory.com/317
[์๋ฃ๊ตฌ์กฐ] TreeMap
โ TreeMap ์ด๋?์ด์งํธ๋ฆฌ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ Map ์ปฌ๋ ์ ๊ฐ์ฒด ์ ์ฅ ์ ์๋ ์ ๋ ฌ(default : ์ค๋ฆ์ฐจ์) โTreeMap ์ ์ธTreeMap map=new TreeMap(); โ TreeMap ์ถ๊ฐ, ์ญ์ // ์ถ๊ฐmap.put(1,1);// ์ญ์ map.remove(1); โ T
melody-coding.tistory.com
< ์ด์ค์ฐ์ ์์ํ >
๋ฌธ์ ํ์ด (Java)
import java.util.*;
class Solution {
public int[] solution(String[] operations) {
int[] answer = { 0, 0 };
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int i = 0; i < operations.length; i++) {
String str[] = operations[i].split(" ");
int num = Integer.parseInt(str[1]);
if (str[0].charAt(0) == 'I') {
if (map.containsKey(num)) {
map.put(num, map.get(num) + 1);
} else {
map.put(num, 1);
}
} else {
if (map.size() == 0) {
continue;
}
if (num == 1) {
if (map.lastEntry().getValue() == 1) {
map.remove(map.lastKey());
} else {
map.put(map.lastKey(), map.get(map.lastKey()) - 1);
}
} else {
if (map.firstEntry().getValue() == 1) {
map.remove(map.firstKey());
} else {
map.put(map.firstKey(), map.get(map.firstKey()) - 1);
}
}
}
}
if (!map.isEmpty()) {
answer[0] = map.lastKey();
answer[1] = map.firstKey();
}
return answer;
}
}
TreeMap์ ์ ์ธํ ํ operations๋ฅผ ํ์ํ๋ฉฐ ๋ค์ ๊ณผ์ ์ ๊ฑฐ์น๋ค.
1. I๋ผ๋ฉด map์ ์ถ๊ฐ
2. D์ด๊ณ map์ด ๋น์๋ค๋ฉด ๋ค์ ๋ช ๋ น์ด๋ก ๋์ด๊ฐ๊ธฐ
3. D์ด๊ณ 1์ด๋ผ๋ฉด ์ต๋๊ฐ ์ญ์ , -1์ด๋ผ๋ฉด ์ต์๊ฐ ์ญ์
์ต์ข TreeMap์ด ๋น์ด์์ง ์๋ค๋ฉด ์ต๋๊ฐ๊ณผ ์ต์๊ฐ์ answer์ ์ ์ฅํ ํ ๋ฐํํ๋ค.

์ถ์ฒ: ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉ ํ ์คํธ ์ฐ์ต,
https://school.programmers.co.kr/learn/challenges
'๐Algorithm > ๐ฅprogrammers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [programmers] ๋ฒ ์คํธ์จ๋ฒ (0) | 2026.07.24 |
|---|---|
| [programmers] ์ ์ ์ผ๊ฐํ (0) | 2026.07.22 |
| [programmers] ๊ฐ์ฅ ๋จผ ๋ ธ๋ (0) | 2026.07.21 |
| [programmers] ๋จ์ด ๋ณํ (0) | 2026.07.20 |
| [programmers] ๋คํธ์ํฌ (0) | 2026.07.15 |