Home > Backend Development > C++ > What is the minimum number of exchanges required so that a given substring contains exactly K 1's?

What is the minimum number of exchanges required so that a given substring contains exactly K 1's?

王林
Release: 2023-08-25 23:25:10
forward
695 people have browsed it

What is the minimum number of exchanges required so that a given substring contains exactly K 1s?

Finding the minimum number of swaps required for a substring to contain exactly K ones is a common problem in computer science and programming. In this article, we will delve into this problem and provide a C solution for it. This question has applications in various fields, including string manipulation, data structure optimization, and coding challenges in interviews.

Problem Statement

Given a binary string and a number K, the task is to find the minimum number of swaps required to ensure that each substring of the string has exactly K 1's.

method

To solve this problem, we can use the two-pointer method and sliding window technology. The basic idea is to maintain a window of size K and calculate the number of exchanges required for all 1s in the window.

Example

This is a C function that implements the above method -

#include<bits/stdc++.h>
using namespace std;

int minSwaps(string s, int K) {
   int n = s.length();
   vector<int> onesPrefix(n, 0);
   if(s[0] == '1') onesPrefix[0] = 1;
   
   for(int i = 1; i < n; i++) {
      onesPrefix[i] = onesPrefix[i-1];
      if(s[i] == '1') onesPrefix[i]++;
   }
   
   int ans = INT_MAX;
   for(int i = 0; i <= n - K; i++) {
      int j = i + K - 1;
      int ones = onesPrefix[j] - ((i == 0) ? 0 : onesPrefix[i - 1]);
      ans = min(ans, K - ones);
   }
   
   return ans;
}

int main() {
   string s = "10010110";
   int K = 3;
   cout << "Minimum number of swaps = " << minSwaps(s, K) << endl;
   return 0;
}
Copy after login

Output

Minimum number of swaps = 1
Copy after login

Test case description

Assume the string is "10010110", K = 3.

In the initial binary string "10010110", we want each substring of size 3 to have exactly 3 1's. For example, the substring "100" requires 2 exchanges to become "111". Similarly, the substring "001" also requires 2 exchanges. By iterating over the string, we find that the minimum number of swaps required for the substring "101" is 1.

in conclusion

This question is a great example of how to combine algorithms, data structures, and an understanding of the C language to solve complex problems. Understanding and implementing such questions can be very beneficial for software engineers, especially in coding interviews and competitive programming.

The above is the detailed content of What is the minimum number of exchanges required so that a given substring contains exactly K 1's?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template