Advertisement
nikunjsoni

1425

Apr 17th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int constrainedSubsetSum(vector<int>& A, int k) {
  4.         deque<int> q;
  5.         int res = A[0];
  6.         for (int i = 0; i < A.size(); ++i) {
  7.             A[i] += q.size() ? q.front() : 0;
  8.             res = max(res, A[i]);
  9.             while (q.size() && A[i] > q.back())
  10.                 q.pop_back();
  11.             if (A[i] > 0)
  12.                 q.push_back(A[i]);
  13.             if (i >= k && q.size() && q.front() == A[i - k])
  14.                 q.pop_front();
  15.         }
  16.         return res;
  17.     }
  18. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement