查找完成给定进程的处理时间
给定N个进程和两个数组, arr1[] 和 arr2[]每个大小为N。 arr1[]包含任何进程在临界区花费的时间,而arr2[]表示一个进程从临界区出来后完成处理所花费的时间。任务是找出所有进程完成它们的处理所花费的时间(无论是在临界区还是在临界区外),如果以任何顺序处理的话。
Note: No two processes can be using the critical section at the same instant of time but more than one process can be processed at the same instant of time outside the critical section.
例子:
Input: N = 3, arr1[] = {1, 4, 3}, arr2[] = {2, 3, 1}
Output: 9
Explanation:
The 1st process: enters in critical section at time 0.
So after 1 unit time it completes critical section task and takes 2 more unit outside critical section.
So the total time after which 1st process is finished is 3 units.
The 2nd process: After 1 unit of time into the critical section and comes out of critical section after 5th unit.
Then spends 3 unit of time outside the critical section and finally is finished after 8th unit of time.
The 3rd process: After 5 units of time accesses the critical section and comes out after 8th unit of time.
Then spend 1 more unit outside the critical section and is finished after 9 units of time from the starting of all the processes.
So the total time taken is 9
Input: N = 2, arr1[] = {2, 1}, arr2[] = {5, 2}
Output: 7
方法:解决问题的方法是基于排序的概念。按照步骤:
- 将 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)