📜  根据 B 中的执行顺序查找执行 A 中任务所花费的时间

📅  最后修改于: 2021-09-07 04:31:05             🧑  作者: Mango

给定两个队列AB ,每个队列的大小为N ,任务是根据B中的执行顺序找到执行A 中任务所需的最短时间,其中:

  1. 如果在队列B前面找到的任务在队列A前面,则弹出该任务并执行它。
  2. 如果在队列B前面找到的任务在队列A前面没有找到,则从队列A 中弹出当前任务并将其推送到末尾。
  3. 队列中的 push 和 pop 操作花费一个单位时间,任务的执行是在恒定时间内完成的。

例子

方法:
对于队列A 中的每个任务:

  1. 如果队列 A 的前端任务与队列B的前端任务相同。从两个队列中弹出任务并执行它。将总时间增加一个单位。
  2. 如果队列 A 的前端任务与队列B的前端任务不同。从队列 A 中弹出任务并将其推送到队列 A 的后面,并将总时间增加两个单位。 (1 个用于弹出操作,1 个用于推送操作)
  3. 重复以上步骤,直到队列A中的任务全部执行完毕。

下面是上述方法的实现:

CPP
// C++ program to find the total
// time taken to execute the task
// in given order
  
#include "bits/stdc++.h"
using namespace std;
  
// Function to calculate the
// total time taken to execute
// the given task in original order
int run_tasks(queue& A,
              queue& B)
{
  
    // To find the total time
    // taken for executing
    // the task
    int total_time = 0;
  
    // While A is not empty
    while (!A.empty()) {
  
        // Store the front element of queue A and B
        int x = A.front();
        int y = B.front();
  
        // If the front element of the queue A
        // is equal to the front element of queue B
        // then pop the element from both
        // the queues and execute the task
        // Increment total_time by 1
        if (x == y) {
            A.pop();
            B.pop();
            total_time++;
        }
  
        // If front element of queue A is not equal
        // to front element of queue B then
        // pop the element from queue A &
        // push it at the back of queue A
        // Increment the total_time by 2
        //(1 for push operation and
        // 1 for pop operation)
        else {
            A.pop();
            A.push(x);
            total_time += 2;
        }
    }
  
    // Return the total time taken
    return total_time;
}
  
// Driver Code
int main()
{
    // Given task to be executed
    queue A;
    A.push(3);
    A.push(2);
    A.push(1);
    A.push(4);
  
    // Order in which task need to be
    // executed
    queue B;
    B.push(4);
    B.push(1);
    B.push(3);
    B.push(2);
  
    // Function the returns the total
    // time taken to execute all the task
    cout << run_tasks(A, B);
  
    return 0;
}


Java
// Java program to find the total
// time taken to execute the task
// in given order
import java.util.*;
  
class GFG
{
  
// Function to calculate the
// total time taken to execute
// the given task in original order
static int run_tasks(Queue A,
                    Queue B)
{
  
    // To find the total time
    // taken for executing
    // the task
    int total_time = 0;
  
    // While A is not empty
    while (!A.isEmpty())
    {
  
        // Store the front element of queue A and B
        int x = A.peek();
        int y = B.peek();
  
        // If the front element of the queue A
        // is equal to the front element of queue B
        // then pop the element from both
        // the queues and execute the task
        // Increment total_time by 1
        if (x == y) 
        {
            A.remove();
            B.remove();
            total_time++;
        }
  
        // If front element of queue A is not equal
        // to front element of queue B then
        // pop the element from queue A &
        // push it at the back of queue A
        // Increment the total_time by 2
        //(1 for push operation and
        // 1 for pop operation)
        else
        {
            A.remove();
            A.add(x);
            total_time += 2;
        }
    }
  
    // Return the total time taken
    return total_time;
}
  
// Driver Code
public static void main(String[] args)
{
    // Given task to be executed
    Queue A = new LinkedList();
    A.add(3);
    A.add(2);
    A.add(1);
    A.add(4);
  
    // Order in which task need to be
    // executed
    Queue B = new LinkedList();
    B.add(4);
    B.add(1);
    B.add(3);
    B.add(2);
  
    // Function the returns the total
    // time taken to execute all the task
    System.out.print(run_tasks(A, B));
  
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to find the total
# time taken to execute the task
# in given order
from collections import deque
  
# Function to calculate the
# total time taken to execute
# the given task in original order
def run_tasks(A, B):
  
    # To find the total time
    # taken for executing
    # the task
    total_time = 0
  
    # While A is not empty
    while (len(A) > 0):
  
        # Store the front element of queue A and B
        x = A.popleft()
        y = B.popleft()
  
        # If the front element of the queue A
        # is equal to the front element of queue B
        # then pop the element from both
        # the queues and execute the task
        # Increment total_time by 1
        if (x == y):
            total_time += 1
  
        # If front element of queue A is not equal
        # to front element of queue B then
        # pop the element from queue A &
        # append it at the back of queue A
        # Increment the total_time by 2
        #(1 for append operation and
        # 1 for pop operation)
        else:
            B.appendleft(y)
            A.append(x)
            total_time += 2
  
    # Return the total time taken
    return total_time
  
# Driver Code
if __name__ == '__main__':
  
    # Given task to be executed
    A = deque()
    A.append(3)
    A.append(2)
    A.append(1)
    A.append(4)
  
    # Order in which task need to be
    # executed
    B = deque()
    B.append(4)
    B.append(1)
    B.append(3)
    B.append(2)
  
    # Function the returns the total
    # time taken to execute all the task
    print(run_tasks(A, B))
  
# This code is contributed by mohit kumar 29


C#
// C# program to find the total
// time taken to execute the task
// in given order
using System;
using System.Collections.Generic;
  
class GFG
{
  
// Function to calculate the
// total time taken to execute
// the given task in original order
static int run_tasks(Queue A,
                    Queue B)
{
  
    // To find the total time
    // taken for executing
    // the task
    int total_time = 0;
  
    // While A is not empty
    while (A.Count != 0)
    {
  
        // Store the front element of queue A and B
        int x = A.Peek();
        int y = B.Peek();
  
        // If the front element of the queue A
        // is equal to the front element of queue B
        // then pop the element from both
        // the queues and execute the task
        // Increment total_time by 1
        if (x == y) 
        {
            A.Dequeue();
            B.Dequeue();
            total_time++;
        }
  
        // If front element of queue A is not equal
        // to front element of queue B then
        // pop the element from queue A &
        // push it at the back of queue A
        // Increment the total_time by 2
        //(1 for push operation and
        // 1 for pop operation)
        else
        {
            A.Dequeue();
            A.Enqueue(x);
            total_time += 2;
        }
    }
  
    // Return the total time taken
    return total_time;
}
  
// Driver Code
public static void Main(String[] args)
{
    // Given task to be executed
    Queue A = new Queue();
    A.Enqueue(3);
    A.Enqueue(2);
    A.Enqueue(1);
    A.Enqueue(4);
  
    // Order in which task need to be
    // executed
    Queue B = new Queue();
    B.Enqueue(4);
    B.Enqueue(1);
    B.Enqueue(3);
    B.Enqueue(2);
  
    // Function the returns the total
    // time taken to execute all the task
    Console.Write(run_tasks(A, B));
}
}
  
// This code is contributed by PrinciRaj1992


输出:
14

时间复杂度: O(N 2 ),其中 N 是任务数。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live