给定一个二进制字符串str 、两个整数数组R[]和大小为N 的C[] 。将所有字符从索引i翻转到R[i]需要C[i]成本。任务是最小化将给定二进制字符串转换为仅0所需的成本。
例子:
Input: str = “1010”, R[] = {1, 2, 2, 3}, C[] = {3, 1, 2, 3}
Output: 4
Explanation:
Flipping all the characters from index 1 to 2, modifies str to “1100”. Therefore, cost = 1
Flipping all the characters from index 1 to 2, modifies str to “0000”. Therefore, cost = cost + 3 = 4
Therefore, minimum cost required is 4.
Input: str = “01100”, R[] = {1, 2, 3, 4, 5}, C[] = {1, 5, 5, 2, 3}
Output: 10
方法:该问题可以使用贪心技术解决。这个想法是从左到右遍历给定的字符串并检查当前字符是否为非零字符。如果发现为真,则将所有字符从当前索引 (= i)翻转到第R[i]个索引。请按照以下步骤解决问题:
- 初始化一个变量,比如flip ,以存储当前字符可以翻转的次数。
- 创建一个优先级队列,比如pq来存储当前翻转值大于 0 的字符右侧的字符索引范围。
- 初始化一个变量,比如cost来存储获得所需字符串的最小成本。
- 遍历给定的字符串并检查当前字符是否为“ 1 ”。如果发现为真,则将i到R[i]范围内的所有字符翻转,并将 cost 的值增加C[i] 。
- 最后,打印cost的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to get the minimum
// Cost to convert all characters
// of given string to 0s
int minCost(string str, int N, int R[], int C[])
{
// Stores the range of indexes
// of characters that need
// to be flipped
priority_queue, greater > pq;
// Stores the number of times
// current character is flipped
int flip = 0;
// Stores minimum cost to get
// the required string
int cost = 0;
// Traverse the given string
for (int i = 0; i < N; i++)
{
// Remove all value from pq
// whose value is less than i
while (pq.size() > 0 and pq.top() < i)
{
pq.pop();
// Update flip
flip--;
}
// If current character
// is flipped odd times
if (flip % 2 == 1)
{
str[i] = '1' - str[i] + '0';
}
// If current character contains
// non-zero value
if (str[i] == '1')
{
// Update flip
flip++;
// Update cost
cost += C[i];
// Append R[i] into pq
pq.push(R[i]);
}
}
return cost;
}
// Driver Code
int main()
{
string str = "1010";
int R[] = { 1, 2, 2, 3 };
int C[] = { 3, 1, 2, 3 };
int N = str.length();
// Function call
cout << minCost(str, N, R, C);
}
Java
// Java program to implement
// the above approach
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
// Function to get the minimum
// Cost to convert all characters
// of given string to 0s
public static int minCost(String s,
int R[],
int C[],
int N)
{
char ch[] = s.toCharArray();
// Stores the range of indexes
// of characters that need
// to be flipped
PriorityQueue pq = new PriorityQueue<>();
// Stores the number of times
// current character is flipped
int flip = 0;
// Stores minimum cost to get
// the required string
int cost = 0;
// Traverse the given string
for (int i = 0; i < N; i++) {
// Remove all value from pq
// whose value is less than i
while (pq.size() > 0 && pq.peek() < i)
{
pq.poll();
// Update flip
flip--;
}
// Get the current number
int cn = ch[i] - '0';
// If current character
// is flipped odd times
if (flip % 2 == 1)
cn = 1 - cn;
// If current character contains
// non-zero value
if (cn == 1)
{
// Update flip
flip++;
// Update cost
cost += C[i];
// Append R[i] into pq
pq.add(R[i]);
}
}
return cost;
}
// Driver Code
public static void main(String[] args)
{
int N = 4;
String s = "1010";
int R[] = { 1, 2, 2, 3 };
int C[] = { 3, 1, 2, 3 };
// Function call
System.out.println(minCost(s, R, C, N));
}
}
Python3
# Python3 program to implement
# the above approach
# Function to get the minimum
# Cost to convert all characters
# of given string to 0s
def minCost(s, R, C, N) :
ch = list(s)
# Stores the range of indexes
# of characters that need
# to be flipped
pq = []
# Stores the number of times
# current character is flipped
flip = 0
# Stores minimum cost to get
# the required string
cost = 0
# Traverse the given string
for i in range(N) :
# Remove all value from pq
# whose value is less than i
while (len(pq) > 0 and pq[0] < i) :
pq.pop(0);
# Update flip
flip -= 1
# Get the current number
cn = ord(ch[i]) - ord('0')
# If current character
# is flipped odd times
if (flip % 2 == 1) :
cn = 1 - cn
# If current character contains
# non-zero value
if (cn == 1) :
# Update flip
flip += 1
# Update cost
cost += C[i]
# Append R[i] into pq
pq.append(R[i])
return cost
# Driver code
N = 4
s = "1010"
R = [ 1, 2, 2, 3 ]
C = [ 3, 1, 2, 3 ]
# Function call
print(minCost(s, R, C, N))
# This code is contributed by divyeshrabadiya07.
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to get the minimum
// Cost to convert all characters
// of given string to 0s
public static int minCost(String s, int []R,
int []C, int N)
{
char []ch = s.ToCharArray();
// Stores the range of indexes
// of characters that need
// to be flipped
Queue pq = new Queue();
// Stores the number of times
// current character is flipped
int flip = 0;
// Stores minimum cost to get
// the required string
int cost = 0;
// Traverse the given string
for(int i = 0; i < N; i++)
{
// Remove all value from pq
// whose value is less than i
while (pq.Count > 0 && pq.Peek() < i)
{
pq.Dequeue();
// Update flip
flip--;
}
// Get the current number
int cn = ch[i] - '0';
// If current character
// is flipped odd times
if (flip % 2 == 1)
cn = 1 - cn;
// If current character contains
// non-zero value
if (cn == 1)
{
// Update flip
flip++;
// Update cost
cost += C[i];
// Append R[i] into pq
pq.Enqueue(R[i]);
}
}
return cost;
}
// Driver Code
public static void Main(String[] args)
{
int N = 4;
String s = "1010";
int []R = { 1, 2, 2, 3 };
int []C = { 3, 1, 2, 3 };
// Function call
Console.WriteLine(minCost(s, R, C, N));
}
}
// This code is contributed by Amit Katiyar
4
时间复杂度: O(N * log N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live