LeetCode 1345. Jump Game IV


原题链接在这里:https://leetcode.com/problems/jump-game-iv/

题目:

Given an array of integers arr, you are initially positioned at the first index of the array.

In one step you can jump from index i to index:

  • i + 1 where: i + 1 < arr.length.
  • i - 1 where: i - 1 >= 0.
  • j where: arr[i] == arr[j] and i != j.

Return the minimum number of steps to reach the last index of the array.

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

Example 1:

Input: arr = [100,-23,-23,404,100,23,23,23,3,404]
Output: 3
Explanation: You need three jumps from index 0 --> 4 --> 3 --> 9. Note that index 9 is the last index of the array.

Example 2:

Input: arr = [7]
Output: 0
Explanation: Start index is the last index. You do not need to jump.

Example 3:

Input: arr = [7,6,9,6,9,6,9,7]
Output: 1
Explanation: You can jump directly from index 0 to index 7 which is last index of the array.

Constraints:

  • 1 <= arr.length <= 5 * 104
  • -108 <= arr[i] <= 108

题解:

Perform BFS from index 0. 

When polled index is last index, return level.

For each polled index, check left, right and other indices with same value. If not visited, put into queue.

How to get other indices with same value. The answer is to use a HashMap<Integer, List<Integer>> valToIndices.

Note: In order to same time, after check nexts, nexts.clear(). Otherwide, if there are a lot of 7. e.g.[7, 7, 7 ….. 7, 11]. If we do not clear, then for each polled 7, we need to check the list again. However, these 7s are aleady visited.

Time Complexity: O(n). n = arr.length.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int minJumps(int[] arr) {
 3         if(arr == null || arr.length == 0){
 4             return 0;
 5         }
 6         
 7         int n = arr.length;
 8         HashMap<Integer, List<Integer>> hm = new HashMap<>();
 9         for(int i = 0; i < n; i++){
10             hm.putIfAbsent(arr[i], new ArrayList<Integer>());
11             hm.get(arr[i]).add(i);
12         }
13         
14         boolean [] visited = new boolean[n];
15         LinkedList<Integer> que = new LinkedList<>();
16         que.add(0);
17         visited[0] = true;
18         int res = 0;
19         while(!que.isEmpty()){
20             int size = que.size();
21             while(size-- > 0){
22                 int cur = que.poll();
23                 if(cur == n - 1){
24                     return res;
25                 }
26                 
27                 if(cur - 1 >= 0 && !visited[cur - 1]){
28                     que.add(cur - 1);
29                     visited[cur - 1] = true;
30                 }
31                 
32                 if(cur + 1 < n && !visited[cur + 1]){
33                     que.add(cur + 1);
34                     visited[cur + 1] = true;
35                 }
36                 
37                 List<Integer> nexts = hm.get(arr[cur]);
38                 for(int next : nexts){
39                     if(!visited[next]){
40                         que.add(next);
41                         visited[next] = true;
42                     }
43                 }
44                 
45                 nexts.clear();
46             }
47             
48             res++;
49         }
50         
51         return -1;
52     } 
53 }

类似Jump Game III.

原创文章,作者:wdmbts,如若转载,请注明出处:https://blog.ytso.com/271293.html

(0)
上一篇 2022年7月3日
下一篇 2022年7月3日

相关推荐

发表回复

登录后才能评论