forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
51 lines (39 loc) · 1.39 KB
/
main2.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
/// Source : https://leetcode.com/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/
/// Author : liuyubobobo
/// Time : 2021-10-31
#include <iostream>
#include <vector>
using namespace std;
/// One Pass in Linked List and O(1) Space
/// Time Compelxity: O(n)
/// Space Compelxity: O(1)
/// 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_node_val = head->val, prev_local = -1, first_local = -1, last_local = -1, min_dis = INT_MAX;
ListNode* cur = head->next;
for(int i = 1; cur->next; i ++, cur = cur->next){
if((prev_node_val < cur->val && cur->val > cur->next->val) ||
(prev_node_val > cur->val && cur->val < cur->next->val)){
if(first_local == -1) first_local = i;
last_local = max(last_local, i);
if(prev_local != -1) min_dis = min(min_dis, i - prev_local);
prev_local = i;
}
prev_node_val = cur->val;
}
if(min_dis == INT_MAX) return {-1, -1};
return {min_dis, last_local - first_local};
}
};
int main() {
return 0;
}