[LeetCode] 45. Jump Game II

夜歌乘年少發表於2024-07-03

有點意思,需要動態規劃。

from  typing import List
from collections import Counter
import time


class Solution:
    def jump(self, nums: List[int]) -> int:
        max_reachable = 0
        min_steps = 0

        for i, element in enumerate(nums):
            if i > max_reachable:
                return 0
            if max_reachable >= len(nums) -1:
                return min_steps
            if (i + element) > max_reachable:
                max_reachable = i + element
                min_steps += 1
def main():
    start_time = time.time()
    print(Solution().jump([7,0,9,6,9,6,1,7,9,0,1,2,9,0,3]))
    end_time = time.time()
    print("Time taken: {:.6f} seconds".format(end_time - start_time))

if __name__ == '__main__':
    main()


相關文章