forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
56 lines (43 loc) · 1.29 KB
/
main.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
/// Source : https://leetcode.com/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/
/// Author : liuyubobobo
/// Time : 2021-10-30
#include <iostream>
#include <vector>
using namespace std;
/// Using Array
/// Time Compelxity: O(n)
/// Space Compelxity: O(n)
/// 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) {
vector<int> data;
while(head){
data.push_back(head->val);
head = head->next;
}
int n = data.size();
vector<int> pos;
for(int i = 1; i + 1 < n; i ++){
if(data[i - 1] < data[i] && data[i] > data[i + 1])
pos.push_back(i);
else if(data[i - 1] > data[i] && data[i] < data[i + 1])
pos.push_back(i);
}
if(pos.size() < 2) return {-1, -1};
int min_dis = INT_MAX;
for(int i = 1; i < pos.size(); i ++)
min_dis = min(min_dis, pos[i] - pos[i - 1]);
return {min_dis, pos.back() - pos[0]};
}
};
int main() {
return 0;
}