Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 403 · DP on Array

Frog Jump

LeetCode 403 · ಕಠಿಣ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

State = (stone index, last jump); a hash map turns 'is there a stone at x?' into O(1).

ಕಠಿಣArrayDynamic Programming
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Frog Jump (LeetCode 403) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — DP on Array ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಕಠಿಣ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

Where the frog can go next depends on where it is and how far it jumped last — that pair is the state. From (stone, k) it can try jumps of k-1, k and k+1 and land only if a stone exists at that position. Memoise the pair and see if any path reaches the last stone.

Approach

  1. 1

    Map each stone position to its index.

  2. 2

    dfs(index, prev_jump): if at the last stone, succeed.

  3. 3

    Try next_jump in {prev_jump-1, prev_jump, prev_jump+1}, keeping only positive jumps.

  4. 4

    If a stone exists at stones[index] + next_jump, recurse there.

  5. 5

    Tabulation version marks reachable (index, jump) pairs forward.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ · ಮೊದಲ ಆಲೋಚನೆಯಿಂದ ಅಂತಿಮ ಪರಿಹಾರದವರೆಗೆ

1
Recursion + memoO(n²) time · O(n²) space

lru_cache dfs(index, prev_jump).

2
Forward tabulationAcceptedO(n²) time · O(n²) space

Mark reachable (index, jump) states.

Recursion + memoPythonO(n²) time · O(n²) space
from functools import lru_cache
class Solution:
    def canCross(self, stones: List[int]) -> bool:
        #store/hash the index position of stones in a map
        mpp={}
        for i in range(len(stones)):
            mpp[stones[i]]=i
        @lru_cache(maxsize=None)
        def dfs(index,prev_jump):
            if index == len(stones)-1:
                return True
            ks = [-1,0,+1]
            ans = False
            for k in ks:
                next_jump = prev_jump+k
                if next_jump>0: #next jump must be positive
                    next_step = stones[index]+next_jump #stone at current position + next jump i.e. 5+3 -> 8
                    next_index = mpp.get(next_step) #8 exists on which index/which index we can travel
                    if next_index is not None: #it should be a valid index
                        ans = ans or dfs(next_index,next_jump) #check all possiblities 
            return ans
        return dfs(0,0)
Forward tabulationPythonO(n²) time · O(n²) space
class Solution:
    def canCross(self, stones: List[int]) -> bool:
        #store/hash the index position of stones in a map
        mpp={}
        for i in range(len(stones)):
            mpp[stones[i]]=i
        n = len(stones)
        dp=[[False]*n for _ in range(n)]
        dp[0][0]=1
        for index in range(n):
            for prev_jump in range(n):
                if dp[index][prev_jump]==1:
                    ks = [-1,0,+1]
                    for k in ks:
                        next_jump = prev_jump+k
                        if next_jump>0: #next jump must be positive
                            next_step = stones[index]+next_jump #stone at current position + next jump i.e. 5+3 -> 8
                            next_index = mpp.get(next_step) #8 exists on which index/which index we can travel
                            if next_index is not None: #it should be a valid index
                                dp[next_index][next_jump]=1
                    
        for prev_jump in range(n):
            if dp[n-1][prev_jump]==1:
                return True
        return False
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Frog Jump — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Frog Jump ಹಿಂದಿನ intuition ಏನು?

Where the frog can go next depends on where it is and how far it jumped last — that pair is the state. From (stone, k) it can try jumps of k-1, k and k+1 and land only if a stone exists at that position. Memoise the pair and see if any path reaches the last stone.

Frog Jump (LeetCode 403) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. Map each stone position to its index. 2. dfs(index, prev_jump): if at the last stone, succeed. 3. Try next_jump in {prev_jump-1, prev_jump, prev_jump+1}, keeping only positive jumps. 4. If a stone exists at stones[index] + next_jump, recurse there. 5. Tabulation version marks reachable (index, jump) pairs forward.

Frog Jump ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(n²). Space: O(n²). (Accepted approach: Forward tabulation.)

Frog Jump ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

The first jump must be exactly 1 — starting with prev_jump = 0 enforces it. Zero-length jumps are not allowed.

Frog Jump ಅನ್ನು ಪರಿಹರಿಸಲು ಒಂದಕ್ಕಿಂತ ಹೆಚ್ಚು ಮಾರ್ಗಗಳಿವೆಯೇ?

Recursion + memo (O(n²) time · O(n²) space): lru_cache dfs(index, prev_jump). Forward tabulation (O(n²) time · O(n²) space): Mark reachable (index, jump) states.