Skip to content

1182. Shortest Distance to Target Color 👍

  • Time: $O(n)$
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
 public:
  vector<int> shortestDistanceColor(vector<int>& colors,
                                    vector<vector<int>>& queries) {
    constexpr int kNumColor = 3;
    const int n = colors.size();
    vector<int> ans;
    // left[i][c] := closest index of color c in index i to the left.
    vector<vector<int>> left(n, vector<int>(kNumColor + 1));
    // right[i][c] := closest index of color c in index i to the right.
    vector<vector<int>> right(n, vector<int>(kNumColor + 1));

    vector<int> colorToClosestIndex{0, -1, -1, -1};  // 0-indexed, -1 means N/A
    for (int i = 0; i < n; ++i) {
      colorToClosestIndex[colors[i]] = i;
      for (int c = 1; c <= kNumColor; ++c)
        left[i][c] = colorToClosestIndex[c];
    }

    colorToClosestIndex = {0, -1, -1, -1};  // Reset.
    for (int i = n - 1; i >= 0; --i) {
      colorToClosestIndex[colors[i]] = i;
      for (int c = 1; c <= kNumColor; ++c)
        right[i][c] = colorToClosestIndex[c];
    }

    for (const vector<int>& q : queries) {
      const int i = q[0];
      const int c = q[1];
      const int leftDist = left[i][c] == -1 ? INT_MAX : i - left[i][c];
      const int rightDist = right[i][c] == -1 ? INT_MAX : right[i][c] - i;
      const int minDist = min(leftDist, rightDist);
      ans.push_back(minDist == INT_MAX ? -1 : minDist);
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
  public List<Integer> shortestDistanceColor(int[] colors, int[][] queries) {
    final int kNumColor = 3;
    final int n = colors.length;
    List<Integer> ans = new ArrayList<>();
    // left[i][c] := closest index of color c in index i to the left.
    int[][] left = new int[n][kNumColor + 1];
    // right[i][c] := closest index of color c in index i to the right.
    int[][] right = new int[n][kNumColor + 1];

    int[] colorToClosestIndex = {0, -1, -1, -1}; // 0-indexed, -1 means N/A
    for (int i = 0; i < n; ++i) {
      colorToClosestIndex[colors[i]] = i;
      for (int c = 1; c <= kNumColor; ++c)
        left[i][c] = colorToClosestIndex[c];
    }

    colorToClosestIndex = {0, -1, -1, -1}; // Reset
    for (int i = n - 1; i >= 0; --i) {
      colorToClosestIndex[colors[i]] = i;
      for (int c = 1; c <= kNumColor; ++c)
        right[i][c] = colorToClosestIndex[c];
    }

    for (int[] q : queries) {
      final int i = q[0];
      final int c = q[1];
      final int leftDist = left[i][c] == -1 ? Integer.MAX_VALUE : i - left[i][c];
      final int rightDist = right[i][c] == -1 ? Integer.MAX_VALUE : right[i][c] - i;
      final int minDist = Math.min(leftDist, rightDist);
      ans.add(minDist == Integer.MAX_VALUE ? -1 : minDist);
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
  def shortestDistanceColor(self, colors: List[int], queries: List[List[int]]) -> List[int]:
    kNumColor = 3
    n = len(colors)
    ans = []
    # left[i][c] := closest index of color c in index i to the left.
    left = [[0] * (kNumColor + 1) for _ in range(n)]
    # right[i][c] := closest index of color c in index i to the right.
    right = [[0] * (kNumColor + 1) for _ in range(n)]

    colorToLatestIndex = [0, -1, -1, -1]  # 0-indexed, -1 means N//A
    for i, color in enumerate(colors):
      colorToLatestIndex[color] = i
      for c in range(1, kNumColor + 1):
        left[i][c] = colorToLatestIndex[c]

    colorToLatestIndex = [0, -1, -1, -1]  # Reset.
    for i in range(n - 1, -1, -1):
      colorToLatestIndex[colors[i]] = i
      for c in range(1, kNumColor + 1):
        right[i][c] = colorToLatestIndex[c]

    for i, c in queries:
      leftDist = math.inf if left[i][c] == -1 else i - left[i][c]
      rightDist = math.inf if right[i][c] == -1 else right[i][c] - i
      minDist = min(leftDist, rightDist)
      ans.append(-1 if minDist == math.inf else minDist)

    return ans