Leetcode-995 Minimum Number of K Consecutive Bit Flips(K 连续位的最小翻转次数)

 1 #define maxn 30002
 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 3 int dir[maxn],f[maxn],n;
 4 class Solution
 5 {
 6     public:
 7         int solve(int k)
 8         {
 9             memset(f,0,sizeof(f));
10             int sum=0,res=0;
11             for(int i=0; i+k<=n; i++)
12             {
13                 if((dir[i] + sum) %2== 1)
14                 {
15                     f[i]=1;
16                     res++;
17                 }
18                 sum+=f[i];
19                 if(i-k+1>=0) sum-=f[i-k+1];
20             }
21
22             for(int i=n-k+1; i<n; i++)
23             {
24                 if((dir[i] + sum) %2== 1) return -1;
25                 if(i-k+1>=0) sum-=f[i-k+1];
26             }
27             return res;
28         }
29
30         int minKBitFlips(vector<int>& A, int K)
31         {
32             n = A.size();
33             _for(i,0,n)
34                 if(A[i])
35                     dir[i] = 0;
36                 else
37                     dir[i] = 1;
38             return solve(K);
39         }
40 };

经典ACM问题——奶牛掉头

原文地址:https://www.cnblogs.com/Asurudo/p/10390647.html

时间: 2024-10-29 17:58:01

Leetcode-995 Minimum Number of K Consecutive Bit Flips(K 连续位的最小翻转次数)的相关文章

LeetCode | 0452. Minimum Number of Arrows to Burst Balloons用最少数量的箭引爆气球【Python】

LeetCode 0452. Minimum Number of Arrows to Burst Balloons用最少数量的箭引爆气球[Medium][Python][区间贪心] Problem LeetCode There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of

【leetcode】995. Minimum Number of K Consecutive Bit Flips

题目如下: In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips

[LeetCode] Minimum Number of K Consecutive Bit Flips 连续K位翻转的最小次数

In an array?A?containing only 0s and 1s, a?K-bit flip?consists of choosing a (contiguous) subarray of length?K?and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of?K-bit flips requir

[LeetCode] 452 Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

[LeetCode] 871. Minimum Number of Refueling Stops 最少的加油站个数

A car travels from a starting position to a destination which is?target?miles east of the starting position. Along the way, there are gas stations.? Each?station[i]?represents a gas station that is?station[i][0]?miles east of the starting position, a

LeetCode 1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix (最少翻转次数将二进制矩阵全部置为0)

给一个矩阵mat,每个格子都是0或1,翻转一个格子会将该格子以及相邻的格子(有共同边)全部翻转(0变为1,1变为0) 求问最少需要翻转几次将所有格子全部置为0. 这题的重点是数据范围,比赛结束看了眼数据范围想把自己锤死= = m == mat.length n == mat[0].length 1 <= m <= 3 1 <= n <= 3 mat[i][j] is 0 or 1. 也就是....最多也就9个格子.....暴力怎么都能搞出来的..... 首先分析每个格子要么不反转,

LeetCode 1347. Minimum Number of Steps to Make Two Strings Anagram

题目 用hash,比较两个字符串数组的每个字符的hash值 class Solution { public: int m[300]; int m2[300]; int minSteps(string s, string t) { for(int i=0;i<s.length();i++) { m[s[i]]++; } for(int i=0;i<t.length();i++) { m2[t[i]]++; } int ans=0; int steps=0; for(int i=0;i<30

【leetcode】1347. Minimum Number of Steps to Make Two Strings Anagram

题目如下: Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character. Return the minimum number of steps to make t an anagram of s. An Anagram of a string is a string that contains the same c

Leetcode: Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s