-
Notifications
You must be signed in to change notification settings - Fork 0
/
1051.swift
38 lines (33 loc) · 852 Bytes
/
1051.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
import Foundation
func readList() -> [Int] {
return readLine()!.split(separator: " ").map {Int(String($0))!}
}
let args = readList()
let capacity = args[0], num = args[1], queryNum = args[2]
func isValidPop(list: [Int]) -> Bool {
var stack = [Int](), queue = [Int]()
for current in list.reversed() {
while current < stack.last ?? 0 {
queue.append(stack.removeLast())
}
if stack.count < capacity {
stack.append(current)
} else {
return false
}
}
queue += stack.reversed()
var pre = num
for current in queue {
if current > pre {
return false
} else {
pre = current
}
}
return true
}
(0..<queryNum).forEach {_ in
let list = readList()
print(isValidPop(list: list) ? "YES" : "NO")
}