-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrogJump.py
More file actions
29 lines (26 loc) · 929 Bytes
/
frogJump.py
File metadata and controls
29 lines (26 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/frog-jump/
# Author: Miao Zhang
# Date: 2021-02-08
class Solution:
def canCross(self, stones: List[int]) -> bool:
from collections import defaultdict
dic = defaultdict(bool)
return self.dfs(stones, 0, 0, dic)
def dfs(self, stones: List[int], pos: int, jump: int, dic: defaultdict) -> bool:
n = len(stones)
key = pos | jump << 11
if pos >= n - 1: return True
if key in dic: return dic[key]
for i in range(pos + 1, n):
dist = stones[i] - stones[pos]
if dist < jump - 1: continue
if dist > jump + 1:
dic[key] = False
return dic[key]
if self.dfs(stones, i, dist, dic):
dic[key] = True
return dic[key]
dic[key] = False
return dic[key]