📜  覆盖二进制数组所需的最短时间

📅  最后修改于: 2021-05-17 20:05:49             🧑  作者: Mango

给定一个整数N ,该整数N表示最初用0填充的布尔数组的大小,还给定一个大小为K数组arr [] ,该数组arr []由(基于1的)索引组成,在布尔数组中存在’1’。现在,在一个单位时间,布尔数组中arr [i]的相邻单元变为1,即010变为111 。找到将整个数组转换为1s所需的最短时间。
例子:

方法:
为了解决上述问题,我们必须观察到,我们需要找到零的最长段,直到出现1。例如,对于二进制数00010000010000,最长的段0s在第4到第10位置。现在,观察到索引之间有5个0,这是一个奇数。因此,我们可以得出结论,要覆盖5个零,我们需要5/2 + 1(即3个时间单位),因为所有其他段都将被填充为小于或等于3个时间单位。

  • 如果最长的零段是奇数,则可以得出结论,需要x / 2 +1个时间单位,其中x是最长段中的0数。
  • 如果最长的零段是偶数,那么我们可以得出结论,需要x / 2个时间单位,其中x是最长段中的0数。

我们可以计算最大长度的连续段,直到布尔数组中出现1,然后根据长度是奇数还是偶数返回答案。
下面是上述方法的实现:

C++
// CPP implementation to find the
// Minimum time required to cover a Binary Array
#include 
using namespace std;
  
// function to calculate the time
int solve(vector arr, int n)
{
  
    int k = arr.size();
  
    // Map to mark or store the binary values
    bool mp[n + 2];
  
    // Firstly fill the boolean
    // array with all zeroes
    for (int i = 0; i <= n; i++) {
        mp[i] = 0;
    }
  
    // Mark the 1s
    for (int i = 0; i < k; i++) {
        mp[arr[i]] = 1;
    }
  
    // Number of 0s until first '1' occurs
    int leftSegment = arr[0] - 1;
  
    // Maximum Number of 0s in between 2 '1's.
    for (int i = 1; i < k; i++) {
        leftSegment = max(leftSegment, arr[i] - arr[i - 1] - 1);
    }
  
    // Number of 0s from right until first '1' occurs
    int rightSegment = n - arr[k - 1];
  
    // Return maximum from left and right segment
    int maxSegment = max(leftSegment, rightSegment);
  
    int tim;
  
    // check if count is odd
    if (maxSegment & 1)
        tim = (maxSegment / 2) + 1;
  
    // check ifcount is even
    else
        tim = maxSegment / 2;
  
    // return the time
    return tim;
}
  
// driver code
int main()
{
    // initialise N
    int N = 5;
  
    // array initialisation
    vector arr = { 1, 4 };
  
    cout << solve(arr, N);
}


Java
// Java implementation to find the 
// Minimum time required to cover a Binary Array 
class GFG {
  
    // function to calculate the time 
    static int solve(int []arr, int n) 
    { 
      
        int k = arr.length; 
      
        // Map to mark or store the binary values 
        int mp[] = new int[n + 2]; 
      
        // Firstly fill the boolean 
        // array with all zeroes 
        for (int i = 0; i <= n; i++) { 
            mp[i] = 0; 
        } 
      
        // Mark the 1s 
        for (int i = 0; i < k; i++) { 
            mp[arr[i]] = 1; 
        } 
      
        // Number of 0s until first '1' occurs 
        int leftSegment = arr[0] - 1; 
      
        // Maximum Number of 0s in between 2 '1's. 
        for (int i = 1; i < k; i++) { 
            leftSegment = Math.max(leftSegment, arr[i] - arr[i - 1] - 1); 
        } 
      
        // Number of 0s from right until first '1' occurs 
        int rightSegment = n - arr[k - 1]; 
      
        // Return maximum from left and right segment 
        int maxSegment = Math.max(leftSegment, rightSegment); 
      
        int tim; 
      
        // check if count is odd 
        if ((maxSegment & 1) == 1) 
            tim = (maxSegment / 2) + 1; 
      
        // check ifcount is even 
        else
            tim = maxSegment / 2; 
      
        // return the time 
        return tim; 
    } 
      
    // driver code 
    public static void main (String[] args)
    { 
        // initialise N 
        int N = 5; 
      
        // array initialisation 
        int arr[] = { 1, 4 }; 
      
        System.out.println(solve(arr, N)); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation to find the 
# Minimum time required to cover a Binary Array 
  
# function to calculate the time 
def solve(arr, n) :
  
    k = len(arr) 
  
    # Map to mark or store the binary values
    # Firstly fill the boolean 
    # array with all zeroes 
    mp = [False for i in range(n + 2)] 
  
    # Mark the 1s 
    for i in range(k) :
        mp[arr[i]] = True
  
    # Number of 0s until first '1' occurs 
    leftSegment = arr[0] - 1
  
    # Maximum Number of 0s in between 2 '1's. 
    for i in range(1,k) :
        leftSegment = max(leftSegment, arr[i] - arr[i - 1] - 1)
  
    # Number of 0s from right until first '1' occurs 
    rightSegment = n - arr[k - 1]
  
    # Return maximum from left and right segment 
    maxSegment = max(leftSegment, rightSegment); 
  
    tim = 0
  
    # check if count is odd 
    if (maxSegment & 1) :
        tim = (maxSegment // 2) + 1
  
    # check ifcount is even 
    else :
        tim = maxSegment // 2
  
    # return the time 
    return tim
  
# Driver code 
# initialise N 
N = 5
  
# array initialisation 
arr = [ 1, 4 ] 
  
print(solve(arr, N))
  
# This code is contributed by Sanjit_Prasad


C#
// C# implementation to find the 
// Minimum time required to cover
// a Binary Array 
using System;
  
class GFG{
  
// Function to calculate the time 
static int solve(int[] arr, int n) 
{ 
    int k = arr.Length; 
  
    // Map to mark or store the binary values 
    int[] mp = new int[n + 2]; 
  
    // Firstly fill the boolean 
    // array with all zeroes 
    for(int i = 0; i <= n; i++) 
    { 
        mp[i] = 0; 
    } 
  
    // Mark the 1s 
    for(int i = 0; i < k; i++) 
    { 
        mp[arr[i]] = 1; 
    } 
  
    // Number of 0s until first '1' occurs 
    int leftSegment = arr[0] - 1; 
  
    // Maximum Number of 0s in between 2 '1's. 
    for(int i = 1; i < k; i++)
    { 
        leftSegment = Math.Max(leftSegment, 
                               arr[i] - 
                               arr[i - 1] - 1); 
    } 
  
    // Number of 0s from right until first '1' occurs 
    int rightSegment = n - arr[k - 1]; 
  
    // Return maximum from left and right segment 
    int maxSegment = Math.Max(leftSegment,
                              rightSegment); 
  
    int tim; 
  
    // Check if count is odd 
    if ((maxSegment & 1) == 1) 
        tim = (maxSegment / 2) + 1; 
  
    // Check ifcount is even 
    else
        tim = maxSegment / 2; 
  
    // Return the time 
    return tim; 
} 
  
// Driver code 
public static void Main ()
{ 
      
    // Initialise N 
    int N = 5; 
  
    // Array initialisation 
    int[] arr = { 1, 4 }; 
  
    Console.Write(solve(arr, N)); 
} 
}
  
// This code is contributed by chitranayal


输出:
1