给定一个由N个小写英文字母组成的字符串S 、一个整数K和一个大小为26的数组cost[]表示每个小写英文字母的成本,任务是找到构建长度为K的子序列的最小成本字符 的字符串S 。
例子:
Input: S = “aabcbc”, K = 3, cost[] = {2, 1, 3, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9}
Output: 4
Explanation:
One way to construct a string of size K(= 3) is:
Form the string “abb” taking two ‘b’s with a cost of (2*1 = 2), and one ‘a’ with a cost of (1*2 = 2).
Therefore, the total cost to construct the string “abb” is (2 + 2 = 4), which is the minimum possible.
Input: S = “aaaca”, K = 1, cost[] = {2, 1, 3, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9}
Output: 2
方法:可以通过按递增顺序对数组cost[]进行排序并包括给定字符串S中存在的具有最小成本的前K 个字符来解决给定的问题。请按照以下步骤解决问题:
- 初始化一对 char 和 int 的向量,比如V来存储字符 以及那个字符的代价。
- 此外,初始化一个映射说mp ,键为字符,值为整数,以存储字符串S的每个字符的频率。
- 使用变量i遍历给定的字符串,并在每次迭代中递增mp[S[i]] 的值。
- 使用变量i在范围[0, 25] 上迭代,并在每次迭代中将{char(‘a’ + i), cost[i]}对附加到对V的向量。
- 对 V[] 对的向量进行排序 根据对的第二个元素。
- 现在,初始化一个变量,比如minCost为0来存储长度为K的子序列的最小成本。
- 使用变量i在范围[0, 25] 上迭代并执行以下步骤:
- 初始化一个变量,说,算作mp[‘a’ + i] 。
- 如果count的值小于K ,则将count*V[i].second的值与minCost的值相加,并将K的值更新为K – count 。
- 否则,后添加K * V [I]。第二至minCost的值,并中断环路的进行。
- 完成上述步骤后,打印minCost的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Custom comparator to sort according
// to the second element
bool comparator(pair p1,
pair p2)
{
return p1.second < p2.second;
}
// Function to find the minimum cost
// to construct a subsequence of the
// length K
int minimumCost(string S, int N,
int K, int cost[])
{
// Stores the minimum cost
int minCost = 0;
// Stores the pair of character
// and the cost of that character
vector > V;
// Stores the frequency of each
// character
unordered_map mp;
// Iterate in the range [0, N-1]
for (int i = 0; i < N; i++)
mp[S[i]]++;
// Iterate in the range [0, 25]
for (int i = 0; i < 26; i++) {
V.push_back({ char('a' + i), cost[i] });
}
// Sort the vector of pairs V
// wrt the second element
sort(V.begin(), V.end(), comparator);
// Iterate in the range [0, 25]
for (int i = 0; i < 26; i++) {
// Stores the frequency of the
// current char in the string
int count = mp[char('a' + i)];
// If count is less than
// or equal to K
if (count >= K) {
// Update the value of
// minCost
minCost += V[i].second * K;
break;
}
else if (count < K) {
// Update the value of
// minCost
minCost += V[i].second * count;
// Update the value
// of K
K = K - count;
}
}
// Print the value of minCost
return minCost;
}
// Driver Code
int main()
{
string S = "aabcbc";
int K = 3;
int cost[26] = { 2, 1, 3, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9 };
int N = S.length();
cout << minimumCost(S, N, K, cost);
return 0;
}
Python3
# Python3 program for the above approach
# Function to find the minimum cost
# to construct a subsequence of the
# length K
def minimumCost(S, N, K, cost):
# Stores the minimum cost
minCost = 0
# Stores the pair of character
# and the cost of that character
V = []
# Stores the frequency of each
# character
mp = {}
# Iterate in the range [0, N-1]
for i in range(N):
if (S[i] in mp):
mp[S[i]] += 1
else:
mp[S[i]] = 1
# Iterate in the range [0, 25]
for i in range(26):
V.append([chr(ord('a') + i), cost[i]])
# Sort the array of pairs V
# with the second element
while (1):
flag = 0
for i in range(len(V) - 1):
if (V[i][1] > V[i + 1][1]):
temp = V[i]
V[i] = V[i + 1]
V[i + 1] = temp
flag = 1
if (flag == 0):
break
# Iterate in the range [0, 25]
for i in range(26):
# Stores the frequency of the
# current char in the string
count = mp[chr(ord('a') + i)]
# If count is less than
# or equal to K
if (count >= K):
# Update the value of
# minCost
minCost += V[i][1] * K
break
elif (count < K):
# Update the value of
# minCost
minCost += V[i][1] * count
# Update the value
# of K
K = K - count
# Print the value of minCost
return minCost
# Driver Code
S = "aabcbc"
K = 3
cost = [ 2, 1, 3, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9 ]
N = len(S)
print(minimumCost(S, N, K, cost))
# This code is contributed by _saurabh_jaiswal
Javascript
4
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。