-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2058.cpp
More file actions
44 lines (43 loc) · 1.28 KB
/
2058.cpp
File metadata and controls
44 lines (43 loc) · 1.28 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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
vector<int> nodesBetweenCriticalPoints(ListNode* head) {
int prev = -1;
int current = -1;
vector<int> criticals;
int index = 0;
while (head) {
if (prev != -1 && current != -1) {
if (current > prev && current > head->val) {
criticals.push_back(index);
}
else if (current < prev && current < head->val) {
criticals.push_back(index);
}
}
prev = current;
current = head->val;
head = head->next;
index++;
}
int n = criticals.size();
if (n < 2) {
return {-1, -1};
}
int maxDistance = criticals[n - 1] - criticals[0];
int minDistance = maxDistance;
for (int i = 0; i < n - 1; ++i) {
minDistance = min(minDistance, criticals[i + 1] - criticals[i]);
}
return {minDistance, maxDistance};
}
};