-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
๐ฌย ๋ฌธ์
https://app.codility.com/programmers/lessons/7-stacks_and_queues/fish/
๐ฌย ํ์ด
public func solution(_ A : inout [Int], _ B : inout [Int]) -> Int {
var stack: [Int] = []
var fish0Count = 0
for i in 0..<A.count {
if B[i] == 1 {
stack.append(A[i])
} else {
if stack.isEmpty {
fish0Count += 1
} else {
for _ in 0..<stack.count {
let pop = stack.popLast() ?? -1
if A[i] < pop {
stack.append(pop)
break
}
}
if stack.count == 0 {
fish0Count += 1
}
}
}
}
return fish0Count + stack.count
}
์์์๊ฐ
: ๋ฌธ์ ์ดํด๋ฅผ ์๋ชปํด์ ๋๋ฌด ์ค๋ ๊ฑธ๋ ธ๋ค.
์๊ฐ ๋ณต์ก๋
: O(N)
ํ๊ฐํ
: https://app.codility.com/demo/results/trainingWHWEJW-WG3/