Tutorialspoint
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²)
Dynamic Programming AccentureLTIMindtree
Editorial

Login to view the detailed solution and explanation for this problem.

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.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

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.

Steps to solve by this approach:

 Step 1: Create a map of stone positions for quick lookup
 Step 2: Define a dynamic programming structure to track possible jumps at each stone
 Step 3: Initialize with the base case (first stone with 0 jump distance)
 Step 4: Iterate through all stones and generate possible next positions
 Step 5: For each stone, try all valid jump distances (k-1, k, k+1)
 Step 6: Check if the jump lands on a valid stone and update the DP structure
 Step 7: Check if the last stone has any valid jumps to determine if the frog can reach it

Submitted Code :