-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily115.cpp
More file actions
81 lines (67 loc) · 2.04 KB
/
Copy pathdaily115.cpp
File metadata and controls
81 lines (67 loc) · 2.04 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
// Solution 1 - FAIL
class Solution {
public:
int maxWidthRamp(vector<int>& nums) {
/*
n^2:
brute force, test every possible ramp width and return max
n:
start with left and right pointers
if left num is greater than right num, left++
if right - 1 is >= new left, move right?
else return right - left?
*/
int left = 0;
int right = nums.size() - 1;
while (left < right) {
// std::cout << nums[left] << " - " << nums[right] << std::endl;
if (nums[left] > nums[right]) {
if (nums[left] <= nums[right - 1])
--right;
else
++left;
} else
return right - left;
}
return 0;
}
};
// Solution 2
class Solution {
public:
int maxWidthRamp(vector<int>& nums) {
auto indices = std::vector<int>(nums.size());
for (auto i = 0; i < nums.size(); ++i) {
indices[i] = i;
}
std::sort(indices.begin(), indices.end(), [&nums](int a, int b) {
return nums[a] < nums[b] || (nums[a] == nums[b] && a < b);
});
int min_index = nums.size();
int max_width = 0;
for (auto i = 0; i < nums.size(); ++i) {
max_width = std::max(max_width, indices[i] - min_index);
min_index = std::min(min_index, indices[i]);
}
return max_width;
}
};
// Solution 3
class Solution {
public:
int maxWidthRamp(vector<int>& nums) {
int n = nums.size();
vector<pair<int, int>> pairs(n);
for (int i = 0; i < n; i++) {
pairs[i] = {nums[i], i};
}
sort(pairs.begin(), pairs.end());
int max_width = 0;
int min_index = n;
for (const auto& [value, index] : pairs) {
max_width = max(max_width, index - min_index);
min_index = min(min_index, index);
}
return max_width;
}
};