Formatted question description: https://leetcode.ca/all/1182.html

1182. Shortest Distance to Target Color

Level

Medium

Description

You are given an array colors, in which there are three colors: 1, 2 and 3.

You are also given some queries. Each query consists of two integers i and c, return the shortest distance between the given index i and the target color c. If there is no solution return -1.

Example 1:

Input: colors = [1,1,2,1,3,2,2,3,3], queries = [[1,3],[2,2],[6,1]]

Output: [3,0,3]

Explanation:

The nearest 3 from index 1 is at index 4 (3 steps away).

The nearest 2 from index 2 is at index 2 itself (0 steps away).

The nearest 1 from index 6 is at index 3 (3 steps away).

Example 2:

Input: colors = [1,2], queries = [[0,3]]

Output: [-1]

Explanation: There is no 3 in the array.

Constraints:

  • 1 <= colors.length <= 5*10^4
  • 1 <= colors[i] <= 3
  • 1 <= queries.length <= 5*10^4
  • queries[i].length == 2
  • 0 <= queries[i][0] < colors.length
  • 1 <= queries[i][1] <= 3

Solution

The idea is that given an index and a color, search in both sides of the given index, find the closest index where the color equals the given color and calculate the closest distance. To save runtime, use a map to store each color and index and the distance so that if there are repeated queries, retrieve the distance directly from the map and there is no need to search again. If a color is not found, then the query result is -1.

class Solution {
    public List<Integer> shortestDistanceColor(int[] colors, int[][] queries) {
        final int MAX_LENGTH = 50000;
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        List<Integer> distances = new ArrayList<Integer>();
        int colorsLength = colors.length;
        int queriesCount = queries.length;
        outer:
        for (int i = 0; i < queriesCount; i++) {
            int[] query = queries[i];
            int index = query[0], color = query[1];
            if (colors[index] == color)
                distances.add(0);
            else {
            	int key = color * MAX_LENGTH + index;
                if (map.containsKey(key)) {
                    int distance = map.get(key);
                    distances.add(distance);
                    continue;
                }
                int step = 1;
                int left = index - step, right = index + step;
                while (left >= 0 || right < colorsLength) {
                    if (left >= 0 && right < colorsLength) {
                        int leftColor = colors[left], rightColor = colors[right];
                        if (leftColor == color || rightColor == color) {
                            distances.add(step);
                            map.put(key, step);
                            continue outer;
                        } else {
                            left--;
                            right++;
                            step++;
                        }
                    } else if (left >= 0) {
                        int leftColor = colors[left];
                        if (leftColor == color) {
                            distances.add(step);
                            map.put(key, step);
                            continue outer;
                        } else {
                            left--;
                            step++;
                        }
                    } else {
                        int rightColor = colors[right];
                        if (rightColor == color) {
                            distances.add(step);
                            map.put(key, step);
                            continue outer;
                        } else {
                            right++;
                            step++;
                        }
                    }
                }
                distances.add(-1);
            }
        }
        return distances;
    }
}

All Problems

All Solutions