-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
30 lines (29 loc) · 828 Bytes
/
Solution.cs
File metadata and controls
30 lines (29 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
namespace LeetCode.Medium.Problem1004{
//1004. Max Consecutive Ones III
//https://leetcode.com/problems/max-consecutive-ones-iii/
/*
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.
*/
public class Solution {
public int LongestOnes(int[] nums, int k) {
int left = 0;
int counter = 0;
int ans = 0;
for (int right = 0; right < nums.Length; right++)
{
if (nums[right] == 0)
counter++;
while (counter > k)
{
if (nums[left] == 0)
{
counter--;
}
left++;
}
ans = Math.Max(ans, right - left + 1);
}
return ans;
}
}
}