-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PriorityQueue를 사용해서 풀어야 했는데 리스트를 사용해서 풀었다. PriorityQueue 자료형을 사용한다면 정렬순서가 반대인 두 개의 PriorityQueue를 만들어서 풀면 된다.
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// https://school.programmers.co.kr/learn/courses/30/lessons/42628 | ||
// 힙 | ||
// 이중우선순위큐 | ||
|
||
// 문제 설명 | ||
// 이중 우선순위 큐는 다음 연산을 할 수 있는 자료구조를 말합니다. | ||
// 명령어 수신 탑(높이) | ||
// I 숫자 큐에 주어진 숫자를 삽입합니다. | ||
// D 1 큐에서 최댓값을 삭제합니다. | ||
// D -1 큐에서 최솟값을 삭제합니다. | ||
// 이중 우선순위 큐가 할 연산 operations가 매개변수로 주어질 때, 모든 연산을 처리한 후 큐가 비어있으면 [0,0] 비어있지 않으면 [최댓값, 최솟값]을 return 하도록 solution 함수를 구현해주세요. | ||
|
||
// 제한사항 | ||
// operations는 길이가 1 이상 1,000,000 이하인 문자열 배열입니다. | ||
// operations의 원소는 큐가 수행할 연산을 나타냅니다. | ||
// 원소는 “명령어 데이터” 형식으로 주어집니다.- 최댓값/최솟값을 삭제하는 연산에서 최댓값/최솟값이 둘 이상인 경우, 하나만 삭제합니다. | ||
// 빈 큐에 데이터를 삭제하라는 연산이 주어질 경우, 해당 연산은 무시합니다. | ||
|
||
// 입출력 예 | ||
// operations return | ||
// ["I 16", "I -5643", "D -1", "D 1", "D 1", "I 123", "D -1"] [0,0] | ||
// ["I -45", "I 653", "D 1", "I -642", "I 45", "I 97", "D 1", "D -1", "I 333"] [333, -45] | ||
|
||
// 입출력 예 설명 | ||
// 입출력 예 #1 | ||
// 16과 -5643을 삽입합니다. | ||
// 최솟값을 삭제합니다. -5643이 삭제되고 16이 남아있습니다. | ||
// 최댓값을 삭제합니다. 16이 삭제되고 이중 우선순위 큐는 비어있습니다. | ||
// 우선순위 큐가 비어있으므로 최댓값 삭제 연산이 무시됩니다. | ||
// 123을 삽입합니다. | ||
// 최솟값을 삭제합니다. 123이 삭제되고 이중 우선순위 큐는 비어있습니다. | ||
// 따라서 [0, 0]을 반환합니다. | ||
|
||
// 입출력 예 #2 | ||
// -45와 653을 삽입후 최댓값(653)을 삭제합니다. -45가 남아있습니다. | ||
// -642, 45, 97을 삽입 후 최댓값(97), 최솟값(-642)을 삭제합니다. -45와 45가 남아있습니다. | ||
// 333을 삽입합니다. | ||
// 이중 우선순위 큐에 -45, 45, 333이 남아있으므로, [333, -45]를 반환합니다. | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Collections; | ||
|
||
class Solution { | ||
public int[] solution(String[] operations) { | ||
List<Integer> sortedList = new ArrayList<>(); | ||
|
||
for (String operation : operations) { | ||
processOperation(operation, sortedList); | ||
} | ||
|
||
if (sortedList.isEmpty()) { | ||
return new int[]{0, 0}; | ||
} else { | ||
int lastIndex = sortedList.size() - 1; | ||
return new int[]{sortedList.get(lastIndex), sortedList.get(0)}; | ||
} | ||
} | ||
|
||
private void processOperation(String operation, List<Integer> sortedList) { | ||
if (operation.startsWith("I")) { | ||
insertNumberAndSort(operation, sortedList); | ||
} else if ("D 1".equals(operation) && !sortedList.isEmpty()) { | ||
removeLastElement(sortedList); | ||
} else if ("D -1".equals(operation) && !sortedList.isEmpty()) { | ||
removeFirstElement(sortedList); | ||
} | ||
} | ||
|
||
private void insertNumberAndSort(String operation, List<Integer> sortedList) { | ||
Integer number = getNumber(operation); | ||
sortedList.add(number); | ||
Collections.sort(sortedList); | ||
} | ||
|
||
private Integer getNumber(String operation) { | ||
String[] splits = operation.split(" "); | ||
return Integer.valueOf(splits[1]); | ||
} | ||
|
||
private void removeLastElement(List<Integer> sortedList) { | ||
sortedList.remove(sortedList.size() - 1); | ||
} | ||
|
||
private void removeFirstElement(List<Integer> sortedList) { | ||
sortedList.remove(0); | ||
} | ||
} |