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)


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