给定一个二进制字符串S ,任务是计算将所有 1 组合在一起所需的最小跳转次数。
例子:
Input: S = “000010011000100”
Output: 5
Explanation:
000010011000100 -> 000000111000100 requires 2 jumps.
000000111000100 -> 000000111100000 requires 3 jumps.
Hence, at least 5 jumps are required to group all 1’s together.
Input: S = “100010001”
Output: 6
Explanation:
100010001 -> 000110001 requires 3 jumps.
000110001 -> 000111000 requires 3 jumps.
方法:
我们可以观察到,为了最大限度地减少将所有 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
Javascript
输出:
10
时间复杂度: O(N)
辅助空间: O(N)