403.Frog-Jump

403. Frog Jump

题目地址

https://leetcode.com/problems/frog-jump/

题目描述

A frog is crossing a river. The river is divided into x 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 is able to cross the river by landing on the last stone. Initially, the frog is on the first stone and assume the first jump must be 1 unit.

If the frog's last jump was k units, then its next jump must be either k - 1, k, or k + 1 units. Note that the frog can only jump in the forward direction.

Note:
The number of stones is ≥ 2 and is < 1,100.
Each stone's position will be a non-negative integer < 231.
The first stone's position is always 0.

Example 1:
[0,1,3,5,6,8,12,17]
There are a total of 8 stones.
The first stone at the 0th unit, second stone at the 1st unit,
third stone at the 3rd unit, and so on...
The last stone at the 17th unit.

Return true. 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:
[0,1,2,3,4,8,9,11]
Return false. There is no way to jump to the last stone as 
the gap between the 5th and 6th stone is too large.

代码

Approach #1 DFS + Memorization

Time: O(3^n)

Memo: 记录是否能够跳i,并且跳的距离为jumpsize,的三种状态

  • -1: unvisited

  • 0: visited, false

  • 1: visited, true

public class Solution {
  public boolean canCross(int[] stones) {
    int[][] memo = new int[stones.length][stones.length];
    for (int[] row : memo) {
        Arrays.fill(row, -1);
    }
    return dfs(stones, 0, 0, memo) == 1;
  }

  public int dfs(int[] stones, int ind, int jumpsize, int[][] memo) {
    if (memo[ind][jumpsize] >= 0) {
        return memo[ind][jumpsize];
    }
    for (int i = ind + 1; i < stones.length; i++) {
      int gap = stones[i] - stones[ind];
      if (gap >= jumpsize - 1 && gap <= jumpsize + 1) {
          if (dfs(stones, i, gap, memo) == 1) {
              memo[ind][gap] = 1;
              return 1;
          }
      }
    }
    memo[ind][jumpsize] = (ind == stones.length - 1) ? 1 : 0;
    return memo[ind][jumpsize];
  }
}
class Solution {
  public boolean canCross(int[] stones) {
    int[][] memo = new int[stones.length][stones.length];
    for (int[] row: memo) {
      Arrays.fill(row, -1);
    }
    return dfs(stones, 0, 0, memo) == 1;
  }

  private int dfs(int[] stones, int ind, int jumpsize, int[][] memo) {
    if (memo[ind][jumpsize] >= 0) {
      return memo[ind][jumpsize];
    }
    int ind1 = Arrays.binarySearch(stones, ind + 1, stones.length, stones[ind] + jumpsize);
    if (ind1 >= 0 && dfs(stones, ind1, jumpsize, memo) == 1) {
      memo[ind1][jumpsize] = 1;
      return 1;
    }
    int ind2 = Arrays.binarySearch(stones, ind + 1, stones.length, stones[ind] + jumpsize - 1);
    if (ind2 >= 0 && dfs(stones, ind2, jumpsize - 1, memo)  == 1) {
      memo[ind][jumpsize -1] = 1;
      return 1;
    }
    int ind3 = Arrays.binarySearch(stones, ind + 1, stones.length, stones[ind] + jumpsize + 1);
    if (ind3 >= 0 && dfs(stones, ind3, jumpsize + 1, memo)  == 1) {
      memo[ind][jumpsize + 1] = 1;
      return 1;
    }

    memo[ind][jumpsize] = (ind == stones.length - 1) ? 1: 0;
    return memo[ind][jumpsize];
  }
}

Approach #3 Dynamic Programming

class Solution {
  public boolean canCross(int[] stones) {
    HashMap<Integer, Set<Integer>> map = new HashMap();
    for (int i = 0; i < stones.length; i++) {
      map.put(stones[i], new HashSet<Integer>());
    }
    map.get(0).add(0);
    for (int i = 0; i < stones.length; i++) {
      for (int k : map.get(stones[i])) {
        for (int step = k - 1; step <= k + 1; step++) {
          if (step > 0 && map.containsKey(stones[i] + step)) {
            map.get(stones[i] + step).add(step);
          }
        }
      }
    }
    return map.get(stones[stones.length - 1]).size() > 0
  }
}

Approach #4 BFS

Queue

  • Pair第一个值存储Index

  • Pair第二个值存储jumpsize

下一步的位置 = stones[index] + jumpsize + d

class Solution {
    public boolean canCross(int[] stones) {
        int len = stones.length;
        boolean visited[][] = new boolean[len][len];
        Queue<Pair> queue = new LinkedList<>();
        int[] directions = new int[]{-1, 0, 1};
        queue.offer(new Pair(0, stones[0]));
        visited[0][0] = true;
        while (!queue.isEmpty()) {
            Pair cur = queue.poll();
            for (int d : directions) {
                int step = cur.second + d;
                int dest = stones[cur.first] + step;
                int ind = Arrays.binarySearch(stones, cur.first + 1, len, dest);

                if (ind >= 0 && ind > cur.first) {
                    if (!visited[cur.first][ind]) {
                        if (ind == len - 1) {
                            return true;
                        }
                        visited[cur.first][ind] = true;
                        queue.offer(new Pair(ind, step));
                    }
                }
            }
        }

        return false;
    }
}

class Pair {
    Integer first;
    Integer second;
    Pair(Integer first, Integer second) {
        this.first = first;
        this.second = second;
    }
}

Last updated