I n
D 1
D -1
D
연산이 들어오면 무시EMPTY
Operation operation = Operation.findBy(st.nextToken());
int num = Integer.parseInt(st.nextToken());
public enum Operation {
INSERT("I"),
DELETE("D"),
;
private final String operation;
Operation(String operation) {
this.operation = operation;
}
public String getOperation() {
return operation;
}
public boolean isDelete() {
return this == DELETE;
}
public boolean isInsert() {
return this == INSERT;
}
public static Operation findBy(String operation) {
for (Operation value : values()) {
if (value.getOperation().equals(operation)) {
return value;
}
}
throw new UnsupportedOperationException("해당하는 연산이 없습니다.");
}
}
if (operation.isInsert()) {
doublyProirityQueue.put(num, doublyProirityQueue.getOrDefault(num, 0) + 1);
continue;
}
if (operation.isDelete()) {
if (doublyProirityQueue.isEmpty()) {
continue;
}
if (isMinValueOf(num)) {
int value = doublyProirityQueue.get(doublyProirityQueue.firstKey());
if (value == 1) {
doublyProirityQueue.remove(doublyProirityQueue.firstKey());
continue;
}
doublyProirityQueue.put(doublyProirityQueue.firstKey(), value - 1);
continue;
}
if (doublyProirityQueue.isEmpty()) {
sb.append(EMPTY).append("\n");
continue;
}
sb.append(doublyProirityQueue.lastKey()).append(" ").append(doublyProirityQueue.firstKey()).append("\n");
EMPTY
를 출력한다.내부 구조
추가 참조
잠깐..
public K firstKey() {
return key(getFirstEntry());
}
// getFirstEntry() 는
final Entry<K,V> getFirstEntry() {
Entry<K,V> p = root;
if (p != null)
while (p.left != null)
p = p.left;
return p;
}
//left 혹은 right 노드를 타고 들어가면 항상 최소, 최댓값인 키임.
// getLastEntry() 는
final Entry<K,V> getLastEntry() {
Entry<K,V> p = root;
if (p != null)
while (p.right != null)
p = p.right;
return p;
}