Shortest Distance to Target Color in C++

C++Server Side ProgrammingProgramming

Suppose we have an array color, in which there are three colors: 1, 2 and 3. We have given some queries. Each query consists of two integers i and c, we have to find the shortest distance between the given index i and the target color c. If there is no solution, then return -1. So if the colors array is like [1,1,2,1,3,2,2,3,3], and the queries array is like [[1,3], [2,2], [6,1]], the output will be [3,0,3]. This is because the nearest 3 from index 1 is at index 4 (3 steps away). Then the nearest 2 from index 2 is at index 2 itself (0 steps away). And the nearest 1 from index 6 is at index 3 (3 steps away).

To solve this, we will follow these steps −

  • Create one matrix called index with 4 rows, n := number of elements in the color array

  • for I in range 0 to n – 1

    • insert i into index[colors[i]]

    • x := queries[i, 0] and c := queries[i, 1]

    • if size of index[c] is 0, then insert -1 into ret, and skip next iteration

    • it := first element that is not less than x – first element of index[c]

    • op1 := infinity, op2 := infinity

    • if it = size of index[c], decrease it by 1 op1 := |x – index[c, it]|

    • otherwise when it = 0, then op1 := |x – index[c, it]|

    • otherwise op1 := |x – index[c, it]|, decrease it by 1 and op2 := |x – index[c, it]|

    • insert minimum of op1 and op2 into ret

  • return ret

Example (C++)

Let us see the following implementation to get a better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class Solution {
public:
   vector<int> shortestDistanceColor(vector<int>& colors, vector<vector<int>>& queries) {
      vector < vector <int> >idx(4);
      int n = colors.size();
      for(int i = 0; i < n; i++){
         idx[colors[i]].push_back(i);
      }
      vector <int> ret;
      for(int i = 0; i < queries.size(); i++){
         int x = queries[i][0];
         int c = queries[i][1];
         if(idx[c].size() == 0){
            ret.push_back(-1);
            continue;
         }
         int it = lower_bound(idx[c].begin(), idx[c].end() , x) - idx[c].begin();
         int op1 = INT_MAX;
         int op2 = INT_MAX;
         if(it == idx[c].size()){
            it--;
            op1 = abs(x - idx[c][it]);
         }
         else if(it == 0){
            op1 = abs(x - idx[c][it]);
         }
         else{
            op1 = abs(x - idx[c][it]);
            it--;
            op2 = abs(x - idx[c][it]);
         }
         ret.push_back(min(op1, op2));
      }
      return ret;
   }
};
main(){
   vector<int> v = {1,1,2,1,3,2,2,3,3};
   vector<vector<int>> v1 = {{1,3},{2,2},{6,1}};
   Solution ob;
   print_vector(ob.shortestDistanceColor(v, v1));
}

Input

[1,1,2,1,3,2,2,3,3]
[[1,3],[2,2],[6,1]]

Output

[3,0,3]
raja
Published on 31-Mar-2020 09:06:19
Advertisements