我是靠谱客的博主 结实小鸭子,这篇文章主要介绍LeetCode题解(0403):青蛙过河(Python),现在分享给大家,希望可以做个参考。

题目:原题链接(困难)

标签:动态规划

解法时间复杂度空间复杂度执行用时
Ans 1 (Python) O ( N ) O(N) O(N) O ( N ) O(N) O(N)204ms (71.62%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:
    def canCross(self, stones: List[int]) -> bool:
        size = len(stones)
        index = {stone: i for i, stone in enumerate(stones)}

        if stones[1] - stones[0] != 1:
            return False

        dp = [set() for _ in range(size)]

        dp[1].add(stones[1] - stones[0])

        for i in range(1, size):
            stone = stones[i]
            for step in dp[i]:
                if stone + step + 1 in index:
                    dp[index[stone + step + 1]].add(step + 1)
                if stone + step in index:
                    dp[index[stone + step]].add(step)
                if step > 1 and stone + step - 1 in index:
                    dp[index[stone + step - 1]].add(step - 1)

        return len(dp[-1]) > 0

最后

以上就是结实小鸭子最近收集整理的关于LeetCode题解(0403):青蛙过河(Python)的全部内容,更多相关LeetCode题解(0403)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部