leetcode(c++)(单调序列)
#include <iostream> #include <vector> #include <queue> using namespace std; vector<int>maxSlidingWindow(const vector<int>& nums,int k) { int n = nums.size(); deque<int>q; vector<int>res(n-k+1); for(int i = 0; i < n; ++i) { int start = i-k+1; while(!q.empty() && i - q.front() >= k)q.pop_front(); while(!q.empty() && nums[q.front()] <= nums[i])q.pop_back(); q.push_back(i); if(start >= 0)res[start] = nums[q.front()]; } return res; } void print(const vector<int>& nums) { for(auto num : nums) { cout << num << " "; } cout << endl; } int shortestSubarray(const vector<int>& A, int k) { int n = A.size(),res = n + 1; vector<int>sum(n + 1); for(int i = 0; i < n; ++i) { sum[i+1] = sum[i] + A[i]; } deque<int>q; for(int i = 0; i < n + 1 ; ++i) { while(!q.empty() && sum[i] - sum[q.front()] >= k) { res = min(res,i - q.front()); q.pop_front(); } while(!q.empty() && sum[q.front()] >= sum[i])q.pop_back(); q.push_back(i); } return res <= n ? res : -1; } int constrainedSubSum(const vector<int>& nums,int k) { deque<int>q; int n = nums.size(); vector<int>sum(n); int res = sum[0]; for(int i = 0; i < n; ++i) { sum[i] = nums[i]; if(!q.empty())sum[i]+=sum[q.front()]; res = max(res,sum[i]); if(!q.empty() && i - q.front() >= k)q.pop_front(); while(!q.empty() && sum[q.back()] <= sum[i])q.pop_back(); if(sum[i] > 0)q.push_back(i); } return res; } int longestSubarry(const vector<int>& A,int limit) { deque<int>maxd,mind; int left = 0, res = 0; for(int i = 0 ; i < A.size(); ++i) { while(!maxd.empty() && maxd.back() < A[i])maxd.pop_back(); while(!mind.empty() && A[i] < mind.front())mind.pop_back(); maxd.push_back(A[i]); mind.push_back(A[i]); if(maxd.front() - mind.front() > limit) { if(maxd.front() == A[left])maxd.pop_front(); if(mind.front() == A[left])mind.pop_front(); ++left; } res = max(res,i - left + 1); } return res; } int maxResult(const vector<int>& nums,int k) { int n = nums.size(); vector<int>dp(n); dp[0] = nums[0]; deque<int>q; q.push_back(0); for(int i = 0; i < n - 1;++i) { while(!q.empty() && i - q.front() >= k)q.pop_front(); while(!q.empty() && dp[q.front()] <= dp[i])q.pop_back(); q.push_back(i); dp[i+1]=dp[q.front()] + nums[i + 1]; } return dp[n-1]; } int main() { //LeetCode239 vector<int>nums{1,3,-1,-3,5,3,6,7}; int k = 3; print(maxSlidingWindow(nums,k)); //LeetCode862 vector<int>A{1,2}; k = 2; cout << shortestSubarray(A,k) << endl; //LeetCode1425 nums = {10,2,-10,5,20}; k = 2; cout << constrainedSubSum(nums,k) << endl; //LeetCode1438 nums = {8,2,4,7}; k = 4; cout << longestSubarry(nums,k) << endl; //LeetCode1696 nums ={1,-1,-2,4,-7,3}; k = 2; cout << maxResult(nums,k) << endl; return 0; }