forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-absolute-difference-queries.cpp
58 lines (56 loc) · 2.04 KB
/
minimum-absolute-difference-queries.cpp
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
// Time: O(r * (n + q)), r is the max of nums
// Space: O(r * n)
class Solution {
public:
vector<int> minDifference(vector<int>& nums, vector<vector<int>>& queries) {
static const int INF = numeric_limits<int>::max();
vector<vector<int>> prefix(1, vector<int>(*max_element(cbegin(nums), cend(nums)) + 1));
for (const auto& num : nums) {
prefix.emplace_back(prefix.back());
++prefix.back()[num];
}
vector<int> result;
for (const auto& query : queries) {
int min_diff = INF, prev = -1;
for (int num = 0; num < size(prefix[0]); ++num) {
if (!(prefix[query[0]][num] < prefix[query[1] + 1][num])) {
continue;
}
if (prev != -1) {
min_diff = min(min_diff, num - prev);
}
prev = num;
}
result.emplace_back((min_diff != INF) ? min_diff : -1);
}
return result;
}
};
// Time: O(r + n + q * r * logn), r is the max of nums
// Space: O(r + n)
class Solution2 {
public:
vector<int> minDifference(vector<int>& nums, vector<vector<int>>& queries) {
static const int INF = numeric_limits<int>::max();
vector<vector<int>> idxs(*max_element(cbegin(nums), cend(nums)) + 1);
for (int i = 0; i < size(nums); ++i) {
idxs[nums[i]].emplace_back(i);
}
vector<int> result;
for (const auto& query : queries) {
int min_diff = INF, prev = -1;
for (int num = 0; num < size(idxs); ++num) {
const auto cit = lower_bound(cbegin(idxs[num]), cend(idxs[num]), query[0]);
if (!(cit != cend(idxs[num]) && *cit <= query[1])) {
continue;
}
if (prev != -1) {
min_diff = min(min_diff, num - prev);
}
prev = num;
}
result.emplace_back((min_diff != INF) ? min_diff : -1);
}
return result;
}
};