ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 1345. Jump Game IV

2022-07-03 08:32:03  阅读:222  来源: 互联网

标签:index arr cur int IV Jump Game que visited


原题链接在这里: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.

标签:index,arr,cur,int,IV,Jump,Game,que,visited
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/16439188.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有