
Problem
Solution
Submissions
Frog Jump
Certification: Advanced Level
Accuracy: 100%
Submissions: 1
Points: 12
A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not be a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of stones' positions (in units) in sorted ascending order, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be 1 unit.
If the frog's last jump was k units, its next jump must be either k-1, k, or k+1 units. The frog can only jump in the forward direction.
Example 1
- Input: stones = [0, 1, 3, 5, 6, 8, 12, 17]
- Output: true
- Explanation:
Step 1: The frog starts at position 0 and jumps 1 unit to position 1.
Step 2: It then jumps 2 units (k+1 = 1+1 = 2) to position 3.
Step 3: It then jumps 2 units (k = 2) to position 5.
Step 4: It then jumps 3 units (k+1 = 2+1 = 3) to position 8.
Step 5: It then jumps 4 units (k+1 = 3+1 = 4) to position 12.
Step 6: It then jumps 5 units (k+1 = 4+1 = 5) to position 17, which is the last stone.
Example 2
- Input: stones = [0, 1, 2, 3, 4, 8, 9, 11]
- Output: false
- Explanation:
Step 1: The frog starts at position 0 and jumps 1 unit to position 1.
Step 2: It then jumps 1 unit (k = 1) to position 2.
Step 3: It then jumps 1 unit (k = 1) to position 3.
Step 4: It then jumps 1 unit (k = 1) to position 4.
Step 5: It then needs to jump at least 4 units (k, k+1 = 1, 2) to reach the next stone at position 8.
Step 6: But the maximum jump length is 2 units (k+1 = 1+1 = 2), so it cannot reach position 8.
Constraints
- 2 <= stones.length <= 2000
- 0 <= stones[i] <= 2^31 - 1
- stones[0] = 0
- stones is sorted in a strictly increasing order.
- Time Complexity: O(n²) where n is the number of stones
- Space Complexity: O(n²)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use dynamic programming to track possible jumps at each stone.
- For each stone, store the possible jump distances that can reach it.
- Use a hash map for quick lookup of stone positions.
- At each stone, try the three possible jump distances (k-1, k, k+1).
- Memoize results to avoid redundant calculations.