leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2058.cpp (830B)


      1 class Solution {
      2   public:
      3     vector<int> nodesBetweenCriticalPoints(ListNode *head) {
      4         ListNode *prev = head, *crnt = head->next, *next;
      5         vector<int> res(2, INT_MAX);
      6         int first = 0, last = 0;
      7         for (int pos = 1; next = crnt->next; pos++) {
      8             if (crnt->val > prev->val && crnt->val > next->val ||
      9                 crnt->val < prev->val && crnt->val < next->val) {
     10                 if (!first)
     11                     first = last = pos;
     12                 else {
     13                     res[0] = min(res[0], pos - last);
     14                     res[1] = pos - first;
     15                     last = pos;
     16                 }
     17             }
     18             prev = crnt;
     19             crnt = next;
     20         }
     21         if (res[0] == INT_MAX) res[0] = -1;
     22         if (res[1] == INT_MAX) res[1] = -1;
     23         return res;
     24     }
     25 };