📌  相关文章
📜  一个给定的子字符串恰好由K 1组成的最小交换次数

📅  最后修改于: 2021-04-17 19:06:41             🧑  作者: Mango

给定大小为N的二进制字符串S和三个正整数LRK ,任务是找到所需的最小交换数,以使子字符串{S [L],.. S [R]}包含恰好K 1 s。如果无法这样做,请打印“ -1”

例子:

方法:想法是计算子字符串{S [L],…,S [R]}中子字符串和子字符串外部存在的10的数量。然后,检查在该范围之外是否存在足够的1 s或0 s,可以交换该范围,以使子字符串恰好包含K 1 s。

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

  • 将1s和0s的频率分别存储在cntOnescntZeros中的字符串S中。
  • 另外,将1 s和0 s的频率分别存储在子串S [L,R]中的10
  • 使用以下公式在子字符串S [L,R]之外找到10的频率: (rem_ones = cntOnes – 1)rem_zero =(cntZeros – 0)
  • 如果k≥1 ,则执行以下操作:
    • 初始化rem =(K –一) ,它表示使总和等于K所需的1的数目。
    • 如果零≥REMrem_ones≥REM,打印REM作为结果的值。
  • 否则,如果K <个,则执行以下操作:
    • 初始化rem =(ones-K) ,它表示使总和等于K所需的0数。
    • 如果那些≥REMrem_zeros≥REM,REM打印作为结果的值。
  • 在所有其他情况下,请打印-1

下面是上述方法的实现:

C++
// CPP program for the above approach
#include
using namespace std;
 
    // Function to find the minimum number
    // of swaps required such that the
    // substring {s[l], .., s[r]} consists
    // of exactly k 1s
   int minimumSwaps(string s, int l, int r, int k)
    {
      
        // Store the size of the string
        int n = s.length();
 
        // Store the total number of 1s
        // and 0s in the entire string
        int tot_ones = 0, tot_zeros = 0;
 
        // Traverse the string S to find
        // the frequency of 1 and 0
        for (int i = 0; i < s.length(); i++)
        {
            if (s[i] == '1')
                tot_ones++;
            else
                tot_zeros++;
        }
 
        // Store the number of 1s and
        // 0s in the substring s[l, r]
        int ones = 0, zeros = 0, sum = 0;
 
        // Traverse the substring S[l, r]
        // to find the frequency of 1s
        // and 0s in it
        for (int i = l - 1; i < r; i++)
        {
            if (s[i] == '1')
            {
                ones++;
                sum++;
            }
            else
                zeros++;
        }
 
        // Store the count of 1s and
        // 0s outside substring s[l, r]
        int rem_ones = tot_ones - ones;
        int rem_zeros = tot_zeros - zeros;
 
        // Check if the sum of the
        // substring is at most K
        if (k >= sum)
        {
 
            // Store number of 1s required
            int rem = k - sum;
 
            // Check if there are enough 1s
            // remaining to be swapped
            if (zeros >= rem && rem_ones >= rem)
                return rem;
        }
 
        // If the count of 1s in the substring exceeds k
        else if (k < sum)
        {
 
            // Store the number of 0s required
            int rem = sum - k;
 
            // Check if there are enough 0s
            // remaining to be swapped
            if (ones >= rem && rem_zeros >= rem)
                return rem;
        }
 
        // In all other cases, print -1
        return -1;
    }
 
    // Driver Code
    int main()
    {
        string S = "110011111000101";
        int L = 5, R = 8, K = 2;
        cout<


Java
// Java program for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the minimum number
    // of swaps required such that the
    // substring {s[l], .., s[r]} consists
    // of exactly k 1s
    static int minimumSwaps(
        String s, int l, int r, int k)
    {
        // Store the size of the string
        int n = s.length();
 
        // Store the total number of 1s
        // and 0s in the entire string
        int tot_ones = 0, tot_zeros = 0;
 
        // Traverse the string S to find
        // the frequency of 1 and 0
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '1')
                tot_ones++;
            else
                tot_zeros++;
        }
 
        // Store the number of 1s and
        // 0s in the substring s[l, r]
        int ones = 0, zeros = 0, sum = 0;
 
        // Traverse the substring S[l, r]
        // to find the frequency of 1s
        // and 0s in it
        for (int i = l - 1; i < r; i++) {
            if (s.charAt(i) == '1') {
                ones++;
                sum++;
            }
            else
                zeros++;
        }
 
        // Store the count of 1s and
        // 0s outside substring s[l, r]
        int rem_ones = tot_ones - ones;
        int rem_zeros = tot_zeros - zeros;
 
        // Check if the sum of the
        // substring is at most K
        if (k >= sum) {
 
            // Store number of 1s required
            int rem = k - sum;
 
            // Check if there are enough 1s
            // remaining to be swapped
            if (zeros >= rem && rem_ones >= rem)
                return rem;
        }
 
        // If the count of 1s in the substring exceeds k
        else if (k < sum) {
 
            // Store the number of 0s required
            int rem = sum - k;
 
            // Check if there are enough 0s
            // remaining to be swapped
            if (ones >= rem && rem_zeros >= rem)
                return rem;
        }
 
        // In all other cases, print -1
        return -1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "110011111000101";
        int L = 5, R = 8, K = 2;
        System.out.println(
            minimumSwaps(S, L, R, K));
    }
}


Python3
# Python3 program for the above approach
 
# Function to find the minimum number
# of swaps required such that the
# substring {s[l], .., s[r]} consists
# of exactly k 1s
def minimumSwaps(s, l, r, k) :
 
    # Store the size of the string
    n = len(s)
 
    # Store the total number of 1s
    # and 0s in the entire string
    tot_ones, tot_zeros = 0, 0
 
    # Traverse the string S to find
    # the frequency of 1 and 0
    for i in range(0, len(s)) :
     
        if (s[i] == '1') :
            tot_ones += 1
        else :
            tot_zeros += 1
 
    # Store the number of 1s and
    # 0s in the substring s[l, r]
    ones, zeros, Sum = 0, 0, 0
 
    # Traverse the substring S[l, r]
    # to find the frequency of 1s
    # and 0s in it
    for i in range(l - 1, r) :
     
        if (s[i] == '1') :
         
            ones += 1
            Sum += 1
         
        else :
            zeros += 1
 
    # Store the count of 1s and
    # 0s outside substring s[l, r]
    rem_ones = tot_ones - ones
    rem_zeros = tot_zeros - zeros
 
    # Check if the sum of the
    # substring is at most K
    if (k >= Sum) :
 
        # Store number of 1s required
        rem = k - Sum
 
        # Check if there are enough 1s
        # remaining to be swapped
        if (zeros >= rem and rem_ones >= rem) :
            return rem
 
    # If the count of 1s in the substring exceeds k
    elif (k < Sum) :
 
        # Store the number of 0s required
        rem = Sum - k
 
        # Check if there are enough 0s
        # remaining to be swapped
        if (ones >= rem and rem_zeros >= rem) :
            return rem
 
    # In all other cases, print -1
    return -1
 
S = "110011111000101"
L, R, K = 5, 8, 2
print(minimumSwaps(S, L, R, K))
 
# This code is contributed by divyeshrabadiya07.


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
  // Function to find the minimum number
  // of swaps required such that the
  // substring {s[l], .., s[r]} consists
  // of exactly k 1s
  static int minimumSwaps(string s, int l, int r, int k)
  {
 
    // Store the size of the string
    int n = s.Length;
 
    // Store the total number of 1s
    // and 0s in the entire string
    int tot_ones = 0, tot_zeros = 0;
 
    // Traverse the string S to find
    // the frequency of 1 and 0
    for (int i = 0; i < s.Length; i++)
    {
      if (s[i] == '1')
        tot_ones++;
      else
        tot_zeros++;
    }
 
    // Store the number of 1s and
    // 0s in the substring s[l, r]
    int ones = 0, zeros = 0, sum = 0;
 
    // Traverse the substring S[l, r]
    // to find the frequency of 1s
    // and 0s in it
    for (int i = l - 1; i < r; i++)
    {
      if (s[i] == '1')
      {
        ones++;
        sum++;
      }
      else
        zeros++;
    }
 
    // Store the count of 1s and
    // 0s outside substring s[l, r]
    int rem_ones = tot_ones - ones;
    int rem_zeros = tot_zeros - zeros;
 
    // Check if the sum of the
    // substring is at most K
    if (k >= sum)
    {
 
      // Store number of 1s required
      int rem = k - sum;
 
      // Check if there are enough 1s
      // remaining to be swapped
      if (zeros >= rem && rem_ones >= rem)
        return rem;
    }
 
    // If the count of 1s in the substring exceeds k
    else if (k < sum)
    {
 
      // Store the number of 0s required
      int rem = sum - k;
 
      // Check if there are enough 0s
      // remaining to be swapped
      if (ones >= rem && rem_zeros >= rem)
        return rem;
    }
 
    // In all other cases, print -1
    return -1;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    string S = "110011111000101";
    int L = 5, R = 8, K = 2;
    Console.Write(minimumSwaps(S, L, R, K));
  }
}
 
// This code is contributed by splevel62.


输出:
2

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