📌  相关文章
📜  在给定的二进制字符串中将所有 1 组合在一起所需的最小跳转

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

给定一个二进制字符串S ,任务是计算将所有 1 组合在一起所需的最小跳转次数。
例子:

方法:
我们可以观察到,为了最大限度地减少将所有 1 组合在一起所需的跳跃次数,它们需要在它们当前位置的中位数附近进行分组。计算中位数和将 1 移动到中位数左侧最近位置0所需的移动次数。对中位数右边执行相同的操作。
下面是上述方法的实现:

C++
// C++ Program to find the minimum
// number of jumps required to
// group all ones together in
// the binary string
#include 
using namespace std;
  
// Function to get the
// minimum jump value
int getMinJumps(string s)
{
    // Store all indices
    // of ones
    vector ones;
  
    int jumps = 0, median = 0, ind = 0;
  
    // Populating one's indices
    for (int i = 0; i < s.length(); i++) {
        if (s[i] == '1')
            ones.push_back(i);
    }
  
    if (ones.size() == 0)
        return jumps;
  
    // Calculate median
    median = ones[ones.size() / 2];
    ind = median;
  
    // Jumps required for 1's
    // to the left of median
    for (int i = ind; i >= 0; i--) {
        if (s[i] == '1') {
            jumps += ind - i;
            ind--;
        }
    }
    ind = median;
  
    // Jumps required for 1's
    // to the right of median
    for (int i = ind; i < s.length(); i++) {
        if (s[i] == '1') {
            jumps += i - ind;
            ind++;
        }
    }
  
    // Return the final answer
    return jumps;
}
  
// Driver Code
int main()
{
    string S = "00100000010011";
    cout << getMinJumps(S) << '\n';
    return 0;
}


Java
// Java Program to find the minimum 
// number of jumps required to 
// group all ones together in 
// the binary string 
import java.io.*; 
import java.util.*; 
  
class GFG{
      
// Function to get the 
// minimum jump value 
public static int getMinJumps(String s) 
{ 
      
    // Store all indices 
    // of ones 
    Vector ones = new Vector();
      
    int jumps = 0, median = 0, ind = 0; 
      
    // Populating one's indices 
    for(int i = 0; i < s.length(); i++) 
    { 
       if (s.charAt(i) == '1') 
           ones.add(i); 
    } 
      
    if (ones.size() == 0) 
        return jumps; 
      
    // Calculate median 
    median = (int)ones.get(ones.size() / 2); 
    ind = median; 
      
    // Jumps required for 1's 
    // to the left of median 
    for(int i = ind; i >= 0; i--) 
    { 
       if (s.charAt(i) == '1') 
       { 
           jumps += ind - i; 
           ind--; 
       } 
    } 
    ind = median; 
      
    // Jumps required for 1's 
    // to the right of median 
    for(int i = ind; i < s.length(); i++) 
    { 
       if (s.charAt(i) == '1')
       { 
           jumps += i - ind; 
           ind++; 
       } 
    } 
      
    // Return the final answer 
    return jumps; 
} 
  
// Driver code
public static void main(String[] args)
{
    String S = "00100000010011"; 
      
    System.out.println(getMinJumps(S));
}
}
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to find the minimum
# number of jumps required to group 
# all ones together in the binary string 
  
# Function to get the
# minimum jump value
def getMinJumps(s):
  
    # Store all indices
    # of ones
    ones = []
  
    jumps, median, ind = 0, 0, 0
  
    # Populating one's indices
    for i in range(len(s)):
        if(s[i] == '1'):
            ones.append(i)
  
    if(len(ones) == 0):
        return jumps
  
    # Calculate median
    median = ones[len(ones) // 2]
    ind = median
  
    # Jumps required for 1's
    # to the left of median
    for i in range(ind, -1, -1):
        if(s[i] == '1'):
            jumps += ind - i
            ind -= 1
  
    ind = median
  
    # Jumps required for 1's
    # to the right of median
    for i in range(ind, len(s)):
        if(s[i] == '1'):
            jumps += i - ind
            ind += 1
  
    # Return the final answer
    return jumps
  
# Driver Code
if __name__ == '__main__':
  
    s = "00100000010011"
      
    print(getMinJumps(s))
  
# This code is contributed by Shivam Singh


C#
// C# program to find the minimum 
// number of jumps required to 
// group all ones together in 
// the binary string 
using System; 
using System.Collections; 
using System.Collections.Generic;
  
class GFG{ 
      
// Function to get the 
// minimum jump value 
public static int getMinJumps(string s) 
{ 
      
    // Store all indices 
    // of ones 
    ArrayList ones = new ArrayList();
      
    int jumps = 0, median = 0, ind = 0; 
      
    // Populating one's indices 
    for(int i = 0; i < s.Length; i++) 
    { 
        if (s[i] == '1') 
            ones.Add(i); 
    } 
      
    if (ones.Count== 0) 
        return jumps; 
      
    // Calculate median 
    median = (int)ones[ones.Count / 2]; 
    ind = median; 
      
    // Jumps required for 1's 
    // to the left of median 
    for(int i = ind; i >= 0; i--) 
    { 
        if (s[i] == '1') 
        { 
            jumps += ind - i; 
            ind--; 
        } 
    } 
    ind = median; 
      
    // Jumps required for 1's 
    // to the right of median 
    for(int i = ind; i < s.Length; i++) 
    { 
        if (s[i] == '1') 
        { 
            jumps += i - ind; 
            ind++; 
        } 
    } 
      
    // Return the final answer 
    return jumps; 
} 
  
// Driver code 
public static void Main(string[] args) 
{ 
    string S = "00100000010011"; 
      
    Console.Write(getMinJumps(S)); 
} 
} 
  
// This code is contributed by rutvik_56


输出:

10

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

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