기타/Codility

[Easy] ArrListLen

백곰곰 2022. 5. 22. 15:52
728x90
반응형

[문제]

배열 내 값을 배열 idx로 사용하여 접근하며 '-1'이 있는 배열 idx에 몇번만에 도달하는지 찾는 문제

 

[코드]

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    # write your code in Python 3.6
    answer=1
    i=0
    while True:
        if A[i] == -1:
            break;
        else:
            i = A[i]
            answer+=1
    return answer
    
# PassTheMessage is recommended

 

https://app.codility.com/demo/results/trainingJVBNCC-F9V/

 

Test results - Codility

A non-empty array A consisting of N integers is given. Array A represents a linked list. A list is constructed from this array as follows: the first node (the head) is located at index 0; the value of a node located at index K is A[K]; if the value of a no

app.codility.com

 

728x90

'기타 > Codility' 카테고리의 다른 글

[Easy] Distinct  (0) 2022.05.25
[Easy] PassingCars  (0) 2022.05.24
[Easy] FrogJmp  (0) 2022.05.22
[Easy] CyclicRotation  (0) 2022.05.22
[Easy] BinaryGap  (0) 2022.05.22