Frog Jump LeetCode solution in Python
1# Runtime: 231 ms (Top 72.60%) | Memory: 19.2 MB (Top 61.18%)2class Solution:3def possible(self, i, n, stones, pos, allowedJumps):4if i == n - 1:5return True6key = tuple([i] + allowedJumps)7if key in self.cache:8return self.cache[key]9for jump in allowedJumps:10if jump > 0 and stones[i] + jump in pos:11if self.possible(12pos[stones[i] + jump], n, stones, pos, [jump - 1, jump, jump + 1]13):14self.cache[key] = True15return True16self.cache[key] = False17return False1819def canCross(self, stones: List[int]) -> bool:20n = len(stones)21pos = {}22for i, stone in enumerate(stones):23pos[stone] = i24self.cache = {}25return self.possible(0, n, stones, pos, [1])