通过将二进制字符串分成相同位的 K 组来最大化最小组的长度
给定一个长度为N的二进制字符串S。将字符串的0和1分配到K组中,这样:
- 每组至少包含一个字符。
- 没有组包含 1 和 0。
- 字符串的所有 1 和 0 都分布在所有组中。
任务是以使任何组中的最小字符数最大化的方式分配所有字符并打印该字符数(即K组中任何组中的最小字符数)。
例子:
Input: S = “01011”, K=5
Output: 1
Explanation: the K groups would be – {0}, {0}, {1}, {1}, {1}
Input: S = “11110000000111111”, K=4
Output: 3
方法:这个问题可以很容易地用贪心技术解决。
We will iteratively take each case into consideration i.e.
- for each i, from 1 to K-1,
- we will assign i groups to store all the zeroes and
- K-i groups to store all the ones.
- At each iteration, we will keep taking maximum of the current answer.
可以按照以下方法得出答案:
- 计算字符串中 0 和 1 的数量。
- 将答案初始化为 0。
- 如果 K>N(其中 N 是字符串的长度),则返回 0,因为在这种情况下,不可能使 K 个组中的每一个都包含至少一个字符。
- 从i =1 迭代到i =K-1。在每次迭代中,分配i个组来存储所有的零,分配 K- i组来存储所有的 1。
- 通过将 0 的总数除以分配用于存储 0 的组数来查找 i 组中每个组中的零数(假设留下一些零,因为它们可能不会平均分配给所有组,那么可以将它们放入进入任何一个组。没关系,因为我们需要字符最少的组)。同样,找出每组中 1 的数量。
- 找到两者中的最小值(即组中的 0 和组中的 1)并存储在变量中,例如min_count。
- 在每次迭代中找到所有min_count的最大值并返回。
以下是基于上述方法的代码:
C++
// C++ code for Divide the binary string
// into K groups such that minimum number
// of characters in any group is maximized
#include
using namespace std;
// Function to find the maximized minimum
// number of characters in a group,
// when given string is divided into
// K groups
int divideString(string S, int K)
{
// calculating length of given string
int N = S.length();
int zero = 0, one = 0;
// Calculating number of 0s and 1s
// in given string
for (int i = 0; i < N; i++) {
if (S[i] == '0') {
zero++;
}
else {
one++;
}
}
// initializing answer by 0
int answer = 0;
// if K>size of string, then it is not
// possible to make K groups such that
// each of them contains atleast 1 character
if (K > N) {
return 0;
}
for (int i = 1; i < K; i++) {
// let there be i groups with
// all elements 0
int zero_groups = i;
int one_groups = K - i;
// so, there will be K-i groups with
// all elements 1
int count0 = zero / zero_groups;
// number of 0s in
// each zero_groups
int count1 = one / one_groups;
// number of 1s in
// each one_groups
int min_count = min(count0, count1);
// since we have to find the
// count of characters in group
// with minimum characters
answer = max(answer, min_count);
// since we have to
// maximize the answer
}
// returning answer
return answer;
}
// Driver Code
int main()
{
string S = "11110000000111111";
int K = 4;
int ans = divideString(S, K);
cout << ans;
}
Java
// Java program of the above approach
import java.io.*;
class GFG {
// Function to find the maximized minimum
// number of characters in a group,
// when given string is divided into
// K groups
static int divideString(String S, int K)
{
// calculating length of given string
int N = S.length();
int zero = 0, one = 0;
// Calculating number of 0s and 1s
// in given string
for (int i = 0; i < N; i++) {
if (S.charAt(i) == '0') {
zero++;
}
else {
one++;
}
}
// initializing answer by 0
int answer = 0;
// if K>size of string, then it is not
// possible to make K groups such that
// each of them contains atleast 1 character
if (K > N) {
return 0;
}
for (int i = 1; i < K; i++) {
// let there be i groups with
// all elements 0
int zero_groups = i;
int one_groups = K - i;
// so, there will be K-i groups with
// all elements 1
int count0 = zero / zero_groups;
// number of 0s in
// each zero_groups
int count1 = one / one_groups;
// number of 1s in
// each one_groups
int min_count = Math.min(count0, count1);
// since we have to find the
// count of characters in group
// with minimum characters
answer = Math.max(answer, min_count);
// since we have to
// maximize the answer
}
// returning answer
return answer;
}
// Driver Code
public static void main (String[] args) {
String S = "11110000000111111";
int K = 4;
int ans = divideString(S, K);
System.out.println(ans);
}
}
// This code is contributed by hrithikgarg03188.
Python3
'''Python code for Divide the binary string
into K groups such that minimum number
of characters in any group is maximized
'''
# Function to find the maximized minimum
# number of characters in a group,
# when given string is divided into
# K groups
def divideString(S, K):
# calculating length of given string
N = len(S)
zero, one = 0, 0
# Calculating number of 0s and 1s in given string
for i in range(0, N):
if S[i] == '0':
zero += 1
else:
one += 1
# initializing answer by 0
answer = 0
# if K>size of string, then it is not
# possible to make K groups such that
# each of them contains atleast 1 character
if K > N:
return 0
# let there be i groups with all elements 0
for i in range(1, K):
zero_groups = i
one_groups = K - i
# so, there will be K-i groups with all elements 1
count0 = zero//zero_groups
# number of 0s in each zero_groups
count1 = one//one_groups
# number of 1s in each one_groups
min_count = min(count0, count1)
# since we have to find the count of
# characters in group with minimum characters
answer = max(answer, min_count)
# since we have to maximize the answer
return answer
S = '11110000000111111'
K = 4
ans = divideString(S, K)
print(ans)
'''This code is contributed by Rajat Kumar (GLA University).'''
C#
// C# program of the above approach
using System;
using System.Collections.Generic;
public class GFG{
// Function to find the maximized minimum
// number of characters in a group,
// when given string is divided into
// K groups
static int divideString(string S, int K)
{
// calculating length of given string
int N = S.Length;
int zero = 0, one = 0;
// Calculating number of 0s and 1s
// in given string
for (int i = 0; i < N; i++) {
if (S[i] == '0') {
zero++;
}
else {
one++;
}
}
// initializing answer by 0
int answer = 0;
// if K>size of string, then it is not
// possible to make K groups such that
// each of them contains atleast 1 character
if (K > N) {
return 0;
}
for (int i = 1; i < K; i++) {
// let there be i groups with
// all elements 0
int zero_groups = i;
int one_groups = K - i;
// so, there will be K-i groups with
// all elements 1
int count0 = zero / zero_groups;
// number of 0s in
// each zero_groups
int count1 = one / one_groups;
// number of 1s in
// each one_groups
int min_count = Math.Min(count0, count1);
// since we have to find the
// count of characters in group
// with minimum characters
answer = Math.Max(answer, min_count);
// since we have to
// maximize the answer
}
// returning answer
return answer;
}
// Driver Code
public static void Main(String[] args)
{
string S = "11110000000111111";
int K = 4;
int ans = divideString(S, K);
Console.Write(ans);
}
}
// This code is contributed by sanjoy_62.
Javascript
输出
3
时间复杂度: O(N)
辅助空间: O(1)