Skip to content

[Algorithm] Fishย #147

@hwangJi-dev

Description

@hwangJi-dev

๐Ÿ’ฌย ๋ฌธ์ œ

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/

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions