1004. Max Consecutive Ones III 發表於 2023-12-21 | 分類於 leetcode problemsolution123456789101112131415161718192021222324252627282930313233343536373839404142class Solution {public: int longestOnes(vector<int>& nums, int k) { // silding window int l =0 , r =0, n=nums.size(); int p = k; int ret= 0, window=0; while(r<n) { int cur = nums[r++]; if(cur == 0){ if(p>=0){ p--; window++; } } else if(cur==1) { window++; } // p == 0 , can continue to rignt move while(p < 0) { int prev = nums[l++]; if(prev==0) { p++; window--; } else{ window--; } } if(p>=0){ // update // cout<<l-1<<" "<<r-1<<" "<<p<<" "<<window<<endl; ret = max(ret, window); } } return ret; }}; 1234567891011121314151617181920212223242526272829class Solution {public: int longestOnes(vector<int>& nums, int k) { // silding window int l =0 , r =0, n=nums.size(); int p = k; int ret= 0, window=0; while(r<n) { int cur = nums[r++]; if(cur == 0){ if(p>=0) p--; } window++; // p == 0 , can continue to rignt move while(p < 0) { int prev = nums[l++]; if(prev==0) p++; window--; } if(p>=0){ // update ret = max(ret, window); } } return ret; }}; 123456789101112131415161718192021222324class Solution {public: int longestOnes(vector<int>& nums, int k) { // silding window int l =0 , r =0, n=nums.size(); int ret=0; while(r<n) { if( nums[r++] == 0){ if(k>=0) k--; } // k == 0 , can continue to rignt move while(k < 0) { if(nums[l++]==0) k++; } if(k>=0){ // update ret = max(ret, r-l); } } return ret; }}; analysis time complexity O(n) space complexity O(1)