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

📅  最后修改于: 2021-10-26 02:39:18             🧑  作者: Mango

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

方法:
为了解决上面提到的问题,我们必须观察到我们需要找到最长的零段,直到出现 1。例如,对于二进制数 00010000010000,最长的 0 段是从第 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


Javascript


输出:
1

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程