[Leetcode Learning-java] Jump Game IV

problem:

Difficulty: hard

Description:

The title gives an array, starting from the beginning of the array, to the end of the array, how many steps are required to return the minimum.

Each step can only operate:

1. One step forward i + 1 <arr.length;

2. One step forward i-0 >= 0

3. Jump to another j position where arr[i] == arr[j]

Subject link: https://leetcode.com/problems/jump-game-iv/

Related algorithms:

[Leetcode学习-java]Jump Game I~III :

https://blog.csdn.net/qq_28033719/article/details/110378502

Input range:

  • 1 <= arr.length <= 5 * 10^4
  • -10^8 <= arr[i] <= 10^8

Enter the case:

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 don't 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.

Example 4:
Input: arr = [6,1,9]
Output: 2

Example 5:
Input: arr = [11,22,7,7,7,7,7,7,7,22,13]
Output: 3
 

My code:

In fact, if this question is not up to hrad difficulty, just use bfs, but I just can't figure it out. I thought of a full array, and then dfs actually thought of dp and then went into chaos.

To be reasonable, JumpGame II is a deep question, but II is just medium

Jump Game II :https://blog.csdn.net/qq_28033719/article/details/110378502

Just use bfs to put all possible points in the next step into the queue, and they cannot be the points that have been visited. In addition, the points that have been visited in the previous step will not exist in the next step, because bfs has characteristics and is of the same order. The node will not visit another node, and according to the number of steps taken, it is true that all the current possible step number state sets should not exist in the next state set.

class Solution {
    public int minJumps(int[] arr) {
        int len = arr.length, begin = 0, end = 1, count = 0;
        Map<Integer, List<Integer>> cache = new HashMap<>();
        for(int i = 0; i < len; i ++) {
            cache.putIfAbsent(arr[i], new ArrayList<Integer>());
            cache.get(arr[i]).add(i);
        }
        int[] queue = new int[len]; // 用队列 bfs
        boolean visited[] = new boolean[len]; // bfs 特征, 一个节点不会被其他同阶节点访问
        visited[0] = true;
        while(begin < end) {
            int e = end;
            for(;begin < e;begin ++) {
                int temp = queue[begin], bTemp = temp - 1, nTemp = temp + 1;
                if(temp == len - 1) return count;
                if(cache.containsKey(arr[temp])) // 添加了节点下一阶就移除
                    for(Integer index : cache.get(arr[temp])) 
                        if(!visited[index] && index != temp) {
                             queue[end ++] = index;
                            visited[index] = true;
                        }
                cache.remove(arr[temp]);
                if(bTemp > 0 && !visited[bTemp]) { // 加入 -1 节点
                    queue[end ++] = bTemp;
                    visited[bTemp] = true;
                }
                if(nTemp < len && !visited[nTemp]) { // 加入 +1 节点
                    queue[end ++] = nTemp;
                    visited[nTemp] = true;
                }
            }
            count ++;
        }
        return -1;
    } 
}

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_28033719/article/details/111849840