So I'm stuck at trying to optimize this LeetCode problem called Frog Jump. Here's the basic description of the problem:
Given a list of stones' positions (in units) in sorted ascending order, determine if the frog is able to cross the river by landing on the last stone. Initially, the frog is on the first stone and assume the first jump must be 1 unit.
If the frog's last jump was k units, then its next jump must be either k - 1, k, or k + 1 units. Note that the frog can only jump in the forward direction.
e.g: [0,1,3,5,6,8,12,17]
There are a total of 8 stones. The first stone at the 0th unit, second stone at the 1st unit, third stone at the 3rd unit, and so on... The last stone at the 17th unit.
Return true. The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone.
Here's my solution which works. What I need help with is how to output the same boolean without allocating the extra res array which stores the logical OR of all the explored paths using DFS.
class Solution:
def canCross(self, stones: List[int]) -> bool:
if stones[1] != 1:
return False
res = []
memo = {}
def dfs(curr_stone, last_stone, last_jump):
if curr_stone == last_stone:
res.append(True)
return
if curr_stone > last_stone:
res.append(False)
return
for i in range(-1,2):
next_stone = curr_stone + (last_jump + i)
if next_stone in stones and next_stone > curr_stone and (next_stone, last_stone, last_jump+i) not in memo:
memo[(next_stone, last_stone, last_jump+i)] = 1
dfs(next_stone, last_stone, last_jump+i)
dfs(1, stones[-1], 1)
return any(res)
Can someone help me with how to approach these questions? I always struggle a lot with these sort of questions and end up storing values in an array; however, ideally I would like the result of the recursive code to be the same without allocating the extra res array space.
Since the entire purpose of the function seems to boil down to return any(res)
, it seems like you should return True
/False
from the recursive function instead of appending them, then exit from all recursive calls once a single True
value is found, and not bother saving every found value.
That will involve checking what was returned from the recursive call dfs(next_stone, last_stone, last_jump+i)
, and if it's true, simply returning that True
:
from typing import List
class Solution:
def canCross(self, stones: List[int]) -> bool:
if stones[1] != 1:
return False
memo = {}
def dfs(curr_stone, last_stone, last_jump):
if curr_stone == last_stone:
return True # Return the results instead of appending them to a list
if curr_stone > last_stone:
return False
for i in range(-1, 2):
next_stone = curr_stone + (last_jump + i)
if next_stone in stones and next_stone > curr_stone and (next_stone, last_stone, last_jump + i) not in memo:
memo[(next_stone, last_stone, last_jump + i)] = 1
rec_result = dfs(next_stone, last_stone, last_jump + i)
if rec_result: # Then check the recursive results at the call-site
return True
return dfs(1, stones[-1], 1)
I'll note, I haven't done extensive testing on this, but from some quick "head interpretation", it seems to be equivalent.