Monday, December 26, 2022

[LeetCode] Jump Game IV

Problem: 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:

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.
Input: arr = [7]
Output: 0
Explanation: Start index is the last index. You do not need to jump.
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.


Approach: This is straight forward BFS problem in a given graph where nodes (indices) are connected using the conditions given in the problem statement. We can optimize it using HashMap where we can maintain for value to indices mapping so that we don't have to traverse the array every time.


Implementation in C#:

    public int MinJumps(int[] arr)
    {
        int length = arr?.Length ?? 0;
        if (length <= 1)
        {
            return 0;
        }
        if (length == 2)
        {
            return 1;
        }
        var valToIndexMap = this.GetValToIndexMap(arr);
        bool[] visited = new bool[length];
        Queue<int> queue = new Queue<int>();
        queue.Enqueue(0);
        valToIndexMap[arr[0]].Remove(0);
        visited[0] = true;
        int steps = 0;
        while (queue.Count > 0)
        {
            int size = queue.Count;
           
            for (int i = 0; i < size; ++i)
            {
                int currIndex = queue.Dequeue();
                if (currIndex == length - 1)
                {
                    return steps;
                }
                foreach (int index in valToIndexMap[arr[currIndex]])
                {
                    visited[index] = true;
                    queue.Enqueue(index);
                }
                valToIndexMap[arr[currIndex]].Clear();             
                this.EnqueueValidAndUnvisitedIndex(
                                            queue,
                                            currIndex - 1,
                                            visited,
                                            arr,
                                            valToIndexMap);
                this.EnqueueValidAndUnvisitedIndex(
                                            queue,
                                            currIndex + 1,
                                            visited,
                                            arr,
                                            valToIndexMap);
            }
            ++steps;
        }
        return -1;
    }

    private void EnqueueValidAndUnvisitedIndex(
                                Queue<int> queue,
                                int index,
                                bool[] visited,
                                int[] arr,
                                Dictionary<int, HashSet<int>> valToIndexMap)
    {
        if (isValidAndUnvisitedIndex(index, arr.Length, visited))
        {
            visited[index] = true;
            valToIndexMap[arr[index]].Remove(index);
            queue.Enqueue(index);
        }
    }

    private bool isValidAndUnvisitedIndex(int index, int length, bool[] visited)
    {
        return index >= 0 &&
            index < length &&
            !visited[index];
    }

    private Dictionary<int, HashSet<int>> GetValToIndexMap(int[] arr)
    {
        var valToIndexMap = new Dictionary<int, HashSet<int>>();
        for (int i = 0; i < arr.Length; ++i)
        {
            if (!valToIndexMap.ContainsKey(arr[i]))
            {
                valToIndexMap[arr[i]] = new HashSet<int>();
            }
            valToIndexMap[arr[i]].Add(i);
        }
        return valToIndexMap;
    }

Complexity: O(n)

No comments:

Post a Comment