删除 k 个字符后给定字符串中字符数的最小平方和
给定字符串小写字母和一个数字 k,任务是在删除 'k' 个字符后打印字符串的最小值。字符串的值定义为每个不同字符的计数的平方和。例如考虑字符串“saideep”,这里字符的频率为 s-1、a-1、i-1、e-2、d-1、p-1,字符串的值为 1^2 + 1^2 + 1^2 + 1^2 + 1^2 + 2^2 = 9。
预期时间复杂度:O(k*logn)
例子:
Input : str = abccc, K = 1
Output : 6
We remove c to get the value as 12 + 12 + 22
Input : str = aaab, K = 2
Output : 2
问:亚马逊
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。
一个明确的观察是我们需要删除频率最高的字符。一招是字符马
一个简单的解决方案是使用排序技术将所有当前最高频率减少到 k 次。 For After each reduce 再次排序频率数组。
一个更好的解决方案,用于优先级队列,它具有顶部的最高元素。
- 初始化空的优先级队列。
- 计算每个字符的频率并存储到临时数组中。
- 从队列中删除频率最高的 K 个字符。
- 最后计算每个元素的平方和并返回它。
下面是上述想法的实现。
C++
// C++ program to find min sum of squares
// of characters after k removals
#include
using namespace std;
const int MAX_CHAR = 26;
// Main Function to calculate min sum of
// squares of characters after k removals
int minStringValue(string str, int k)
{
int l = str.length(); // find length of string
// if K is greater than length of string
// so reduced string will become 0
if (k >= l)
return 0;
// Else find Frequency of each character and
// store in an array
int frequency[MAX_CHAR] = { 0 };
for (int i = 0; i < l; i++)
frequency[str[i] - 'a']++;
// Push each char frequency into a priority_queue
priority_queue q;
for (int i = 0; i < MAX_CHAR; i++)
q.push(frequency[i]);
// Removal of K characters
while (k--) {
// Get top element in priority_queue,
// remove it. Decrement by 1 and again
// push into priority_queue
int temp = q.top();
q.pop();
temp = temp - 1;
q.push(temp);
}
// After removal of K characters find sum
// of squares of string Value
int result = 0; // Initialize result
while (!q.empty()) {
int temp = q.top();
result += temp * temp;
q.pop();
}
return result;
}
// Driver Code
int main()
{
string str = "abbccc"; // Input 1
int k = 2;
cout << minStringValue(str, k) << endl;
str = "aaab"; // Input 2
k = 2;
cout << minStringValue(str, k);
return 0;
}
Java
// Java program to find min sum of squares
// of characters after k removals
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Collections;
public class GFG {
static final int MAX_CHAR = 26;
// Main Function to calculate min sum of
// squares of characters after k removals
static int minStringValue(String str, int k)
{
int l = str.length(); // find length of string
// if K is greater than length of string
// so reduced string will become 0
if (k >= l)
return 0;
// Else find Frequency of each character and
// store in an array
int[] frequency = new int[MAX_CHAR];
for (int i = 0; i < l; i++)
frequency[str.charAt(i) - 'a']++;
// creating a priority queue with comparator
// such that elements in the queue are in
// descending order.
PriorityQueue q = new PriorityQueue<>(Collections.reverseOrder());
// Push each char frequency into a priority_queue
for (int i = 0; i < MAX_CHAR; i++) {
if (frequency[i] != 0)
q.add(frequency[i]);
}
// Removal of K characters
while (k != 0) {
// Get top element in priority_queue,
// remove it. Decrement by 1 and again
// push into priority_queue
q.add(q.poll() - 1);
k--;
}
// After removal of K characters find sum
// of squares of string Value
int result = 0; // Initialize result
while (!q.isEmpty()) {
result += q.peek() * q.poll();
}
return result;
}
// Driver Code
public static void main(String args[])
{
String str = "abbccc"; // Input 1
int k = 2;
System.out.println(minStringValue(str, k));
str = "aaab"; // Input 2
k = 2;
System.out.println(minStringValue(str, k));
}
}
// This code is contributed by Sumit Ghosh
Python 3
# Python3 program to find min sum of
# squares of characters after k removals
from queue import PriorityQueue
MAX_CHAR = 26
# Main Function to calculate min sum of
# squares of characters after k removals
def minStringValue(str, k):
l = len(str) # find length of string
# if K is greater than length of string
# so reduced string will become 0
if(k >= l):
return 0
# Else find Frequency of each
# character and store in an array
frequency = [0] * MAX_CHAR
for i in range(0, l):
frequency[ord(str[i]) - 97] += 1
# Push each char frequency negative
# into a priority_queue as the queue
# by default is minheap
q = PriorityQueue()
for i in range(0, MAX_CHAR):
q.put(-frequency[i])
# Removal of K characters
while(k > 0):
# Get top element in priority_queue
# multiply it by -1 as temp is negative
# remove it. Increment by 1 and again
# push into priority_queue
temp = q.get()
temp = temp + 1
q.put(temp, temp)
k = k - 1
# After removal of K characters find
# sum of squares of string Value
result = 0; # initialize result
while not q.empty():
temp = q.get()
temp = temp * (-1)
result += temp * temp
return result
# Driver Code
if __name__ == "__main__":
str = "abbccc"
k = 2
print(minStringValue(str, k))
str = "aaab"
k = 2
print(minStringValue(str, k))
# This code is contributed
# by Sairahul Jella
C#
// C# program to find min sum of squares
// of characters after k removals
using System;
using System.Collections.Generic;
class GFG {
static readonly int MAX_CHAR = 26;
// Main Function to calculate min sum of
// squares of characters after k removals
static int minStringValue(String str, int k)
{
int l = str.Length; // find length of string
// if K is greater than length of string
// so reduced string will become 0
if (k >= l)
return 0;
// Else find Frequency of each character and
// store in an array
int[] frequency = new int[MAX_CHAR];
for (int i = 0; i < l; i++)
frequency[str[i] - 'a']++;
// creating a priority queue with comparator
// such that elements in the queue are in
// descending order.
List q = new List();
// Push each char frequency into a priority_queue
for (int i = 0; i < MAX_CHAR; i++)
{
if (frequency[i] != 0)
q.Add(frequency[i]);
}
// Removal of K characters
while (k != 0)
{
q.Sort();
q.Reverse();
// Get top element in priority_queue,
// remove it. Decrement by 1 and again
// push into priority_queue
q.Add(q[0] - 1);
q.RemoveAt(0);
k--;
}
// After removal of K characters find sum
// of squares of string Value
int result = 0; // Initialize result
while (q.Count != 0)
{
result += q[0] * q[0];
q.RemoveAt(0);
}
return result;
}
// Driver Code
public static void Main(String []args)
{
String str = "abbccc"; // Input 1
int k = 2;
Console.WriteLine(minStringValue(str, k));
str = "aaab"; // Input 2
k = 2;
Console.WriteLine(minStringValue(str, k));
}
}
// This code is contributed by gauravrajput1
Javascript
输出:
6
2