본문 바로가기

알고리즘/해커랭크

Jumping on the Clouds

문제

https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem?h_l=interview&playlist_slugs%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D%5B%5D=warmup

 

thunderheads를 피해서 점프하기

 

입력 리스트가 주어졌을 때 1, 2만큼 이동가능하고 0만 밟아서 최소 점프로 배열의 끝에 도착해야 함

 

입력 예

7

0 0 1 0 0 1 0

 

출력 예

4

 

제출한 코드

def jumpingOnClouds(c):
    idx,cnt = 0,0

    while idx<len(c)-1:
        if idx+2>len(c)-1:
            cnt+=1
            return cnt
        if c[idx+2]==1:
            idx+=1
        else:
            idx+=2
        cnt+=1           
    return cnt

인덱스와 카운트 변수 설정

인덱스 범위까지 루프

최소 점프가 목적이기 때문에 2만큼 점프했을 때 thunderhead(1)이면 1만 점프함(idx+=1)

마지막에 2만큼 점프했을 때 범위를 벗어나면 1만 점프하고 점프한 횟수 출력

'알고리즘 > 해커랭크' 카테고리의 다른 글

Frequency Queries  (0) 2020.03.30
Two Strings  (0) 2020.03.28
New Year Chaos  (0) 2020.03.16
Arrays: Left Rotation  (0) 2020.03.16
Cats and a Mouse  (0) 2020.03.16