-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution995.cs
41 lines (35 loc) · 943 Bytes
/
Solution995.cs
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
31
32
33
34
35
36
37
38
39
40
41
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution995
{
/// <summary>
/// 995. Minimum Number of K Consecutive Bit Flips - Hard
/// <a href="https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips">See the problem</a>
/// </summary>
public int MinKBitFlips(int[] nums, int k)
{
var n = nums.Length;
var flipped = new bool[n];
var result = 0;
var flipCount = 0;
for (var i = 0; i < n; i++)
{
if (i >= k)
{
flipCount -= flipped[i - k] ? 1 : 0;
}
if (flipCount % 2 == nums[i])
{
if (i + k > n)
{
return -1;
}
flipped[i] = true;
flipCount++;
result++;
}
}
return result;
}
}