leetcode

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

1438.cpp (683B)


      1 class Solution {
      2   public:
      3     int longestSubarray(vector<int> &nums, int limit) {
      4         deque<int> maxd, mind;
      5         int i = 0, j;
      6         for (j = 0; j < nums.size(); ++j) {
      7             while (!maxd.empty() && nums[j] > maxd.back())
      8                 maxd.pop_back();
      9             while (!mind.empty() && nums[j] < mind.back())
     10                 mind.pop_back();
     11             maxd.push_back(nums[j]), mind.push_back(nums[j]);
     12             if (maxd.front() - mind.front() > limit) {
     13                 if (maxd.front() == nums[i]) maxd.pop_front();
     14                 if (mind.front() == nums[i]) mind.pop_front();
     15                 i++;
     16             }
     17         }
     18         return j - i;
     19     }
     20 };