There is a time when you are learning a programming language and you feel like it sets you back. The learning curve to a new language is high and one can reach a point of despair. This was during this period that I had been trying to solve the same problems over and over and with no success. How did I manage to overcome that?
I managed to overcome this despair by stepping back and assessing which part am I missing or which point-of-view was I not considering. I asked my mentor, a senior Googler, how he is able to think quickly for solutions to problems. He pointed out an article about the curve of learning a new language. It’s never straight, it will always be going up and down. He also encouraged me to step back and ask the right questions somewhat like talking to the problem and asking questions as to what the problem wants us to do next.
All that frustration aside, let’s get into the problem. Imagine there are several players and each player gets an array of clouds. 0 is safe while 1 is to be avoided.
Example:
clouds = [0,1,0,0,0,1,0]
The list clouds is indexed from 0 -> 6.
Since we are to avoid the 1’s that means a player’s step would only take indexes 0, 2, 4, 6 or 0, 2, 3, 4, 6. The problem asks us to return the least amount of steps which is 0 -> 2 -> 4 -> 6. With 0 -> 2, 2 -> 4, 4 -> 6 considered as one step; which means that there are a total of 3 steps.
Sample Input:
First line is the total number of clouds; second line are the clouds which is represented as space-separated binary integers.
7
0 0 1 0 0 1 0
Sample Output:
4

Explanation:
Player avoids 2 and 5 indexes as they are 1’s. Four steps is the minimum and fastest way to reach the last cloud.
Here’s my solution:
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the jumpingOnClouds function below.
def jumpingOnClouds(c):
clouds = [i for i,j in enumerate(c) if j == 0]
count = 0
for index in range(len(clouds)-1):
if clouds[index]+1 == clouds[index+1]:
count += 1
else:
count = 0
if count == 2:
clouds[index] = 0
count = 0
return len([i for i in clouds if i != 0])
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()
As one can see, this is not the most optimized solution. It could be better, but this functional solution was able to pass all test cases. This solution was made before I learned about Big O notation.