Description Link to heading

403. Frog Jump (Hard)

A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist 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: 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.

Example 2:

Input: stones = [0,1,2,3,4,8,9,11]
Output: false
Explanation: There is no way to jump to the last stone as the gap between the 5th and 6th stone is
too large.

Constraints:

  • 2 <= stones.length <= 2000
  • 0 <= stones[i] <= 2³¹ - 1
  • stones[0] == 0
  • stones is sorted in a strictly increasing order.

Solution Link to heading

We use the resutl of dfs(i, k) to indicate whether the frog can arrived at the last stone after jumping k steps from the ith stone:

  • if it can’t arrived at another stone by jumping k steps from the ith stone, return false;
  • otherwise, we denote the index of new stone reached by jumping k steps from the ithe stone as new_idx, if the frog can reach the last stone by jumping k - 1 or k or k + 1 steps, then the result of dfs(i, k) is true.

Boundary condition: if (idx == stones.size() - 1) return true;, andk must not be 0, or return false;

dynamic programming Link to heading

We denote dp[i][k] as whether the ith can be reached by jumping k steps from the pre_idxth stone(pre_idx = ump[stones[i] - k]), the corresponding staet transfer equation is: dp[i] = dp[pre_idx][k] || dp[pre_idx][k - 1] || dp[pre_idx][k + 1];

dp[0][0] = true;

bfs Link to heading

It’s actually another form of memorized search. The visited array is vector<vector<bool>> visited(stones.size(), vector<bool>(stones.size() + 1, false));

Note update visited when pushing pair into q.

Code Link to heading

memorized search Link to heading

class Solution {
  public:
    bool dfs(int start_idx, int mv_step, vector<int> &stones, unordered_map<int, int> &ump, vector<vector<int>> &cache) {
        if (start_idx == stones.size() - 1) {
            return true; 
        }
        if (mv_step <= 0) {
            return false;
        }
        if (ump.find(stones[start_idx] + mv_step) != ump.end()) {
            if (cache[start_idx][mv_step] > -1)
                return cache[start_idx][mv_step];
            int new_idx = ump[stones[start_idx] + mv_step];
            cache[start_idx][mv_step] = dfs(new_idx, mv_step - 1, stones, ump, cache) || dfs(new_idx, mv_step, stones, ump, cache) || dfs(new_idx, mv_step + 1, stones, ump, cache);
            return cache[start_idx][mv_step];
        }
        return false;
    }
    bool canCross(vector<int> &stones) {
        unordered_map<int, int> ump;
        for (int i = 0; i < stones.size(); ++i) {
            ump[stones[i]] = i;
        }
        vector<vector<int>> cache(stones.size(), vector<int>(stones.size() + 1, -1));
        return dfs(0, 1, stones, ump, cache);
    }
};

dynamic programming Link to heading

class Solution {
  public:
    bool canCross(vector<int> &stones) {
        unordered_map<int, int> ump;
        for (int i = 0; i < stones.size(); ++i) {
            ump[stones[i]] = i;
        }
        vector<vector<bool>> dp(stones.size(), vector<bool>(stones.size() + 1, false));
        dp[0][0] = true;
        for (int i = 1; i < stones.size(); ++i) {
            for (int k = 1; k <= i; ++k) {
                if (ump.find(stones[i] - k) != ump.end()) {
                    int pre_idx = ump[stones[i] - k];
                    dp[i][k] = dp[pre_idx][k] || dp[pre_idx][k - 1] || dp[pre_idx][k + 1];
                }
            }
        }
        for (int k = 1; k < stones.size(); ++k) {
            if (dp[stones.size() - 1][k]) {
                return true;
            }
        }
        return false;
    }
};

bfs Link to heading

class Solution {
  public:
    bool canCross(vector<int> &stones) {
        if (stones[1] > 1) {
            return false;
        }
        vector<vector<bool>> visited(stones.size(), vector<bool>(stones.size() + 1, false));
        visited[1][1] = true;
        unordered_map<int, int> ump;
        for (int i = 0; i < stones.size(); ++i) {
            ump[stones[i]] = i;
        }
        queue<pair<int, int>> q;
        q.push({1, 1});
        while (!q.empty()) {
            auto [idx, mv_step] = q.front();
            q.pop();
            if (idx == stones.size() - 1)
                return true;
            for (int i = mv_step + 1; i > 0 && i >= mv_step - 1; --i) {
                if (ump.find(stones[idx] + i) != ump.end()) {
                    int new_idx = ump[stones[idx] + i];
                    if (visited[new_idx][i] == false) {
                        visited[new_idx][i] = true;
                        q.push({new_idx, i});
                    }
                }
            }
        }
        return false;
    }
};