📜  查找完成给定进程的处理时间

📅  最后修改于: 2022-05-13 01:56:04.874000             🧑  作者: Mango

查找完成给定进程的处理时间

给定N个进程和两个数组, arr1[] 和 arr2[]每个大小为N。 arr1[]包含任何进程在临界区花费的时间,而arr2[]表示一个进程从临界区出来后完成处理所花费的时间。任务是找出所有进程完成它们的处理所花费的时间(无论是在临界区还是在临界区外),如果以任何顺序处理的话。

例子:

方法:解决问题的方法是基于排序的概念。按照步骤:

  • 将 arr1[i] 和 arr2[i] 存储在一个列表中并应用排序。
  • 根据 arr2[i] 排序。
  • 维护一个变量,该变量存储进程完成处理所需的最长时间。

下面是上述方法的实现。

C++
// C++ program to implement the approach
#include 
using namespace std;
 
// Comparator for sorting the vector of pair
static bool comp(pair p1,
                 pair p2)
{
    return p1.second > p2.second;
}
 
// Function to find the total time taken
int solution(int arr1[], int arr2[], int N)
{
    vector > v;
 
    // Store all the arr1[i] and arr2[i]
    // as pair in vector
    for (int i = 0; i < N; i++)
        v.push_back({ arr1[i], arr2[i] });
 
    // Sort based on time spent
    // outside critical section (arr2[])
    sort(v.begin(), v.end(), comp);
    int geek = 0, ans = 0;
 
    // Loop to calculate total time
    for (int i = 0; i < N; i++) {
        geek += v[i].first;
        ans = max(ans, geek + v[i].second);
    }
    return ans;
}
 
// Driver code
int main()
{
    int arr1[] = { 1, 4, 3 };
    int arr2[] = { 2, 3, 1 };
    int N = sizeof(arr1) / sizeof(arr1[0]);
 
    // Function call
    cout << solution(arr1, arr2, N);
    return 0;
}


Java
// Java program to implement the approach
import java.util.*;
 
class GFG{
 
// Comparator for sorting the vector of pair
    static class pair implements Comparable
    {
        int first,second;
        pair(int s, int e)
        {
            first = s;
            second = e;
        }
          
        public int compareTo(pair p)
        {
            return p.second - this.second;
        }
    }
 
// Function to find the total time taken
static int solution(int arr1[], int arr2[], int N)
{
    Vector v = new Vector();
 
    // Store all the arr1[i] and arr2[i]
    // as pair in vector
    for (int i = 0; i < N; i++)
        v.add(new pair( arr1[i], arr2[i] ));
 
    // Sort based on time spent
    // outside critical section (arr2[])
    Collections.sort(v);
    int geek = 0, ans = 0;
 
    // Loop to calculate total time
    for (int i = 0; i < N; i++) {
        geek += v.get(i).first;
        ans = Math.max(ans, geek + v.get(i).second);
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int arr1[] = { 1, 4, 3 };
    int arr2[] = { 2, 3, 1 };
    int N = arr1.length;
 
    // Function call
    System.out.print(solution(arr1, arr2, N));
}
}
 
// This code is contributed by 29AjayKumar


C#
// C# program to implement the approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
  // Comparator for sorting the vector of pair
  public class pair
  {
    public int first,second;
    public  pair(int s, int e)
    {
      this.first = s;
      this.second = e;
    }
 
 
  }
 
  // Function to find the total time taken
  static int solution(int []arr1, int []arr2, int N)
  {
    List v = new List();
 
    // Store all the arr1[i] and arr2[i]
    // as pair in vector
    for (int i = 0; i < N; i++)
      v.Add(new pair( arr1[i], arr2[i] ));
 
    // Sort based on time spent
    // outside critical section (arr2[])
    v.Sort((p1,p2)=>p2.second-p1.second);
    int geek = 0, ans = 0;
 
    // Loop to calculate total time
    for (int i = 0; i < N; i++) {
      geek += v[i].first;
      ans = Math.Max(ans, geek + v[i].second);
    }
    return ans;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int []arr1 = { 1, 4, 3 };
    int []arr2 = { 2, 3, 1 };
    int N = arr1.Length;
 
    // Function call
    Console.Write(solution(arr1, arr2, N));
  }
}
 
// This code is contributed by Rajput-Ji


Javascript



输出
9

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