1306. Jump Game III

Posted wentiliangkaihua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1306. Jump Game III相关的知识,希望对你有一定的参考价值。

Given an array of non-negative integers arr, you are initially positioned at start index of the array. When you are at index i, you can jump to i + arr[i] or i - arr[i], check if you can reach to any index with value 0.

Notice that you can not jump outside of the array at any time.

 

Example 1:

Input: arr = [4,2,3,0,3,1,2], start = 5
Output: true
Explanation: 
All possible ways to reach at index 3 with value 0 are: 
index 5 -> index 4 -> index 1 -> index 3 
index 5 -> index 6 -> index 4 -> index 1 -> index 3 

Example 2:

Input: arr = [4,2,3,0,3,1,2], start = 0
Output: true 
Explanation: 
One possible way to reach at index 3 with value 0 is: 
index 0 -> index 4 -> index 1 -> index 3

Example 3:

Input: arr = [3,0,2,1,2], start = 2
Output: false
Explanation: There is no way to reach at index 1 with value 0.

 

Constraints:

  • 1 <= arr.length <= 5 * 10^4
  • 0 <= arr[i] < arr.length
  • 0 <= start < arr.length
class Solution {
public static boolean canReach(int[] arr, int start) {
        int n = arr.length;
        HashSet<Integer> visited = new HashSet<>(); // visited set
        Queue<Integer> q = new LinkedList<>();
        q.add(start);
        while (!q.isEmpty()) {
            int i = q.poll();
            if (arr[i] == 0) return true; // found then return it
            if (visited.contains(i)) continue; // already visited than continue
            visited.add(i);
            if (i + arr[i] < n)
                q.add(i + arr[i]);
            if (i - arr[i] >= 0)
                q.add(i - arr[i]);
        }
        return false;// not found
    }
}

用queue,很巧妙

class Solution {
 public boolean canReach(int[] arr, int start) {
        if(start<0 || start>=arr.length || arr[start]<0) return false;
        if(arr[start] == 0) return true;

        arr[start] = -arr[start];
        return canReach(arr, start + arr[start]) || canReach(arr, start - arr[start]);
    }
}

DFS也可

https://leetcode.com/problems/jump-game-iii/discuss/473221/Simple-Java-DFS-solution

class Solution {
    public boolean canReach(int[] arr, int start) {
        
        return travel(arr, start, new boolean[arr.length]);
    }
    
    private boolean travel(int[] arr, int index, boolean[] visited)
    {  
        //Out of bounds check, found solution already, or visited, get out
        if(index < 0 || index >= arr.length || visited[index])
            return false;
         
        visited[index]=true;
        
        if(arr[index] == 0)
            return true;
        
        return travel(arr, index + arr[index], visited) || travel(arr, index - arr[index], visited);
        
    }
}

https://leetcode.com/problems/jump-game-iii/discuss/482295/Java-Simple-DFS-0(N)-Space-O(N)-time

以上是关于1306. Jump Game III的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 5297. 跳跃游戏 III Jump Game III

Jump Game II

55 Jump Game i && 45 Jump Game ii

来见识一下贪心算法:Jump Game

Jump Game II

LeetCode 55. Jump Game