Jumping on the Clouds
--
Problem statement:-
Solution:-Simple but not so straight forward hackerrank problem where we have to count the number of jumps based on which type of cloud is allowed to jump on it or not in that case the jump range will increase.
Let move towards the code,we declare two variables “i” and “jump” and give them an initial value of 0.We will iterate the cloud list using a while loop which iterate till (length of cloud list-1) as we need to iterate each and every cloud type in cloud list.We will compare the i+1 and i+2 cloud in the list for current iteration and based on that we will jump by 2 jumps or 1 jump.
def jumpingOnClouds(c):
jump,i=0,0
while i< n-1:
if i+2< n and c[i+2]==0:
i+=2
jump+=1
elif i+1< n and c[i+1]==0:
i+=1
jump+=1
return jump
along with that in if loop we will always check if the cloud list has not been exhausted i.e i+2<n or i+1<n. We will increment the number of jumps and value of i in each iteration based on jump count.
Conclusion:-
“Try and Fail but don’t Fail to Try”.
An excellent quote for this problem as I have been struggling to optimize my solution for this problem. Keep learning and Keep hustling…(cheers:-PN)