1182. Minimum distance from target color

Posted by yhchan on Sun, 09 Jan 2022 00:21:42 +0100

1182. Minimum distance from target color

Give you an array of colors, which has 1, 2 and 3 colors.

We need to perform some queries on colors, where each item to be queried is composed of two integers i and c.

Now please help design an algorithm to find the shortest distance from index i to the element with target color c.

If no solution exists, 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 color 3 closest to index 1 is located at index 4 (distance 3).
The color 2 closest to index 2 is itself (distance 0).
The color 1 closest to index 6 is located at index 3 (distance 3).

Example 2:

Input: colors = [1,2], queries = [[0,3]]
Output:[-1]
Explanation: colors No color in 3.

Solution: DP

thought

Problem splitting

The target color may be on the left or right, so it is divided into two sub problems

  • The shortest distance between the left and the target color
  • The shortest distance between the right and the target color

At the last inquiry, take m i n { Left edge And order mark Face colour of most short distance leave , right edge And order mark Face colour of most short distance leave } min \ {the shortest distance between the left and the target color, and the shortest distance between the right and the target color \} min {the shortest distance between the left and the target color, and the shortest distance between the right and the target color}

State representation

  • f ( i , j ) f(i, j) f(i,j) represents the time from the beginning of colors[0] to the end of colors[i] j j The shortest distance of color j (the shortest distance between the left and the target color)
  • g ( i , j ) g(i, j) g(i,j) denotes the time from the beginning of colors[n - 1] to the end of colors[i] j j The shortest distance of color j (the shortest distance between the right and the target color)

state transition

  • Start with colors[0] and end with colors[i] j j The shortest distance of color j, which is the same as that from the beginning of colors[0] to the end of colors[i - 1] j j j is related to the shortest distance of the color, i.e f ( i , j ) f(i,j) f(i,j) from f ( i − 1 , j ) f(i - 1, j) f(i − 1,j) transfer
  • Start with colors[n - 1] and end with colors[i] j j The shortest distance of color j, and from the beginning of colors[n - 1] to the end of colors[i + 1] j j j is related to the shortest distance of the color, i.e g ( i , j ) g(i,j) g(i,j) from g ( i + 1 , j ) g(i+1,j) g(i+1,j) transfer

Complexity

Time complexity: O ( m a x { N , M } ) O(max \{N,M \}) O(max{N,M}), N N N is the length of colors, M M M is the length of queries

Space complexity: O ( N ) O(N) O(N)

code

class Solution {
    public List<Integer> shortestDistanceColor(int[] colors, int[][] queries) {
        int n = colors.length;
        int[][] f = new int[n + 1][4];
        int[][] g = new int[n + 1][4];
        
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= 3; j++) {
                f[i][j] = -1;
                g[i][j] = -1;
            }
        }
        
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= 3; j++) {
                if (colors[i - 1] == j) {
                    f[i][j] = 0;
                } else {
                    if (f[i - 1][j] == -1) f[i][j] = -1;
                    else f[i][j] = f[i - 1][j] + 1;
                }
            }
        }
        
        for (int i = n - 1; i >= 0; i--) {
            for (int j = 1; j <= 3; j++) {
                if (colors[i] == j) {
                    g[i][j] = 0;
                } else {
                    if (g[i + 1][j] == -1) g[i][j] = -1;
                    else g[i][j] = g[i + 1][j] + 1;
                }
            }
        }

        List<Integer> res = new ArrayList<>();
        for (var q : queries) {
            int a = f[q[0] + 1][q[1]], b = g[q[0]][q[1]];
            if (a == -1 && b == -1) res.add(-1);
            else if (a == -1) res.add(b);
            else if (b == -1) res.add(a);
            else res.add(Math.min(a, b));
        }
        
        return res;
    }

}

Topics: Algorithm leetcode Dynamic Programming