Advertisement
jayati

Max Consecutive Ones III

May 3rd, 2024
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.37 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int longestOnes(vector<int>& nums, int k) {
  4.          int i = 0, j;
  5.         for (j = 0; j < nums.size(); ++j)
  6.          {
  7.             if (nums[j] == 0)
  8.             {
  9.                 k--;
  10.             };
  11.             if (k < 0 && nums[i++] == 0)
  12.             {
  13.                k++;
  14.             }
  15.         }
  16.         return j - i;
  17.     }
  18. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement