📜  填充给定N个插槽所需的最短时间

📅  最后修改于: 2021-05-06 19:14:02             🧑  作者: Mango

给定一个表示时隙数的整数N ,以及一个数组arr [] ,该数组arr [][1,N]范围内的K个整数组成。数组的每个元素都在[1,N]范围内,该范围代表已填充插槽的索引。在每个时间单位,带有填充槽的索引将填充相邻的空槽。任务是找到填充所有N个插槽所需的最短时间。

例子:

方法:为了解决给定的问题,想法是执行级别顺序遍历 在给定的N个插槽中使用队列。请按照以下步骤解决问题:

  • 初始化一个变量,例如time0 ,并初始化一个辅助数组Visited [],以在每次迭代中标记填充的索引。
  • 现在,将队列arr []中给定的已填充插槽的索引推送到队列中 并将其标记为已访问。
  • 现在,迭代直到队列不为空,然后执行以下步骤:
    • 从队列中删除前端索引i ,如果相邻的插槽(i – 1)(i + 1)[1,N]范围内并且未被访问,则将它们标记为已访问并将它们推入队列。
    • 时间增加1
  • 完成上述步骤后,将(time – 1)的值打印为填充所有插槽所需的最短时间。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
 
using namespace std;
 
// Function to return the minimum
// time to fill all the slots
void minTime(vector arr, int N, int K)
{
     
    // Stores visited slots
    queue q;
     
    // Checks if a slot is visited or not
    vector vis(N + 1, false);
 
    int time = 0;
 
    // Insert all filled slots
    for (int i = 0; i < K; i++) {
 
        q.push(arr[i]);
        vis[arr[i]] = true;
    }
 
    // Iterate until queue is
    // not empty
    while (q.size() > 0) {
 
        // Iterate through all slots
        // in the queue
        for (int i = 0; i < q.size(); i++) {
 
            // Front index
            int curr = q.front();
            q.pop();
 
            // If previous slot is
            // present and not visited
            if (curr - 1 >= 1 &&
                !vis[curr - 1]) {
                vis[curr - 1] = true;
                q.push(curr - 1);
            }
 
            // If next slot is present
            // and not visited
            if (curr + 1 <= N &&
                !vis[curr + 1]) {
 
                vis[curr + 1] = true;
                q.push(curr + 1);
            }
        }
 
        // Increment the time
        // at each level
        time++;
    }
 
    // Print the answer
    cout << (time - 1);
}
 
// Driver Code
int main()
{
    int N = 6;
    vector arr = { 2, 6 };
    int K = arr.size();
 
    // Function Call
    minTime(arr, N, K);
}


Java
// Java program for the above approach
 
import java.io.*;
import java.util.*;
class GFG {
 
    // Function to return the minimum
    // time to fill all the slots
    public static void minTime(int arr[],
                               int N, int K)
    {
         
        // Stores visited slots
        Queue q = new LinkedList<>();
 
        // Checks if a slot is visited or not
        boolean vis[] = new boolean[N + 1];
        int time = 0;
 
        // Insert all filled slots
        for (int i = 0; i < K; i++) {
 
            q.add(arr[i]);
            vis[arr[i]] = true;
        }
 
        // Iterate until queue is
        // not empty
        while (q.size() > 0) {
 
            // Iterate through all slots
            // in the queue
            for (int i = 0; i < q.size(); i++) {
 
                // Front index
                int curr = q.poll();
 
                // If previous slot is
                // present and not visited
                if (curr - 1 >= 1 &&
                    !vis[curr - 1]) {
                    vis[curr - 1] = true;
                    q.add(curr - 1);
                }
 
                // If next slot is present
                // and not visited
                if (curr + 1 <= N &&
                    !vis[curr + 1]) {
 
                    vis[curr + 1] = true;
                    q.add(curr + 1);
                }
            }
 
            // Increment the time
            // at each level
            time++;
        }
 
        // Print the answer
        System.out.println(time - 1);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 6;
        int arr[] = { 2, 6 };
        int K = arr.length;
 
        // Function Call
        minTime(arr, N, K);
    }
}


Python3
# Python3 program for the above approach
 
# Function to return the minimum
# time to fill all the slots
def minTime(arr, N, K):
     
    # Stores visited slots
    q = []
     
    # Checks if a slot is visited or not
    vis = [False] * (N + 1)
 
    time = 0
 
    # Insert all filled slots
    for i in range(K):
        q.append(arr[i])
        vis[arr[i]] = True
         
    # Iterate until queue is
    # not empty
    while (len(q) > 0):
         
        # Iterate through all slots
        # in the queue
        for i in range(len(q)):
             
            # Front index
            curr = q[0]
            q.pop(0)
 
            # If previous slot is
            # present and not visited
            if (curr - 1 >= 1 and vis[curr - 1] == 0):
                vis[curr - 1] = True
                q.append(curr - 1)
             
            # If next slot is present
            # and not visited
            if (curr + 1 <= N and vis[curr + 1] == 0):
                vis[curr + 1] = True
                q.append(curr + 1)
             
        # Increment the time
        # at each level
        time += 1
     
    # Print the answer
    print(time - 1)
 
# Driver Code
N = 6
arr = [ 2, 6 ]
K = len(arr)
 
# Function Call
minTime(arr, N, K)
 
# This code is contributed by susmitakundugoaldanga


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
   
// Function to return the minimum
// time to fill all the slots
static void minTime(List arr, int N, int K)
{
     
    // Stores visited slots
    Queue q = new Queue();
     
    // Checks if a slot is visited or not
    int []vis = new int[N + 1];
    Array.Clear(vis, 0, vis.Length);
    int time = 0;
 
    // Insert all filled slots
    for (int i = 0; i < K; i++)
    {
        q.Enqueue(arr[i]);
        vis[arr[i]] = 1;
    }
 
    // Iterate until queue is
    // not empty
    while (q.Count > 0)
    {
 
        // Iterate through all slots
        // in the queue
        for (int i = 0; i < q.Count; i++)
        {
 
            // Front index
            int curr = q.Peek();
            q.Dequeue();
 
            // If previous slot is
            // present and not visited
            if (curr - 1 >= 1 &&
                vis[curr - 1]==0)
            {
                vis[curr - 1] = 1;
                q.Enqueue(curr - 1);
            }
 
            // If next slot is present
            // and not visited
            if (curr + 1 <= N &&
                vis[curr + 1] == 0)
            {
 
                vis[curr + 1] = 1;
                q.Enqueue(curr + 1);
            }
        }
 
        // Increment the time
        // at each level
        time++;
    }
 
    // Print the answer
    Console.WriteLine(time-1);
}
 
// Driver Code
public static void Main()
{
    int N = 6;
    List arr = new List() { 2, 6 };
    int K = arr.Count;
 
    // Function Call
    minTime(arr, N, K);
}
}
 
// THIS CODE IS CONTRIBUTED BY SURENDRA_GANGWAR.


输出:
2

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