📌  相关文章
📜  所需的最小交换次数,以便给定的子串恰好由 K 个 1 组成

📅  最后修改于: 2021-09-07 02:36:10             🧑  作者: Mango

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

例子:

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

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

  • 将字符串S中 1 和 0 的频率分别存储在cntOnescntZeros 中
  • 另外,分别存储1秒和0秒中的子串S [L,R]的一的频率。
  • 使用公式: (rem_ones = cntOnes –ones )rem_zero = (cntZeros – zeros)找出子串S[L, R]之外的10的频率。
  • 如果k ≥ ones ,则执行以下操作:
    • 初始化rem = (K –ones) ,它表示使总和等于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.


Javascript


输出:
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live