-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
139 lines (125 loc) · 3.69 KB
/
main.cpp
File metadata and controls
139 lines (125 loc) · 3.69 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Source: https://leetcode.com/problems/shortest-distance-to-target-color
// Title: Shortest Distance to Target Color
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <array>
#include <unordered_map>
#include <vector>
using namespace std;
// Use binary search
class Solution {
public:
vector<int> shortestDistanceColor(vector<int>& colors, vector<vector<int>>& queries) {
int n = colors.size();
// Create color map
auto colorMap = unordered_map<int, vector<int>>(); // color -> indices
for (auto i = 0; i < n; i++) {
colorMap[colors[i]].push_back(i);
}
// Query
auto ans = vector<int>();
for (auto& query : queries) {
auto idx = query[0];
auto color = query[1];
auto it = colorMap.find(color);
if (it == colorMap.cend()) {
ans.push_back(-1);
} else {
auto& idxs = it->second;
auto it2 = lower_bound(idxs.cbegin(), idxs.cend(), idx);
if (it2 == idxs.cbegin()) {
ans.push_back(idxs.front() - idx);
} else if (it2 == idxs.cend()) {
ans.push_back(idx - idxs.back());
} else {
auto leftDist = idx - *(it2 - 1);
auto rightDist = *it2 - idx;
ans.push_back(min(leftDist, rightDist));
}
}
}
return ans;
}
};
// Precompute
class Solution2 {
public:
vector<int> shortestDistanceColor(vector<int>& colors, vector<vector<int>>& queries) {
int n = colors.size();
auto lefts = vector<array<int, 4>>(n);
auto rights = vector<array<int, 4>>(n);
// Lefts
{
auto idxs = array<int, 4>();
idxs.fill(-1);
for (auto i = 0; i < n; i++) {
auto color = colors[i];
idxs[color] = i;
lefts[i] = idxs;
}
}
// Rights
{
auto idxs = array<int, 4>();
idxs.fill(n);
for (auto i = n - 1; i >= 0; i--) {
auto color = colors[i];
idxs[color] = i;
rights[i] = idxs;
}
}
// Query
auto ans = vector<int>();
for (auto& query : queries) {
auto idx = query[0];
auto color = query[1];
auto left = lefts[idx][color];
auto right = rights[idx][color];
if (left == -1 && right == n) {
ans.push_back(-1);
} else if (left == -1) {
ans.push_back(right - idx);
} else if (right == n) {
ans.push_back(idx - left);
} else {
ans.push_back(min(right - idx, idx - left));
}
}
return ans;
}
};