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

📅  最后修改于: 2021-05-05 02:59:57             🧑  作者: Mango

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

例子:

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

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

  1. 计算字符串S1 s的个数,例如cnt1
  2. 计算子字符串S [0],…,S [cnt1-1] (例如cnt0)中0 s数。
  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


输出:
3




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

高效方法:

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

  • 设置两个指针i = 0j = S.length()– 1,然后迭代直到i超过j
  • 如果S [i]等于’ 0 ‘并且S [j]等于’ 1 ‘,则增加计数
  • 增量I如果S [i]‘1’,直到‘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

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

输出:

2




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