📌  相关文章
📜  以升序对二进制字符串进行排序所需删除的最少字符– 集 2

📅  最后修改于: 2022-05-13 01:56:04.675000             🧑  作者: Mango

以升序对二进制字符串进行排序所需删除的最少字符– 集 2

给定二进制字符串 str的大小为N ,任务是从给定的二进制字符串中删除最小数量的字符,以使字符串字符中的字符按排序顺序排列。

例子:

Last Occurrence Approach:本文的第 1 组讨论了线性时间和常数空间中的优化方法。在这里,我们正在讨论动态规划方法。

动态规划方法:这个问题可以使用动态规划来解决,通过观察以下事实,如果需要删除K来使字符串排序到第 i索引并且

请按照以下步骤解决问题:

  • 将变量count1初始化为0N是字符串s的长度
  • 用值0初始化向量dp[n+1]
  • 使用变量i遍历范围[0, n)并执行以下任务:
    • 如果s[i]等于0,则将dp[i+1]设置为count11 + dp[i] 的最小值。
    • 否则,将dp[i+1]设置为dp[i]并将count1的值增加1。
  • 执行上述步骤后,打印dp[n]的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum number
// of deletions to make
// the string sorted
int minDeletions(string s)
{
    int n = s.size();
 
    // dp[i+1] stores minimum number of
    // deletion to make
    // substring(0, i) valid
    vector dp(n + 1, 0);
    int count1 = 0;
 
    for (int i = 0; i < n; i++) {
        if (s[i] == '0') {
 
            // Case 1: remove current 0
            // Case 2: keep current 0
            // then delete all 1 before it
            dp[i + 1] = min(count1,
                            1 + dp[i]);
        }
        else {
 
            // Appending 1 is always valid
            // if substring(0, i) is sorted
            dp[i + 1] = dp[i];
            count1++;
        }
    }
    return dp[n];
}
 
// Driver Code
int main()
{
    string s = "00101101";
    cout << minDeletions(s);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class GFG
{
   
// Function to find the minimum number
// of deletions to make
// the string sorted
static int minDeletions(String s)
{
    int n = s.length();
 
    // dp[i+1] stores minimum number of
    // deletion to make
    // substring(0, i) valid
    int []dp = new int[n + 1];
    for(int i = 0; i < n + 1; i++) {
        dp[i] = 0;
    }
    int count1 = 0;
 
    for (int i = 0; i < n; i++) {
        if (s.charAt(i) == '0') {
 
            // Case 1: remove current 0
            // Case 2: keep current 0
            // then delete all 1 before it
            dp[i + 1] = Math.min(count1,
                            1 + dp[i]);
        }
        else {
 
            // Appending 1 is always valid
            // if substring(0, i) is sorted
            dp[i + 1] = dp[i];
            count1++;
        }
    }
    return dp[n];
}
 
// Driver Code
public static void main(String args[])
{
    String s = "00101101";
    System.out.println(minDeletions(s));
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python code for the above approach
 
# Function to find the minimum number
# of deletions to make
# the string sorted
def minDeletions(s):
    n = len(s)
 
    # dp[i+1] stores minimum number of
    # deletion to make
    # substring(0, i) valid
    dp = [0] * (n + 1)
    count1 = 0
 
    for i in range(n):
        if (s[i] == '0'):
 
            # Case 1: remove current 0
            # Case 2: keep current 0
            # then delete all 1 before it
            dp[i + 1] = min(count1, 1 + dp[i])
        else:
 
            # Appending 1 is always valid
            # if substring(0, i) is sorted
            dp[i + 1] = dp[i]
            count1 += 1
    return dp[n]
 
# Driver Code
s = "00101101"
print(minDeletions(s))
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
public class GFG
{
 
    // Function to find the minimum number
    // of deletions to make
    // the string sorted
    static int minDeletions(string s)
    {
        int n = s.Length;
 
        // dp[i+1] stores minimum number of
        // deletion to make
        // substring(0, i) valid
        int[] dp = new int[n + 1];
        for (int i = 0; i < n + 1; i++)
        {
            dp[i] = 0;
        }
        int count1 = 0;
 
        for (int i = 0; i < n; i++)
        {
            if (s[i] == '0')
            {
 
                // Case 1: remove current 0
                // Case 2: keep current 0
                // then delete all 1 before it
                dp[i + 1] = Math.Min(count1,
                                1 + dp[i]);
            }
            else
            {
 
                // Appending 1 is always valid
                // if substring(0, i) is sorted
                dp[i + 1] = dp[i];
                count1++;
            }
        }
        return dp[n];
    }
 
    // Driver Code
    public static void Main()
    {
        string s = "00101101";
        Console.Write(minDeletions(s));
 
    }
}
 
// This code is contributed by Saurabh Jaiswal


Javascript



输出
2

时间复杂度: O(N)
辅助空间: O(N)