1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Solution { public: int longestOnes(vector<int>& A, int K) { int n = A.size(), left = 0, right = 0, ans = 0; vector<int> P(n + 1); for (int i = 1; i <= n; ++i) P[i] = P[i - 1] + (1 - A[i - 1]); while (right++ < n) { left = lower_bound(P.begin(), P.end(), P[right] - K) - P.begin(); ans = max(ans, right - left); } return ans; } };
|