📌  相关文章
📜  获取给定二进制字符串所需的最少操作数

📅  最后修改于: 2021-09-06 06:44:00             🧑  作者: Mango

给定一个长度为N的二进制字符串S ,任务是通过最少的操作次数从一个长度为N的字符串获得S ,比如T ,长度为N ,仅由零组成。每个操作都涉及从字符串S 中选择任何索引i并翻转字符串T 的索引[i, N – 1]处的所有位。
例子:

方法:
这个想法是在给定的字符串S 中找到第一次出现的1并在该索引处执行给定的操作。在此步骤之后,对于特定索引处ST字符的每个不匹配,重复该操作。
请按照以下步骤操作:

  1. 迭代S并标记1的第一次出现。
  2. 初始化两个变量,比如lastans ,其中last存储执行最后操作的字符( 0 或 1 ),并且ans保持所需的最小步骤数的计数。
  3. 从第一次出现的1迭代到字符串。
  4. 如果当前字符为0last = 1 ,则增加ans的计数,并设置last = 0
  5. 否则,如果last = 0且当前字符为1 ,则设置last = 1
  6. 返回ans的最终值。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the minimum
// number of operations required
// to obtain the string s
int minOperations(string s)
{
    int n = s.size();
    int pos = -1;
 
    // Iterate the string s
    for (int i = 0; i < s.size(); i++) {
 
        // If first occurrence of 1
        // is found
        if (s[i] == '1') {
 
            // Mark the index
            pos = i;
            break;
        }
    }
 
    // Base case: If no 1 occurred
    if (pos == -1) {
 
        // No operations required
        return 0;
    }
 
    // Stores the character
    // for which last operation
    // was performed
    int last = 1;
 
    // Stores minimum number
    // of operations
    int ans = 1;
 
    // Iterate from pos to n
    for (int i = pos + 1; i < n; i++) {
 
        // Check if s[i] is 0
        if (s[i] == '0') {
 
            // Check if last operation was
            // performed because of 1
            if (last == 1) {
                ans++;
 
                // Set last to 0
                last = 0;
            }
        }
        else {
 
            if (last == 0) {
 
                // Check if last operation was
                // performed because of 0
                ans++;
 
                // Set last to 1
                last = 1;
            }
        }
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
    string s = "10111";
 
    cout << minOperations(s);
    return 0;
}


Java
// Java program to implement the
// above approach
import java.util.*;
 
class GFG{
 
// Function to find the minimum
// number of operations required
// to obtain the string s
static int minOperations(String s)
{
    int n = s.length();
    int pos = -1;
 
    // Iterate the string s
    for(int i = 0; i < s.length(); i++)
    {
         
        // If first occurrence of 1
        // is found
        if (s.charAt(i) == '1')
        {
             
            // Mark the index
            pos = i;
            break;
        }
    }
 
    // Base case: If no 1 occurred
    if (pos == -1)
    {
 
        // No operations required
        return 0;
    }
 
    // Stores the character
    // for which last operation
    // was performed
    int last = 1;
 
    // Stores minimum number
    // of operations
    int ans = 1;
 
    // Iterate from pos to n
    for(int i = pos + 1; i < n; i++)
    {
         
        // Check if s[i] is 0
        if (s.charAt(i) == '0')
        {
 
            // Check if last operation was
            // performed because of 1
            if (last == 1)
            {
                ans++;
 
                // Set last to 0
                last = 0;
            }
        }
        else
        {
            if (last == 0)
            {
 
                // Check if last operation was
                // performed because of 0
                ans++;
 
                // Set last to 1
                last = 1;
            }
        }
    }
     
    // Return the answer
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    String s = "10111";
     
    System.out.println(minOperations(s));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to implement
# the above approach
 
# Function to find the minimum
# number of operations required
# to obtain the string s
def minOperations(s):
 
    n = len(s)
    pos = -1
 
    # Iterate the string s
    for i in range(len(s)):
 
        # If first occurrence of 1
        # is found
        if (s[i] == '1'):
 
            # Mark the index
            pos = i
            break
 
    # Base case: If no 1 occurred
    if (pos == -1):
 
        # No operations required
        return 0
 
    # Stores the character
    # for which last operation
    # was performed
    last = 1
 
    # Stores minimum number
    # of operations
    ans = 1
 
    # Iterate from pos to n
    for i in range(pos + 1, n):
 
        # Check if s[i] is 0
        if (s[i] == '0'):
 
            # Check if last operation was
            # performed because of 1
            if (last == 1):
                ans += 1
 
                # Set last to 0
                last = 0
         
        else:
 
            if (last == 0):
 
                # Check if last operation was
                # performed because of 0
                ans += 1
 
                # Set last to 1
                last = 1
 
    # Return the answer
    return ans
 
# Driver Code
if __name__ == "__main__":
     
    s = "10111"
 
    print(minOperations(s))
 
# This code is contributed by chitranayal


C#
// C# program to implement the
// above approach
using System;
class GFG{
  
// Function to find the minimum
// number of operations required
// to obtain the string s
static int minOperations(String s)
{
    int n = s.Length;
    int pos = -1;
  
    // Iterate the string s
    for(int i = 0; i < s.Length; i++)
    {
          
        // If first occurrence of 1
        // is found
        if (s[i] == '1')
        {
              
            // Mark the index
            pos = i;
            break;
        }
    }
  
    // Base case: If no 1 occurred
    if (pos == -1)
    {
  
        // No operations required
        return 0;
    }
  
    // Stores the character
    // for which last operation
    // was performed
    int last = 1;
  
    // Stores minimum number
    // of operations
    int ans = 1;
  
    // Iterate from pos to n
    for(int i = pos + 1; i < n; i++)
    {
          
        // Check if s[i] is 0
        if (s[i] == '0')
        {
  
            // Check if last operation was
            // performed because of 1
            if (last == 1)
            {
                ans++;
  
                // Set last to 0
                last = 0;
            }
        }
        else
        {
            if (last == 0)
            {
  
                // Check if last operation was
                // performed because of 0
                ans++;
  
                // Set last to 1
                last = 1;
            }
        }
    }
      
    // Return the answer
    return ans;
}
  
// Driver code
public static void Main(string[] args)
{
    String s = "10111";
      
    Console.Write(minOperations(s));
}
}
  
// This code is contributed by rock_cool


Javascript


输出:
3

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

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