给定二进制字符串中所有 0 到 1 之间的最短距离之和
给定一个二进制字符串S ,任务是找到给定字符串S中所有0到1之间的最短距离之和。
例子:
Input: S = “100100”
Output: 5
Explanation:
For the ‘0’ at index 1 the nearest ‘1’ is at index 0 at a distance 1.
For the ‘0’ at index 2 the nearest ‘1’ is at index 3 at a distance 1.
For the ‘0’ at index 4 the nearest ‘1’ is at index 3 at a distance 1.
For the ‘0’ at index 5 the nearest ‘1’ is at index 3 at a distance 2.
Therefore the sum of the distances is 1 + 1 + 1 + 2 = 5.
Input: S = “1111”
Output: 0
方法:给定的问题可以通过使用贪心方法来解决。这个想法是为从左侧最靠近它的每个' 0'搜索' 1' 。类似地,为从右侧最靠近它的每个“0”搜索“ 1” 。最后,根据左右两边的计算值,计算任意' 0'到' 1'的距离之和。请按照以下步骤解决给定的问题。
- 初始化数组prefixDistance(N) , suffixDistance(N)以分别存储从左到右的距离。
- 初始化一个变量,例如cnt = 0 ,它存储任何 ' 0'到最近的 ' 1'之间的距离。
- 初始化一个变量,比如haveOne = false ,以标记字符' 1' 。
- 初始化一个变量,比如sum = 0 ,它存储所有 ' 0'到最接近的 ' 1'之间的总和。
- 使用变量i在范围[0, N – 1]上进行迭代,执行以下步骤:
- 如果S[i]的值为“ 1”,则将haveOne指定为true ,将cnt指定为0 ,将 prefixDistance[i]指定为0 。
- 否则,如果haveOne为真,则将cnt的值增加1并将prefixDistance[i]的值分配为cnt 。
- 使用变量i在范围[0, N – 1]上进行迭代,执行以下步骤:
- 如果S[i]的值为“ 1”,则将haveOne指定为true ,将cnt指定为0 ,将 suffixDistance[i]指定为0 。
- 否则,如果haveOne为真,则将cnt的值增加1并将suffixDistance[i]的值分配为cnt 。
- 使用变量i在[0, N – 1]范围内进行迭代,如果S[i]的值为“ 1” ,则将总和更新为sum += min(prefixDistance[i], suffixDistance[i]) 。
- 完成上述步骤后,打印sum的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the total sum of
// the shortest distance between every
// 0 to 1 in a given binary string
void findTotalDistance(string S, int N)
{
// Stores the prefix distance and
// suffix distance from 0 to 1
vector prefixDistance(N);
vector suffixDistance(N);
// Stores the current distance
// from 1 to 0
int cnt = 0;
// Marks the 1
bool haveOne = false;
for (int i = 0; i < N; ++i) {
// If current character is 1
if (S[i] == '1') {
// Mark haveOne to true
haveOne = true;
// Assign the cnt to 0
cnt = 0;
// Assign prefixDistance[i] as 0
prefixDistance[i] = 0;
}
// If haveOne is true
else if (haveOne) {
// Update the cnt
cnt++;
// Update prefixDistance[i]
prefixDistance[i] = cnt;
}
// Assign prefixDistance[i]
// as INT_MAX
else
prefixDistance[i] = INT_MAX;
}
// Assign haveOne as false
haveOne = false;
for (int i = N - 1; i >= 0; --i) {
// If current character is 1
if (S[i] == '1') {
// Mark haveOne to true
haveOne = true;
// Assign the cnt to 0
cnt = 0;
// Assign the suffixDistance[i]
// as 0
suffixDistance[i] = 0;
}
// If haveOne is true
else if (haveOne) {
// Update the cnt
cnt++;
// Update suffixDistance[i]
// as cnt
suffixDistance[i] = cnt;
}
else
// Assign suffixDistance[i]
// as INT_MAX
suffixDistance[i] = INT_MAX;
}
// Stores the total sum of distances
// between 0 to nearest 1
int sum = 0;
for (int i = 0; i < N; ++i) {
// If current character is 0
if (S[i] == '0') {
// Update the value of sum
sum += min(prefixDistance[i],
suffixDistance[i]);
}
}
// Print the value of the sum
cout << sum << endl;
}
// Driver Code
int main()
{
string S = "100100";
int N = S.length();
findTotalDistance(S, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
public class GFG{
// Function to find the total sum of
// the shortest distance between every
// 0 to 1 in a given binary string
static void findTotalDistance(String S, int N)
{
// Stores the prefix distance and
// suffix distance from 0 to 1
int []prefixDistance = new int[N];
int []suffixDistance = new int[N];
// Stores the current distance
// from 1 to 0
int cnt = 0;
// Marks the 1
boolean haveOne = false;
for (int i = 0; i < N; ++i) {
// If current character is 1
if (S.charAt(i) == '1') {
// Mark haveOne to true
haveOne = true;
// Assign the cnt to 0
cnt = 0;
// Assign prefixDistance[i] as 0
prefixDistance[i] = 0;
}
// If haveOne is true
else if (haveOne) {
// Update the cnt
cnt++;
// Update prefixDistance[i]
prefixDistance[i] = cnt;
}
// Assign prefixDistance[i]
// as INT_MAX
else
prefixDistance[i] = Integer.MAX_VALUE;
}
// Assign haveOne as false
haveOne = false;
for (int i = N - 1; i >= 0; --i) {
// If current character is 1
if (S.charAt(i) == '1') {
// Mark haveOne to true
haveOne = true;
// Assign the cnt to 0
cnt = 0;
// Assign the suffixDistance[i]
// as 0
suffixDistance[i] = 0;
}
// If haveOne is true
else if (haveOne) {
// Update the cnt
cnt++;
// Update suffixDistance[i]
// as cnt
suffixDistance[i] = cnt;
}
else
// Assign suffixDistance[i]
// as INT_MAX
suffixDistance[i] = Integer.MAX_VALUE;
}
// Stores the total sum of distances
// between 0 to nearest 1
int sum = 0;
for (int i = 0; i < N; ++i) {
// If current character is 0
if (S.charAt(i) == '0') {
// Update the value of sum
sum += Math.min(prefixDistance[i],
suffixDistance[i]);
}
}
// Print the value of the sum
System.out.print(sum);
}
// Driver Code
public static void main(String []args)
{
String S = "100100";
int N = S.length();
findTotalDistance(S, N);
}
}
// This code is contributed by AnkThon
Python3
# python program for the above approach
# Function to find the total sum of
# the shortest distance between every
# 0 to 1 in a given binary string
INT_MAX = 2147483647
def findTotalDistance(S, N):
# Stores the prefix distance and
# suffix distance from 0 to 1
prefixDistance = [0 for _ in range(N)]
suffixDistance = [0 for _ in range(N)]
# Stores the current distance
# from 1 to 0
cnt = 0
# Marks the 1
haveOne = False
for i in range(0, N):
# If current character is 1
if (S[i] == '1'):
# // Mark haveOne to true
haveOne = True
# Assign the cnt to 0
cnt = 0
# Assign prefixDistance[i] as 0
prefixDistance[i] = 0
# If haveOne is true
elif (haveOne):
# Update the cnt
cnt = cnt + 1
# Update prefixDistance[i]
prefixDistance[i] = cnt
# Assign prefixDistance[i]
# as INT_MAX
else:
prefixDistance[i] = INT_MAX
# Assign haveOne as false
haveOne = False
for i in range(N-1, -1, -1):
# If current character is 1
if (S[i] == '1'):
# Mark haveOne to true
haveOne = True
# Assign the cnt to 0
cnt = 0
# Assign the suffixDistance[i]
# as 0
suffixDistance[i] = 0
# If haveOne is true
elif (haveOne):
# Update the cnt
cnt = cnt + 1
# Update suffixDistance[i]
# as cnt
suffixDistance[i] = cnt
else:
# // Assign suffixDistance[i]
# // as INT_MAX
suffixDistance[i] = INT_MAX
# Stores the total sum of distances
# between 0 to nearest 1
sum = 0
for i in range(0, N):
# If current character is 0
if (S[i] == '0'):
# Update the value of sum
sum += min(prefixDistance[i], suffixDistance[i])
# Print the value of the sum
print(sum)
# Driver Code
if __name__ == "__main__":
S = "100100"
N = len(S)
findTotalDistance(S, N)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the total sum of
// the shortest distance between every
// 0 to 1 in a given binary string
static void findTotalDistance(string S, int N)
{
// Stores the prefix distance and
// suffix distance from 0 to 1
int []prefixDistance = new int[N];
int []suffixDistance = new int[N];
// Stores the current distance
// from 1 to 0
int cnt = 0;
// Marks the 1
bool haveOne = false;
for (int i = 0; i < N; ++i) {
// If current character is 1
if (S[i] == '1') {
// Mark haveOne to true
haveOne = true;
// Assign the cnt to 0
cnt = 0;
// Assign prefixDistance[i] as 0
prefixDistance[i] = 0;
}
// If haveOne is true
else if (haveOne) {
// Update the cnt
cnt++;
// Update prefixDistance[i]
prefixDistance[i] = cnt;
}
// Assign prefixDistance[i]
// as INT_MAX
else
prefixDistance[i] = Int32.MaxValue;
}
// Assign haveOne as false
haveOne = false;
for (int i = N - 1; i >= 0; --i) {
// If current character is 1
if (S[i] == '1') {
// Mark haveOne to true
haveOne = true;
// Assign the cnt to 0
cnt = 0;
// Assign the suffixDistance[i]
// as 0
suffixDistance[i] = 0;
}
// If haveOne is true
else if (haveOne) {
// Update the cnt
cnt++;
// Update suffixDistance[i]
// as cnt
suffixDistance[i] = cnt;
}
else
// Assign suffixDistance[i]
// as INT_MAX
suffixDistance[i] = Int32.MaxValue;
}
// Stores the total sum of distances
// between 0 to nearest 1
int sum = 0;
for (int i = 0; i < N; ++i) {
// If current character is 0
if (S[i] == '0') {
// Update the value of sum
sum += Math.Min(prefixDistance[i],
suffixDistance[i]);
}
}
// Print the value of the sum
Console.Write(sum);
}
// Driver Code
public static void Main()
{
string S = "100100";
int N = S.Length;
findTotalDistance(S, N);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
5
时间复杂度: O(N)
辅助空间: O(N)