본문 바로가기
Problem Solving/Codility

[Python] 7.2. Fish

by 부르르 2021. 3. 29.

app.codility.com/demo/results/trainingWVD5AJ-4CG/

 

Test results - Codility

You are given two non-empty arrays A and B consisting of N integers. Arrays A and B represent N voracious fish in a river, ordered downstream along the flow of the river. The fish are numbered from 0 to N − 1. If P and Q are two fish and P < Q, then fish

app.codility.com


Task Score

100%

Correctness

100%

Performance

100%

Detected time complexity

O(N)

def solution(A, B):
    
    stack = []
    cnt = 0

    for i in range(len(A)):
        if B[i] == 1:
            stack.append(A[i])
        else:
            while stack:
                f = stack[-1]
                if f > A[i]:
                    break
                else:
                    stack.pop()
            if not stack:
                cnt += 1
    
    return cnt + len(stack)

 

문제를 푸는데 도움이 된 테스트 케이스

 

 

728x90
반응형

'Problem Solving > Codility' 카테고리의 다른 글

[Python] 8.1. Dominator  (0) 2021.04.05
[Python] 7.3. Nesting  (0) 2021.03.30
[Python] 7.1. Brackets  (0) 2021.03.27
[Python] 6.4. Triangle  (0) 2021.03.26
[Python] 6.3. NumberOfDiscIntersections  (0) 2021.03.25

댓글