给定一个正整数K和一个由N 个字符组成的字符串S ,任务是最大化从给定字符串中恰好选择 K 个字符的频率之和。
例子:
Input: K – 3, S = ”GEEKSFORGEEKS”
Output: 12
Explanation:
Choose the character E from the given string S, the sum of frequency of 3 E’s is 3*4 = 12, which is the maximum.
Input: K = 10, S = ”GEEKSFORGEEKS”
Output: 28
方法:通过选择具有较高频率的K 个字符,可以使用贪心方法解决给定的问题。请按照以下步骤解决问题:
- 初始化一个变量,比如sum为0 ,它存储从字符串S 中选择的恰好 K 个字符的频率的结果总和。
- 找到字符串S中每个字符的频率并将其存储在一个辅助数组中,比如freq[] 。
- 按降序对数组freq[]进行排序。
- 遍历数组freq[]并执行以下操作:
- 如果当前频率小于K ,则将freq[i]*freq[i] 添加到变量sum 中,因为它最大化结果总和,并在选择freq[i]字符从K递减freq[i]的值。
- 否则,将K*freq[i]的值添加到变量sum并在选择了K 个字符跳出循环。
- 完成上述步骤后,打印sum的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum sum of
// frequencies of the exactly K chosen
// characters from the string S
int maximumSum(string S, int N, int K)
{
// Stores the resultant maximum sum
int sum = 0;
// Stores the frequency of array
// elements
int freq[256] = { 0 };
// Find the frequency of character
for (int i = 0; i < N; i++) {
freq[int(S[i])]++;
}
// Sort the frequency array in the
// descending order
sort(freq, freq + 256, greater());
// Iterate to chose K elements greedily
for (int i = 0; i < 256; i++) {
// If the freq[i] cards are
// chosen
if (K > freq[i]) {
sum += freq[i] * freq[i];
K -= freq[i];
}
// K cards have been picked
else {
sum += freq[i] * K;
break;
}
}
// Return the resultant sum
return sum;
}
// Driver Code
int main()
{
string S = "GEEKSFORGEEKS";
int K = 10;
int N = S.length();
cout << maximumSum(S, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the maximum sum of
// frequencies of the exactly K chosen
// characters from the String S
static int maximumSum(String S, int N, int K)
{
// Stores the resultant maximum sum
int sum = 0;
// Stores the frequency of array
// elements
Integer []freq = new Integer[256];
Arrays.fill(freq, 0);
// Find the frequency of character
for(int i = 0; i < N; i++)
{
freq[(int)S.charAt(i)] += 1;
}
// Sort the frequency array in the
// descending order
Arrays.sort(freq, Collections.reverseOrder());
// Iterate to chose K elements greedily
for(int i = 0; i < 256; i++)
{
// If the freq[i] cards are
// chosen
if (K > freq[i])
{
sum += freq[i] * freq[i];
K -= freq[i];
}
// K cards have been picked
else
{
sum += freq[i] * K;
break;
}
}
// Return the resultant sum
return sum;
}
// Driver Code
public static void main(String args[])
{
String S = "GEEKSFORGEEKS";
int K = 10;
int N = S.length();
System.out.print(maximumSum(S, N, K));
}
}
// This code is contributed by ipg2016107
Python3
# Python3 program for the above approach
# Function to find the maximum sum of
# frequencies of the exactly K chosen
# characters from the string S
def maximumSum(S, N, K):
# Stores the resultant maximum sum
sum = 0
# Stores the frequency of array
# elements
freq = [0] * 256
# Find the frequency of character
for i in range(N):
freq[ord(S[i])] += 1
# Sort the frequency array in the
# descending order
freq = sorted(freq)[::-1]
# Iterate to chose K elements greedily
for i in range(256):
# If the freq[i] cards are
# chosen
if (K > freq[i]):
sum += freq[i] * freq[i]
K -= freq[i]
# K cards have been picked
else:
sum += freq[i] * K
break
# Return the resultant sum
return sum
# Driver Code
if __name__ == '__main__':
S = "GEEKSFORGEEKS"
K = 10
N = len(S)
print(maximumSum(S, N, K))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the maximum sum of
// frequencies of the exactly K chosen
// characters from the string S
static int maximumSum(string S, int N, int K)
{
// Stores the resultant maximum sum
int sum = 0;
// Stores the frequency of array
// elements
int []freq = new int[256];
Array.Clear(freq, 0, 256);
// Find the frequency of character
for(int i = 0; i < N; i++)
{
freq[(int)S[i]]++;
}
// Sort the frequency array in the
// descending order
Array.Sort(freq);
Array.Reverse(freq);
// Iterate to chose K elements greedily
for(int i = 0; i < 256; i++)
{
// If the freq[i] cards are
// chosen
if (K > freq[i])
{
sum += freq[i] * freq[i];
K -= freq[i];
}
// K cards have been picked
else
{
sum += freq[i] * K;
break;
}
}
// Return the resultant sum
return sum;
}
// Driver Code
public static void Main()
{
string S = "GEEKSFORGEEKS";
int K = 10;
int N = S.Length;
Console.Write(maximumSum(S, N, K));
}
}
// This code is contributed by ipg2016107
Javascript
输出:
28
时间复杂度: O(N)
辅助空间: O(26)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。