📌  相关文章
📜  最大化二进制字符串所需的最少操作数

📅  最后修改于: 2021-10-26 02:29:28             🧑  作者: Mango

给定一个二进制字符串S ,任务是找到使S表示的值最大化所需执行的最小交换次数。

例子:

天真的方法:
可以观察到,为了最大化所需的字符串,应该交换字符,以便所有0都在右侧,所有1都在左侧。因此,字符串需要修改为“ 1…10…0 ”序列。

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

  1. 计算字符串S1的数量,比如cnt1
  2. 计算子串S[0], …, S[cnt1-1]0的数量,比如cnt0
  3. 打印cnt0作为必需的答案。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the number
// of operations required
int minOperation(string s, int n)
{
 
    // Count of 1's
    int cnt1 = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == '1')
            cnt1++;
    }
 
    // Count of 0's upto (cnt1)-th index
    int cnt0 = 0;
    for (int i = 0; i < cnt1; i++) {
        if (s[i] == '0')
            cnt0++;
    }
 
    // Return the answer
    return cnt0;
}
 
// Driver Code
int main()
{
    int n = 8;
 
    string s = "01001011";
 
    int ans = minOperation(s, n);
 
    cout << ans << endl;
}


Java
// Java program to implement
// the above approach
class GFG{
 
// Function to find the number
// of operations required
static int minOperation(String s, int n)
{
     
    // Count of 1's
    int cnt1 = 0;
    for(int i = 0; i < n; i++)
    {
        if (s.charAt(i) == '1')
            cnt1++;
    }
 
    // Count of 0's upto (cnt1)-th index
    int cnt0 = 0;
    for(int i = 0; i < cnt1; i++)
    {
        if (s.charAt(i) == '0')
            cnt0++;
    }
 
    // Return the answer
    return cnt0;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 8;
 
    String s = "01001011";
 
    int ans = minOperation(s, n);
 
    System.out.print(ans + "\n");
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to implement
# the above approach
 
# Function to find the number
# of operations required
def minOperation(s, n):
     
    # Count of 1's
    cnt1 = 0;
     
    for i in range(n):
        if (ord(s[i]) == ord('1')):
            cnt1 += 1;
 
    # Count of 0's upto (cnt1)-th index
    cnt0 = 0;
     
    for i in range(0, cnt1):
        if (s[i] == '0'):
            cnt0 += 1;
 
    # Return the answer
    return cnt0;
 
# Driver Code
if __name__ == '__main__':
     
    n = 8;
    s = "01001011";
    ans = minOperation(s, n);
 
    print(ans);
 
# This code is contributed by Amit Katiyar


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to find the number
// of operations required
static int minOperation(String s, int n)
{
     
    // Count of 1's
    int cnt1 = 0;
    for(int i = 0; i < n; i++)
    {
        if (s[i] == '1')
            cnt1++;
    }
 
    // Count of 0's upto (cnt1)-th index
    int cnt0 = 0;
    for(int i = 0; i < cnt1; i++)
    {
        if (s[i] == '0')
            cnt0++;
    }
 
    // Return the answer
    return cnt0;
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 8;
 
    String s = "01001011";
 
    int ans = minOperation(s, n);
 
    Console.Write(ans + "\n");
}
}
 
// This code is contributed by PrinciRaj1992


C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the number
// of operations required
int minOperation(string s, int n)
{
 
    int ans = 0;
    int i = 0, j = n - 1;
    while (i < j) {
 
        // Swap 0's and 1's
        if (s[i] == '0' && s[j] == '1') {
            ans++;
            i++;
            j--;
            continue;
        }
 
        if (s[i] == '1') {
            i++;
        }
 
        if (s[j] == '0') {
            j--;
        }
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
    int n = 8;
 
    string s = "10100101";
 
    int ans = minOperation(s, n);
 
    cout << ans << endl;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find the number
// of operations required
static int minOperation(String s, int n)
{
    int ans = 0;
    int i = 0, j = n - 1;
     
    while (i < j)
    {
         
        // Swap 0's and 1's
        if (s.charAt(i) == '0' &&
            s.charAt(j) == '1')
        {
            ans++;
            i++;
            j--;
            continue;
        }
 
        if (s.charAt(i) == '1')
        {
            i++;
        }
 
        if (s.charAt(j) == '0')
        {
            j--;
        }
    }
 
    // Return the answer
    return ans;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 8;
     
    String s = "10100101";
     
    System.out.println(minOperation(s, n));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to implement
# the above approach
 
# Function to find the number
# of operations required
def minOperation(s, n):
    ans = 0;
    i = 0; j = n - 1;
 
    while (i < j):
 
        # Swap 0's and 1's
        if (s[i] == '0' and s[j] == '1'):
            ans += 1;
            i += 1;
            j -= 1;
            continue;
         
        if (s[i] == '1'):
            i += 1;
         
        if (s[j] == '0'):
            j -= 1;
         
    # Return the answer
    return ans;
 
# Driver code
if __name__ == '__main__':
    n = 8;
 
    s = "10100101";
 
    print(minOperation(s, n));
 
# This code is contributed by sapnasingh4991


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to find the number
// of operations required
static int minOperation(String s, int n)
{
    int ans = 0;
    int i = 0, j = n - 1;
     
    while (i < j)
    {
         
        // Swap 0's and 1's
        if (s[i] == '0' &&
            s[j] == '1')
        {
            ans++;
            i++;
            j--;
            continue;
        }
 
        if (s[i] == '1')
        {
            i++;
        }
 
        if (s[j] == '0')
        {
            j--;
        }
    }
 
    // Return the answer
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 8;
     
    String s = "10100101";
     
    Console.WriteLine(minOperation(s, n));
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
3

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

有效的方法:

可以使用双指针技术进一步优化上述方法。请按照以下步骤解决问题:

  • 设置两个指针i = 0j = S.length() – 1并迭代直到i超过j
  • 如果S[i]等于“ 0 ”且S[j]等于“ 1 ”,则增加计数
  • 如果S[i]是“ 1 ”,则递增i直到出现“ 0 ”。类似地,减少j直到S[j]等于“ 1 ”。
  • 打印计数作为必需的答案。

下面是上述方法的实现:

C++

// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the number
// of operations required
int minOperation(string s, int n)
{
 
    int ans = 0;
    int i = 0, j = n - 1;
    while (i < j) {
 
        // Swap 0's and 1's
        if (s[i] == '0' && s[j] == '1') {
            ans++;
            i++;
            j--;
            continue;
        }
 
        if (s[i] == '1') {
            i++;
        }
 
        if (s[j] == '0') {
            j--;
        }
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
    int n = 8;
 
    string s = "10100101";
 
    int ans = minOperation(s, n);
 
    cout << ans << endl;
}

Java

// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find the number
// of operations required
static int minOperation(String s, int n)
{
    int ans = 0;
    int i = 0, j = n - 1;
     
    while (i < j)
    {
         
        // Swap 0's and 1's
        if (s.charAt(i) == '0' &&
            s.charAt(j) == '1')
        {
            ans++;
            i++;
            j--;
            continue;
        }
 
        if (s.charAt(i) == '1')
        {
            i++;
        }
 
        if (s.charAt(j) == '0')
        {
            j--;
        }
    }
 
    // Return the answer
    return ans;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 8;
     
    String s = "10100101";
     
    System.out.println(minOperation(s, n));
}
}
 
// This code is contributed by offbeat

蟒蛇3

# Python3 program to implement
# the above approach
 
# Function to find the number
# of operations required
def minOperation(s, n):
    ans = 0;
    i = 0; j = n - 1;
 
    while (i < j):
 
        # Swap 0's and 1's
        if (s[i] == '0' and s[j] == '1'):
            ans += 1;
            i += 1;
            j -= 1;
            continue;
         
        if (s[i] == '1'):
            i += 1;
         
        if (s[j] == '0'):
            j -= 1;
         
    # Return the answer
    return ans;
 
# Driver code
if __name__ == '__main__':
    n = 8;
 
    s = "10100101";
 
    print(minOperation(s, n));
 
# This code is contributed by sapnasingh4991

C#

// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to find the number
// of operations required
static int minOperation(String s, int n)
{
    int ans = 0;
    int i = 0, j = n - 1;
     
    while (i < j)
    {
         
        // Swap 0's and 1's
        if (s[i] == '0' &&
            s[j] == '1')
        {
            ans++;
            i++;
            j--;
            continue;
        }
 
        if (s[i] == '1')
        {
            i++;
        }
 
        if (s[j] == '0')
        {
            j--;
        }
    }
 
    // Return the answer
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 8;
     
    String s = "10100101";
     
    Console.WriteLine(minOperation(s, n));
}
}
 
// This code is contributed by sapnasingh4991

Javascript


输出:

2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程