▼ Question
--A list containing multiple 0s or 1s is given. --0 is an ordinary cloud. 1 is a thunderstorm. --The index number becomes the cloud number. ――If you follow the clouds and reach the last index number, you will reach the goal. --You can jump +1 or +2 when following the clouds. --Avoid thunderstorms. --Find the shortest number of jumps required to reach the goal (you can always reach the goal)
▼sample input
python
6
0 0 0 0 1 0
▼sample output
python
3
▼my answer
python
def jumpingOnClouds(c):
i= ans = 0
while i <= len(c)-2:
#In the case of one before the final point
if i+2 == len(c):
ans += 1
return ans
#Is it possible to skip two?
elif c[i+2] != 1:
i += 2
else:
i += 1
ans += 1
return ans
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
c = list(map(int, input().rstrip().split()))
result = jumpingOnClouds(c)
fptr.write(str(result) + '\n')
fptr.close()
** ・ len (c) ** Find the number of elements. ※1 or more c [len (c)] does not exist.
** ・ "!" ** ! Comes in front ☓ a =! 3 ◯ a != 3
Recommended Posts