使所有 k 大小子数组的平均值小于 1 的最小翻转次数
给定一个大小为N的数组A ,每个元素为 0 或 1 和一个整数K 。找到需要翻转的最小元素个数,使得任何大小大于或等于K的子数组的算术平均值都不为 1。
例子:
Input: N = 5, A = {1, 1, 1, 1, 1}, K = 5
Output: 1
Explanation: Initially, mean of only sub-array of size 5 is (1+1+1+1+1)/5 = 1. So, flip the first element (i.e. make it 0). The array now becomes {0, 1, 1, 1, 1}, whose mean is less than 1. So, we needed just 1 flip to satisfy the required condition.
Note that {1, 1, 1, 1, 0} also satisfies required condition. Other arrays are also possible.
Input: N = 4, A = {1, 1, 0, 1}, K = 2
Output: 1
Explanation: flip the first 1 (i.e. element at 0 index), to that resultant array becomes {0, 1, 0, 1} in which no sub-array of size 2 of more has a mean 1.
Note that {1, 0, 0, 1} is also a possible array satisfying required condition.
方法:这个问题可以很容易地通过使用贪心技术来解决。
观察结果是,只有当其中所有 K 个元素都等于 1 时,大小为 K 的二进制数组的算术平均值才等于 1。此外,如果大小为 K 的所有子数组的含义都小于 1,那么所有大小大于 K 的子数组也意味着小于 1。因此,可以使用以下方法来解决问题 -
- 开始遍历给定的数组。
- 保持当前连续计数的计数,直到变量中的当前索引,例如“计数”。
- 如果当前元素为 1,我们将计数加 1,否则我们将其设为 0,因为在第i个索引处结束的连续 1 变为 0。
- 如果计数等于 K,这意味着有 K 个连续的 1 结束于当前索引,因此我们将答案增加 1(这意味着当前索引将变为 0)并再次使计数变量为 0。
下面是上述方法的实现:
C++
// C++ program to find Minimum flips to
// Make mean of all k size
// Sub-arrays less than 1
#include
using namespace std;
// Function to calculate
// Minimum flips to make
// Mean of all k size
// Subarrays less than 1
int minimumFlips(int N, int A[], int K)
{
// Initializing answer by 0
// That stores the number of flips
int answer = 0;
// Initializing count variable by 0
// That stores the number of consecutive 1s
int count = 0;
// iterating through the array
for (int i = 0; i < N; i++) {
if (A[i] == 1) {
// If current element is 1,
// We increment count by 1
count++;
// if count of consecutive 1s
// Reaches k, we increment the answer
// as the mean of the subarray from
// i-k to ith element becomes 1
if (count == K) {
answer++;
count = 0;
}
}
// else if current element is
// 0, we make count 0
else {
count = 0;
}
}
// returning the required answer
return answer;
}
// Driver Code
int main()
{
int N = 5, K = 5;
int A[] = { 1, 1, 1, 1, 1 };
int minimum_flips = minimumFlips(N, A, K);
cout << minimum_flips;
}
Java
// Java program to find Minimum flips to
// Make mean of all k size
// Sub-arrays less than 1
import java.io.*;
class GFG {
// Function to calculate
// Minimum flips to make
// Mean of all k size
// Subarrays less than 1
static int minimumFlips(int N, int A[], int K)
{
// Initializing answer by 0
// That stores the number of flips
int answer = 0;
// Initializing count variable by 0
// That stores the number of consecutive 1s
int count = 0;
// iterating through the array
for (int i = 0; i < N; i++)
{
if (A[i] == 1)
{
// If current element is 1,
// We increment count by 1
count++;
// if count of consecutive 1s
// Reaches k, we increment the answer
// as the mean of the subarray from
// i-k to ith element becomes 1
if (count == K) {
answer++;
count = 0;
}
}
// else if current element is
// 0, we make count 0
else {
count = 0;
}
}
// returning the required answer
return answer;
}
// Driver Code
public static void main (String[] args) {
int N = 5, K = 5;
int A[] = { 1, 1, 1, 1, 1 };
int minimum_flips = minimumFlips(N, A, K);
System.out.println( minimum_flips);
}
}
// This code is contributed by hrithikgarg03188.
Python
# Python program to find Minimum flips to
# Make mean of all k size
# Sub-arrays less than 1
# Function to calculate
# Minimum flips to make
# Mean of all k size
# Subarrays less than 1
def minimumFlips(N, A, K):
# Initializing answer by 0
# That stores the number of flips
answer = 0
# Initializing count variable by 0
# That stores the number of consecutive 1s
count = 0
# iterating through the array
for i in range(0, N):
if (A[i] == 1):
# If current element is 1,
# We increment count by 1
count += 1
# if count of consecutive 1s
# Reaches k, we increment the answer
# as the mean of the subarray from
# i-k to ith element becomes 1
if (count == K):
answer += 1
count = 0
# else if current element is
# 0, we make count 0
else:
count = 0
# returning the required answer
return answer
# Driver Code
N = 5
K = 5
A = [ 1, 1, 1, 1, 1 ]
minimum_flips = minimumFlips(N, A, K)
print(minimum_flips)
# This code is contributed by Samim Hossain Mondal.
C#
// C# program to find Minimum flips to
// Make mean of all k size
// Sub-arrays less than 1
using System;
class GFG {
// Function to calculate
// Minimum flips to make
// Mean of all k size
// Subarrays less than 1
static int minimumFlips(int N, int[] A, int K)
{
// Initializing answer by 0
// That stores the number of flips
int answer = 0;
// Initializing count variable by 0
// That stores the number of consecutive 1s
int count = 0;
// iterating through the array
for (int i = 0; i < N; i++) {
if (A[i] == 1) {
// If current element is 1,
// We increment count by 1
count++;
// if count of consecutive 1s
// Reaches k, we increment the answer
// as the mean of the subarray from
// i-k to ith element becomes 1
if (count == K) {
answer++;
count = 0;
}
}
// else if current element is
// 0, we make count 0
else {
count = 0;
}
}
// returning the required answer
return answer;
}
// Driver Code
public static void Main(string[] args)
{
int N = 5, K = 5;
int[] A = { 1, 1, 1, 1, 1 };
int minimum_flips = minimumFlips(N, A, K);
Console.WriteLine(minimum_flips);
}
}
// This code is contributed by ukasp.
Javascript
1
时间复杂度:O(N)
辅助空间:O(1)