개발자식

[Codility_Lesson 4] FrogRiverOne 본문

Algorithm/Codility

[Codility_Lesson 4] FrogRiverOne

밍츠 2022. 6. 30. 02:52

문제 : FrogRiverOne

 

Test results - Codility

A small frog wants to get to the other side of a river. The frog is initially located on one bank of the river (position 0) and wants to get to the opposite bank (position X+1). Leaves fall from a tree onto the surface of the river. You are given an array

app.codility.com

문제 해석 : 

개구리가 강을 건너기 위해 나뭇잎이 1부터 X까지 (위치)떨어져 있어야 한다.

배열A는 시간(인덱스)에 따라 떨어지는 위치가 담겨있다.

 

나의 코드

- all, any를 이용하여 떨어진 여부를 확인했더니 효율성 tc 2개를 만족시키지 못했다.

 

 

정답 코드

def solution(X, A):
    temp =[0] * (X+1)
    check_sum =0
    for i in range(len(A)):
        if temp[A[i]]==0:
            temp[A[i]]+=1
            check_sum+=1
            if check_sum==X:
                return i
    return -1

- 해당 위치가 나뭇잎이 떨어지지 않은 경우 (즉 해당 temp 값이 0인 경우) 만 chek_sum의 +1 하고 바로 return 할 수 있게 한다.

Comments