HackerRank World Codesprint April: Jumping on the Clouds

,

https://www.hackerrank.com/contests/world-codesprint-april/challenges/jumping-on-the-clouds

solution

Simulate it greedily. $O(N)$.

If you can jump to $i+2$-th cloud, then do it, else jump to $i+1$-th one.

implementation

#!/usr/bin/env python3
n = int(input())
a = list(map(int,input().split()))
ans = 0
i = 0
while i < n-1:
    i = min(i+2, n-1)
    if a[i] == 1:
        i -= 1
    ans += 1
print(ans)