• + 0 comments

    Python

    This solutions separates constants and variables, allowing for faster execution. The max_index and size are constants, allowing us to only measure the length of the array once. For the solution to be optimal, we need to try to jump the furthest we can. Since the question emphasizes that there's always a possible solution, this means that in case there's a dangerous cloud in position + 2 we can always jump to position + 1 and this will be the minimum number of jumps.

    def jumpingOnClouds(clouds: list[int])-> int:
        position: int = 0
        jumps: int = 0
        size: int = len(clouds)
        max_index: int = size -1
        
        while position < max_index:
            if(
                position + 2 > max_index or
                clouds[position + 2] == 1
            ):
                position += 1
            else:
                position += 2
            jumps += 1
        
        return jumps