-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path波峰波谷
66 lines (55 loc) · 1.49 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
波峰波谷 一个每次只升或降1 的array (0,1,2,1,2,3) 求所有局部最大或最小 (example里应该是2 和 1).
folow up 是局部最大最小很少的情况下怎么做到log(n) (二分法) 后来也算是满意过了
vector<int> getPeakElement(vector<int> &nums) {
vector<int> res;
if (nums.size() <= 2) {
return res;
}
int pre = nums[1] - nums[0];
for (int i = 2; i < nums.size(); i++) {
if (nums[i] - nums[i - 1] != pre) {
res.push_back(nums[i - 1]);
pre = nums[i] - nums[i - 1];
}
}
return res;
}
vector<int> getPeaks(vector<int> &nums) {
vector<int> res;
if (nums.size() <= 2) {
return res;
}
getHelper(nums, 0, nums.size() - 1, res);
return res;
}
void getHelper(vector<int>& nums, int start, int end, vector<int>& res) {
if (start + 1 < end) {
if (isPeak(nums, start)) {
res.push_back(nums[start]);
}
if (isPeak(nums, right)) {
res.push_back(nums[end]);
}
return;
}
int mid = start + (end - start) / 2;
if (isPeak(nums, mid) {
res.push_back(nums[mid]);
}
if (abs(nums[start] - nums[mid]) != abs(mid - start)) {
getHelper(nums, start, mid - 1, res);
}
if (abs(nums[end] - nums[mid]) != abs(mid - end)) {
getHelper(nums, mid + 1, end, res);
}
return;
}
bool isPeak(vector<int>& nums, int idx) {
if (idx == 0 || idx == nums.size() - 1) {
return false;
}
if (nums[idx] > nums[idx - 1] && nums[idx] > nums[idx + 1] || (nums[idx] < nums[idx - 1] && nums[idx] < nums[idx + 1]) {
return true;
}
return false;
}