leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2058.cpp (830B)
0 class Solution { 1 public: 2 vector<int> nodesBetweenCriticalPoints(ListNode *head) { 3 ListNode *prev = head, *crnt = head->next, *next; 4 vector<int> res(2, INT_MAX); 5 int first = 0, last = 0; 6 for (int pos = 1; next = crnt->next; pos++) { 7 if (crnt->val > prev->val && crnt->val > next->val || 8 crnt->val < prev->val && crnt->val < next->val) { 9 if (!first) 10 first = last = pos; 11 else { 12 res[0] = min(res[0], pos - last); 13 res[1] = pos - first; 14 last = pos; 15 } 16 } 17 prev = crnt; 18 crnt = next; 19 } 20 if (res[0] == INT_MAX) res[0] = -1; 21 if (res[1] == INT_MAX) res[1] = -1; 22 return res; 23 } 24 };