Skip to content

Commit ef74237

Browse files
committed
[level 3] Title: 이중우선순위큐, Time: 115.39 ms, Memory: 144 MB -BaekjoonHub
1 parent fa7fcbc commit ef74237

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

프로그래머스/3/42628. 이중우선순위큐/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### 성능 요약
66

7-
메모리: 133 MB, 시간: 67.80 ms
7+
메모리: 144 MB, 시간: 115.39 ms
88

99
### 구분
1010

@@ -16,7 +16,7 @@
1616

1717
### 제출 일자
1818

19-
2025년 08월 30일 12:24:03
19+
2025년 08월 30일 12:33:25
2020

2121
### 문제 설명
2222

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
11
import java.util.*;
22
class Solution {
33
public int[] solution(String[] operations) {
4-
// key 값을 기준으로 정렬된 TreeMap
54
TreeMap<Integer,Integer> map = new TreeMap<>();
6-
7-
for(String operation: operations){
8-
String[] params = operation.split(" ");
9-
String command = params[0]; // 명령어
10-
int number = Integer.parseInt(params[1]); // 숫자
11-
if(command.equals("I")){ // 삽입 명령어
12-
map.put(number, map.getOrDefault(number,0) + 1);
13-
} else { // 삭제 명령어
14-
if (map.size() > 0){
15-
if(number > 0) { // 최댓값 삭제
16-
int lastKey = map.lastKey();
17-
int cnt = map.get(lastKey);
18-
if(cnt == 1){
19-
map.remove(lastKey);
20-
} else {
21-
map.put(lastKey, cnt - 1);
22-
}
23-
} else { // 최솟값 삭제
24-
int firstKey = map.firstKey();
25-
int cnt = map.get(firstKey);
26-
if(cnt == 1){
27-
map.remove(firstKey);
28-
} else {
29-
map.put(firstKey, cnt - 1);
30-
}
31-
}
5+
6+
for(String operation : operations){
7+
String command = operation.split(" ")[0];
8+
int number = Integer.parseInt(operation.split(" ")[1]);
9+
10+
if(command.equals("I")){
11+
map.put(number,map.getOrDefault(number,0) +1);
12+
} else{
13+
if(map.size() > 0){
14+
if(number == -1) { // 최솟값 제거
15+
int firstKey = map.firstKey();
16+
int cnt = map.get(firstKey);
17+
if(cnt > 1) {
18+
map.put(firstKey,cnt - 1);
19+
} else{
20+
map.remove(firstKey);
21+
}
22+
} else{ // 최댓값 제거
23+
int lastKey = map.lastKey();
24+
int cnt = map.get(lastKey);
25+
if(cnt > 1) {
26+
map.put(lastKey,cnt - 1);
27+
} else{
28+
map.remove(lastKey);
29+
}
30+
}
3231
}
3332
}
3433
}
35-
if(map.size() == 0) return new int[]{0,0};
36-
37-
return new int[]{map.lastKey(), map.firstKey()};
34+
if(map.size() == 0) return new int[]{0,0};
35+
return new int[]{map.lastKey(),map.firstKey()};
3836
}
3937
}

0 commit comments

Comments
 (0)