DEV Community

Wenqi Jiang
Wenqi Jiang

Posted on

1345. Jump Game IV

Description

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.
Enter fullscreen mode Exit fullscreen mode

Example 2:

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

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.
Enter fullscreen mode Exit fullscreen mode

Constraints:

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

Solutions

Solution 1

Intuition

BFS

Put the indexes that can be reached in each step into the queue.

100,-23,-23,404,100,23,23,23,3,404
0, 1, 2, 3, 4, 5, 6, 7, 8, 9

  • 0 step: 0
  • 1 step: 1, 4
  • 2 step: 2, 3, 5
  • 3 step: 9(over)

Code

class Solution {
    public int minJumps(int[] arr) {
        int n = arr.length;
        if (n == 1) {
            return 0;
        }
        // number with indexes
        Map<Integer, List<Integer>> map = new HashMap<>();

        for (int i = 0; i < n; i++) {
            List<Integer> list = map.getOrDefault(arr[i], new ArrayList<>());
            list.add(i);
            map.put(arr[i], list);
        }

        // bfs
        Queue<Integer> queue = new LinkedList<>();
        // start at index 0
        queue.offer(0);
        // init steps with 0
        int steps = 0;
        while (!queue.isEmpty()) {
            steps++;
            int size = queue.size();
            for (int i = 0; i < size; i++) {

                int index = queue.poll();
                int number = arr[index];

                // left index
                if (index - 1 >= 0 && map.containsKey(arr[index - 1])) {
                    queue.offer(index - 1);
                }
                // right index
                if (index + 1 <= n - 1 && map.containsKey(arr[index + 1])) {
                    if (index + 1 == n - 1) {
                        return steps;
                    }
                    queue.offer(index + 1);
                }
                // other indexes with same number
                if (map.containsKey(number)) {
                    for (int sameNumberIndex : map.get(number)) {
                        if (sameNumberIndex != index) {
                            if (sameNumberIndex == n - 1) {
                                return steps;
                            }
                            queue.offer(sameNumberIndex);
                        }
                    }
                }
                map.remove(number);
            }

        }
        return steps;

    }
}
Enter fullscreen mode Exit fullscreen mode

Complexity

  • Time: O(n)
  • Space: O(n)

Discussion (0)