-
Notifications
You must be signed in to change notification settings - Fork 3
/
[프로그래머스] 신재웅_이중우선순위큐.swift
46 lines (38 loc) · 1.09 KB
/
[프로그래머스] 신재웅_이중우선순위큐.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import Foundation
func solution(_ operations:[String]) -> [Int] {
var queue = DoublePriorityQueue()
for operation in operations {
queue.operate(operation)
}
return queue.result()
}
struct DoublePriorityQueue {
private var elements = [Int]()
mutating func operate(_ string: String) {
let array = string.split(separator: " ")
guard let operation = array.first,
let last = array.last,
let number = Int(last),
array.count == 2
else {
return
}
switch operation {
case "I":
elements.append(number)
case "D":
if number == 1 {
elements = elements.dropLast()
} else if number == -1 {
elements = Array(elements.dropFirst())
}
default : break
}
elements.sort()
}
func result() -> [Int] {
let max = elements.max() ?? 0
let min = elements.min() ?? 0
return [max, min]
}
}