python 3.x – This is Crossing river code

Niñez friends from Kakao Elementary School meet a stream with stepping stones on an autumn picnic with Ms. Ryan and are about to cross over to the other side. Ms. Ryan made the following rules so that his Ninies friends can safely cross the stepping stone.Stepping stones are placed in a row, and the number of stepping stones is written on each stepping stone, and the number of stepping stones decreases by one for each step. When the number of stepping stones reaches 0, you can no longer step on them, and in this case, you can skip several spaces at once with the next stepping stone. However, if there are multiple stepping stones that can be stepped on next, you can only skip to the nearest stepping stone.The Niñez friends are on the left side of the stream, and they must arrive across the right side of the stream to be counted as crossing the stepping stone. The Niese friends must cross the stepping bridge one at a time, and after one friend crosses all the stepping stones, the next friend begins to cross.

Complete the solution function to return how many people can cross the stepping stone, given an array of stones containing the number written on the stepping stones in order and the maximum number of steps k that can be skipped at a time as a parameter.


1.The number of Ninez friends who must cross the stepping stone is considered to be unlimited.

2.The stones array has a size of 1 to 200,000.

3.The value of each element in the stones array is a natural number between 1 and 200,000,000.
k is a natural number greater than or equal to 1 and less than or equal to the length of the stones.

How should i revise my code? this is kakao intern code test.I used loop to change stone numbers when stepped and repeat many times . It returns how many people could cross the river.

I/O example

stones=(2, 4, 5, 3, 2, 1, 4, 2, 5, 1) k=3

def solution(stones, k):
    for count in range(max(stones)):
        for i in range(num):
            if stat==i-1:
                if stones(i)==0:
                    for j in range(i+1,num):
                        if stones(j)==0:
                            if j-stat>=k:
                                return answer
                        elif stones(j)!=0:
                elif stones(i)!=0:
        if stat+k>=num:
        elif stat+k<num:
            return answer
    return answer