将所有 0 转换为 1 的成本最小化,转换 0 组的成本为 X,1 的成本为 X/3
给定二进制字符串str ,仅由两个字符“ 1 ”和“ 0 ”以及一个整数X组成,任务是计算将所有字符转换为“1”的最小成本。将一个'0'转换为'1'的成本为X ,将一个'1'转换为'0'的成本为X / 3 。如果将“0”转换为“1” ,则与其左右相邻的所有0作为一个组在其左右将被转换为“1” ,而无需任何成本。
例子:
Input: str = “101001”, X = 6
Output: 8
Explanation: Replace the character at index 2 i.e ‘1’ with ‘0’ which will take 6 / 3 =2 cost, then change it back to ‘1’ with the cost of 6 so that the string becomes “111111”
Input: str = “10110100”, X = 3
Output: 6
方法:给定的问题可以使用 动态规划。可以按照以下步骤解决问题:
- 声明一个数组dp[]来存储计算的答案
- 用INT_MAX初始化第一个元素
- 如果字符串的第一个字符是“ 0 ”,则用K重新初始化dp[]的第一个元素。
- 遍历字符串str并检查以下条件:
- 如果字符是“ 1 ”并且如果dp[i-1]不等于INT_MAX,则将总和增加 1
- 否则,如果字符为“ 0 ”:
- dp[i-1] 等于INT_MAX ,设置dp[i] = k
- 如果前一个字符不等于' 0 ',则计算最小值并将其分配给dp[i]
- 返回dp[N]这是所需的答案
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to find minimum cost required
// to replace all Bs with As
int replaceZerosWithOnes(string str,
int K, int N)
{
// Declare an array to store
// the temporary answer
int dp[N + 1] = { 0 };
// Initialize the dp[0] with INT_MAX
dp[0] = INT_MAX;
int sum = 0;
// If character is 'B'
if (str[0] == '0') {
// Re-initialize dp[0] to K
dp[0] = K;
}
for (int i = 1; i < N; i++) {
// If current character
// is equal to A
if (str[i] == '1') {
// If dp[i-1] is not equal
// to INT_MAX
if (dp[i - 1] != INT_MAX) {
// Increment sum by 1
sum++;
}
dp[i] = dp[i - 1];
}
// If current character is 'B'
// and dp[i-1] is equal to
// INT_MAX
else if (str[i] == '0'
&& dp[i - 1] == INT_MAX) {
// Set dp[i] = k
dp[i] = K;
}
// If current character is 'B' and
// previous character was not 'B'
else if (str[i] == '0'
&& str[i - 1] != '0') {
// Calculate the minimum
// value and assign it to dp[i]
dp[i] = min((dp[i - 1] + (sum * K / 3)),
dp[i - 1] + K);
sum = 0;
}
// Else move next
else
dp[i] = dp[i - 1];
}
// Return last element stored in dp
return dp[N - 1];
}
// Driver Code
int main()
{
string str = "10110100";
int N = str.size();
int K = 3;
// Print the result of the function
cout << replaceZerosWithOnes(str, K, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find minimum cost required
// to replace all Bs with As
static int replaceZerosWithOnes(String str,
int K, int N)
{
// Declare an array to store
// the temporary answer
int dp[] = new int[N + 1];
for (int i = 0; i < N + 1; i++)
dp[i] = 0;
// Initialize the dp[0] with INT_MAX
dp[0] = Integer.MAX_VALUE;
int sum = 0;
// If character is 'B'
if (str.charAt(0) == '0') {
// Re-initialize dp[0] to K
dp[0] = K;
}
for (int i = 1; i < N; i++) {
// If current character
// is equal to A
if (str.charAt(i) == '1') {
// If dp[i-1] is not equal
// to INT_MAX
if (dp[i - 1] != Integer.MAX_VALUE) {
// Increment sum by 1
sum++;
}
dp[i] = dp[i - 1];
}
// If current character is 'B'
// and dp[i-1] is equal to
// INT_MAX
else if (str.charAt(i) == '0'
&& dp[i - 1] == Integer.MAX_VALUE) {
// Set dp[i] = k
dp[i] = K;
}
// If current character is 'B' and
// previous character was not 'B'
else if (str.charAt(i) == '0'
&& str.charAt(i - 1) != '0') {
// Calculate the minimum
// value and assign it to dp[i]
dp[i] = Math.min((dp[i - 1] + (sum * K / 3)),
dp[i - 1] + K);
sum = 0;
}
// Else move next
else
dp[i] = dp[i - 1];
}
// Return last element stored in dp
return dp[N - 1];
}
// Driver code
public static void main(String args[])
{
String str = "10110100";
int N = str.length();
int K = 3;
// Print the result of the function
System.out.println(replaceZerosWithOnes(str, K, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# python implementation for the above approach
INT_MAX = 2147483647
# Function to find minimum cost required
# to replace all Bs with As
def replaceZerosWithOnes(str, K, N):
# Declare an array to store
# the temporary answer
dp = [0 for _ in range(N + 1)]
# Initialize the dp[0] with INT_MAX
dp[0] = INT_MAX
sum = 0
# If character is 'B'
if (str[0] == '0'):
# Re-initialize dp[0] to K
dp[0] = K
for i in range(1, N):
# If current character
# is equal to A
if (str[i] == '1'):
# If dp[i-1] is not equal
# to INT_MAX
if (dp[i - 1] != INT_MAX):
# Increment sum by 1
sum += 1
dp[i] = dp[i - 1]
# If current character is 'B'
# and dp[i-1] is equal to
# INT_MAX
elif (str[i] == '0' and dp[i - 1] == INT_MAX):
# Set dp[i] = k
dp[i] = K
# If current character is 'B' and
# previous character was not 'B'
elif (str[i] == '0' and str[i - 1] != '0'):
# Calculate the minimum
# value and assign it to dp[i]
dp[i] = min((dp[i - 1] + ((sum * K) // 3)), dp[i - 1] + K)
sum = 0
# Else move next
else:
dp[i] = dp[i - 1]
# Return last element stored in dp
return dp[N - 1]
# Driver Code
if __name__ == "__main__":
str = "10110100"
N = len(str)
K = 3
# Print the result of the function
print(replaceZerosWithOnes(str, K, N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections;
class GFG{
// Function to find minimum cost required
// to replace all Bs with As
static int replaceZerosWithOnes(string str,
int K, int N)
{
// Declare an array to store
// the temporary answer
int []dp = new int[N + 1];
for (int i = 0; i < N + 1; i++)
dp[i] = 0;
// Initialize the dp[0] with INT_MAX
dp[0] = Int32.MaxValue;
int sum = 0;
// If character is 'B'
if (str[0] == '0') {
// Re-initialize dp[0] to K
dp[0] = K;
}
for (int i = 1; i < N; i++) {
// If current character
// is equal to A
if (str[i] == '1') {
// If dp[i-1] is not equal
// to INT_MAX
if (dp[i - 1] != Int32.MaxValue) {
// Increment sum by 1
sum++;
}
dp[i] = dp[i - 1];
}
// If current character is 'B'
// and dp[i-1] is equal to
// INT_MAX
else if (str[i] == '0'
&& dp[i - 1] == Int32.MaxValue) {
// Set dp[i] = k
dp[i] = K;
}
// If current character is 'B' and
// previous character was not 'B'
else if (str[i] == '0'
&& str[i - 1] != '0') {
// Calculate the minimum
// value and assign it to dp[i]
dp[i] = Math.Min((dp[i - 1] + (sum * K / 3)),
dp[i - 1] + K);
sum = 0;
}
// Else move next
else
dp[i] = dp[i - 1];
}
// Return last element stored in dp
return dp[N - 1];
}
// Driver code
public static void Main()
{
string str = "10110100";
int N = str.Length;
int K = 3;
// Print the result of the function
Console.Write(replaceZerosWithOnes(str, K, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
6
时间复杂度: O(N)
辅助空间: O(N)