Problem Description
Given an array of colors (where each color is 1, 2, or 3) and a list of queries, each query provides an index and a target color. The goal is to determine the shortest distance from the query index to any index in the array that contains the target color. If the target color does not exist in the array, return -1.
Key Insights
- Pre-compute the positions for each color by traversing the colors array once.
- Use separate sorted lists for each color to store the indices.
- Binary search can quickly find the nearest index in the sorted list for a given query, reducing the overall processing time.
- This method allows each query to be answered in O(log n) time after an O(n) pre-computation.
Space and Time Complexity
Time Complexity: O(n + m log n), where n is the number of elements in the colors array and m is the number of queries. Space Complexity: O(n), used for storing the indices of each color.
Solution
We precompute positions for each color by iterating over the array and storing indices in separate lists. For each query, we perform a binary search on the respective list of indices:
- If the list is empty, we return -1.
- Otherwise, we determine the insertion point and check its neighbor(s) to find the minimum distance between the query index and the nearest occurrence of the target color. This strategy efficiently handles multiple queries even when the input size is large.