-
Notifications
You must be signed in to change notification settings - Fork 15
/
max-distance_test.cpp
94 lines (79 loc) · 2.24 KB
/
max-distance_test.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
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
#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) \
do { \
std::cout << #x << ": " << x << " "; \
} while (0)
#define DEBUGL(x) \
do { \
std::cout << #x << ": " << x << "\n"; \
} while (0)
template <typename T> void print_vector(vector<T> a) {
for (auto &i : a) {
cout << i << " ";
}
cout << "\n";
}
// Time - O(N logN), Space - O(N)
int solve(const vector<int> &A) {
int n = A.size();
if (n == 0) {
return -1;
}
if (n == 1) {
return 0;
}
vector<pair<int, int>> to_sort;
for (int i = 0; i < n; i++) {
to_sort.push_back({A[i], i});
}
sort(to_sort.begin(), to_sort.end());
int max_idx = to_sort[n - 1].second;
int result = 0;
for (int i = n - 2; i >= 0; i--) {
result = max(max_idx - to_sort[i].second, result);
max_idx = max(to_sort[i].second, max_idx);
}
return result;
}
// Time - O(N), Space - O(N)
int solve_linear(const vector<int> &A) {
int n = A.size();
if (n == 0) {
return -1;
}
if (n == 1) {
return 0;
}
// left_min[i] = min(A[0] to A[i])
vector<int> left_min(n);
left_min[0] = A[0];
for (int i = 1; i < n; i++) {
left_min[i] = min(A[i], left_min[i - 1]);
}
// right_max[i] = max(A[i] to A[n - 1])
vector<int> right_max(n);
right_max[n - 1] = A[n - 1];
for (int i = n - 2; i >= 0; i--) {
right_max[i] = max(A[i], right_max[i + 1]);
}
int result = 0;
int i = 0, j = 0;
while (i < n && j < n) {
if (left_min[i] <= right_max[j]) {
result = max(j - i, result);
j += 1;
} else {
i++;
}
}
return result;
}
int main() {
// ios::sync_with_stdio(0);
// cin.tie(0);
vector<int> nums{9, 2, 3, 4, 5, 6, 7, 8, 18, 0};
cout << solve(nums) << "\n";
cout << solve_linear(nums) << "\n";
return 0;
}