๐ฌย ๋ฌธ์
https://app.codility.com/programmers/lessons/7-stacks_and_queues/fish/
๐ฌย ํ์ด
publicfunc solution(_ A :inout[Int], _ B :inout[Int])->Int{varstack:[Int]=[]varfish0Count=0foriin0..<A.count {ifB[i]==1{
stack.append(A[i])}else{if stack.isEmpty {
fish0Count +=1}else{for_in0..<stack.count {letpop= stack.popLast()??-1ifA[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/
๐ฌย ๋ฌธ์ https://app.codility.com/programmers/lessons/7-stacks_and_queues/fish/
๐ฌย ํ์ด์์์๊ฐ: ๋ฌธ์ ์ดํด๋ฅผ ์๋ชปํด์ ๋๋ฌด ์ค๋ ๊ฑธ๋ ธ๋ค.์๊ฐ ๋ณต์ก๋: O(N)ํ๊ฐํ: https://app.codility.com/demo/results/trainingWHWEJW-WG3/