Leetcode 1004 Combination Sum II Solution in c++ | Hindi Coding Community

0

 



Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's.


Example 1:


Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2

Output: 6




class Solution {
public:
int longestOnes(vector<int>& A, int K) {
int res = 0, left = -1, n = A.size();
priority_queue<int, vector<int>, greater<int>> q;
for (int i = 0; i < n; ++i) {
if (A[i] == 0) {
if (K == 0) { left = i; continue; }
if (q.size() == K) {
left = q.top(); q.pop();
}
q.push(i);
}
res = max(res, i - left);
}
return res;
}
};


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !