Shortest Distance to Target Color - Problem

You're working with a colorful array where each position contains one of three colors: 1 (red), 2 (green), or 3 (blue). Your task is to efficiently answer multiple queries about finding the shortest distance from any given position to the nearest occurrence of a specific target color.

For each query (i, c), you need to find the minimum number of steps required to reach color c from index i. If the target color doesn't exist in the array, return -1.

Example: Given colors = [1,1,2,1,3,2,2,3,3], if you're at index 3 (color 1) and looking for color 2, you can go left to index 2 (distance = 1) or right to index 5 (distance = 2), so the answer is 1.

Input & Output

example_1.py — Basic Case
$ Input: colors = [1,1,2,1,3,2,2,3,3], queries = [[1,3],[2,2],[6,1]]
Output: [3, 0, 3]
💡 Note: Query [1,3]: At index 1 (color 1), nearest color 3 is at index 4, distance = |4-1| = 3. Query [2,2]: At index 2 (color 2), we're already at color 2, distance = 0. Query [6,1]: At index 6 (color 2), nearest color 1 is at index 3, distance = |6-3| = 3.
example_2.py — Color Not Found
$ Input: colors = [1,2], queries = [[0,3]]
Output: [-1]
💡 Note: Query [0,3]: At index 0, we're looking for color 3, but color 3 doesn't exist in the array, so we return -1.
example_3.py — Multiple Queries Same Position
$ Input: colors = [1,2,3,1,2], queries = [[2,1],[2,2],[2,3]]
Output: [1, 1, 0]
💡 Note: All queries start at index 2 (color 3). For color 1: nearest is at index 3, distance = 1. For color 2: nearest is at index 1, distance = 1. For color 3: we're already at color 3, distance = 0.

Constraints

  • 1 ≤ colors.length ≤ 5 × 104
  • 1 ≤ colors[i] ≤ 3
  • 1 ≤ queries.length ≤ 5 × 104
  • 0 ≤ queries[i][0] < colors.length
  • 1 ≤ queries[i][1] ≤ 3

Visualization

Tap to expand
🎨 Color Distance Problem VisualizationArt Palette:RedGreenBlueRedGreenDistance to Green (Color 2):10110Query Example:🔍 Query: Position 2, Color Green📍 Current: Blue at position 2✅ Answer: Distance = 1 (nearest green)DP Algorithm:1️⃣ Forward pass: scan left→right2️⃣ Backward pass: scan right←left3️⃣ Take minimum of both directions⚡ Result: O(1) query time!
Understanding the Visualization
1
Build Distance Map
Create a map showing distances to each color from every position
2
Forward Propagation
Scan left-to-right, updating distances based on colors seen so far
3
Backward Propagation
Scan right-to-left, finding closer colors from the other direction
4
Instant Queries
Answer any query in O(1) by looking up the precomputed distance
Key Takeaway
🎯 Key Insight: Instead of searching for each query, precompute all possible answers using dynamic programming. This transforms an O(n) search per query into O(1) lookup time!
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 21
38.2K Views
Medium Frequency
~18 min Avg. Time
1.1K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen