LeetCode 1182: Shortest Distance to Target Color Solution

Master LeetCode problem 1182 (Shortest Distance to Target Color), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1182. Shortest Distance to Target Color

Problem Explanation

Explanation:

Given an array of colors colors and a list of queries queries, we need to find the shortest distance from each query[0] index to the nearest color query[1].

Algorithm:

  1. Initialize a HashMap to store the indices of each color.
  2. For each color in colors, store its indices in the HashMap.
  3. For each query, get the indices of the target color from the HashMap.
  4. For each index in the query's indices, calculate the distance to the query index and update the shortest distance.
  5. Repeat for all queries and return the distances.

Time Complexity:

  • Constructing the HashMap: O(n) where n is the number of colors.
  • Calculating distances for all queries: O(q * c) where q is the number of queries and c is the average number of indices for each color.
  • Overall time complexity: O(n + q * c)

Space Complexity:

  • HashMap to store color indices: O(n) where n is the number of colors.
  • Output array for distances: O(q)
  • Overall space complexity: O(n + q)

: :

Solution Code

class Solution {
    public List<Integer> shortestDistanceColor(int[] colors, int[][] queries) {
        Map<Integer, List<Integer>> colorIndices = new HashMap<>();
        for (int i = 0; i < colors.length; i++) {
            colorIndices.putIfAbsent(colors[i], new ArrayList<>());
            colorIndices.get(colors[i]).add(i);
        }
        
        List<Integer> result = new ArrayList<>();
        for (int[] query : queries) {
            int targetColor = query[1];
            List<Integer> indices = colorIndices.getOrDefault(targetColor, new ArrayList<>());
            int minDistance = Integer.MAX_VALUE;
            for (int index : indices) {
                minDistance = Math.min(minDistance, Math.abs(index - query[0]));
            }
            result.add(minDistance == Integer.MAX_VALUE ? -1 : minDistance);
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1182 (Shortest Distance to Target Color)?

This page provides optimized solutions for LeetCode problem 1182 (Shortest Distance to Target Color) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1182 (Shortest Distance to Target Color)?

The time complexity for LeetCode 1182 (Shortest Distance to Target Color) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1182 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1182 in Java, C++, or Python.

Back to LeetCode Solutions