Skip to content

Commit 69d1664

Browse files
authored
Create 2058. Find the Minimum and Maximum Number of Nodes Between Cri… (#522)
2 parents 83ca70b + 2d86aab commit 69d1664

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
vector<int> nodesBetweenCriticalPoints(ListNode* head)
4+
{
5+
vector<int>store;
6+
7+
int ans1 = INT_MAX, cur, next, n;
8+
int last = head->val, position = 2;
9+
10+
while(head->next)
11+
{
12+
cur = head->val, next = head->next->val;
13+
14+
if((last > cur and cur < next) or (last < cur and cur > next))
15+
store.push_back(position);
16+
position++;
17+
last = cur, head = head->next;
18+
19+
n = store.size();
20+
if(n > 1) ans1 = min(ans1, store[n-1] - store[n-2]);
21+
}
22+
23+
if(ans1 == INT_MAX) return {-1, -1};
24+
25+
return {ans1, store.back() - store[0]};
26+
}
27+
};

0 commit comments

Comments
 (0)