Skip to content

Commit f0e20e8

Browse files
author
Adam Lin
committed
Improvement, use two ptr rather than prefix sum
1 parent 0385b46 commit f0e20e8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

1004_longestOnes.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,27 @@ class Solution {
5252
return prefixSum[R] - prefixSum[L - 1];
5353
}
5454
}
55+
};
56+
57+
class Solution {
58+
public:
59+
int longestOnes(vector<int>& nums, int k) {
60+
int L = 0, R = 0, maxWindow = 0, numZeros = 0;
61+
62+
while(R < nums.size()){
63+
if(nums[R] == 0){
64+
numZeros++;
65+
}
66+
67+
while(numZeros > k){
68+
if(nums[L] == 0){
69+
numZeros--;
70+
}
71+
L++;
72+
}
73+
maxWindow = max(maxWindow, R-L+1);
74+
R++;
75+
}
76+
return maxWindow;
77+
}
5578
};

0 commit comments

Comments
 (0)