
Problem
Solution
Submissions
Frog Jump Problem
Certification: Advanced Level
Accuracy: 0%
Submissions: 0
Points: 15
Write a C++ program to determine if a frog can cross a river by landing on the last stone. The frog can only jump forward, and if its last jump was k units, its next jump must be either k-1, k, or k+1 units.
Example 1
- Input: stones = [0,1,3,4,5,7,9,10,12]
- Output: true
- Explanation:
- The frog can cross the river by jumping on the stones in this way:
- Jump from stone 0 to stone 1 with a jump of 1 unit.
- Jump from stone 1 to stone 3 with a jump of 2 units (previous jump was 1, so next jump can be 0, 1, or 2).
- Jump from stone 3 to stone 4 with a jump of 1 unit (previous jump was 2, so next jump can be 1, 2, or 3).
- Jump from stone 4 to stone 5 with a jump of 1 unit (previous jump was 1, so next jump can be 0, 1, or 2).
- Jump from stone 5 to stone 7 with a jump of 2 units (previous jump was 1, so next jump can be 0, 1, or 2).
- Jump from stone 7 to stone 9 with a jump of 2 units (previous jump was 2, so next jump can be 1, 2, or 3).
- Jump from stone 9 to stone 10 with a jump of 1 unit (previous jump was 2, so next jump can be 1, 2, or 3).
- Jump from stone 10 to stone 12 with a jump of 2 units (previous jump was 1, so next jump can be 0, 1, or 2).
Example 2
- Input: stones = [0,1,2,3,4,8,9,11]
- Output: false
- Explanation:
- The frog can't reach the last stone because the gap between position 4 and position 8 is too large.
- There's no way for the frog to jump 4, 5, or 6 units to reach position 8.
Constraints
- 2 ≤ stones.length ≤ 2000
- 0 ≤ stones[i] ≤ 2^31 - 1
- stones[0] == 0
- stones is sorted in 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 keep track of possible jumps at each stone.
- Consider using a hash map for efficient lookup of stone positions.
- Use recursion with memoization to avoid recalculating the same states.
- For each position, check if it's reachable using valid jump sizes.
- Consider the constraints on jump sizes based on the previous jump.