检查 CPU 是否会成功处理给定的请求
给定一个整数容量,一个 CPU 在任何给定时间处理的最大进程数和一个二维数组request[][] ,每个请求都有三个参数:
- 一些需要 CPU 的进程,
- 开始时间,
- 完成时间。
任务是检查 CPU 是否会成功处理所有请求。如果是,则返回 TRUE,否则返回 FALSE。 CPU 的启动时间最初为 0。
例子:
Input: request[][] = {{2, 1, 5}, {3, 3, 7}}, capacity = 4
Output: false
Explanation: First request says that 2 processes need CPU at time=1 and will release back at time=5. CPU servicing 2 process at time=1. Second request comes and asks CPU to service 3 more processes at time=3 to time=7. At time=3, CPU is busy with request-1 and hence can’t service request-2. So, return FALSE.
Input: request[][] = {{2, 1, 5}, {3, 3, 7}}, capacity = 5
Output: true
方法:这个想法是在每个时间点跟踪 CPU 利用率。如果 CPU 利用率超过其最大限制,则以 FALSE 返回停止程序。令curr为当前在 CPU 中的进程数。所以最初curr = 0 for request[i]
- at fromi time curr will be increased by numProcessi
- at toi time curr will be decreased by numProcessi
在任何时间点,如果curr>capacity ,则返回false。请按照以下步骤解决问题:
- 初始化对v[] 的向量。
- 使用变量i遍历范围[0, request.size())并执行以下任务:
- 将对{request[i][1], request[i][0]}推送到向量v[]。
- 将对{request[i][2], -request[i][0]}推送到向量v[]。
- 对向量v[] 进行排序。
- 将变量curr初始化为0。
- 使用变量i遍历范围[0, v.size())并执行以下任务:
- 将v[i].second添加到变量curr。
- 如果curr大于capacity ,则返回false。
- 执行上述步骤后,返回true作为答案。
下面是上述方法的实现:
C++
// C++ Program for the above approach
#include
#include
#include
using namespace std;
// Function to check whether all requests
// can be fulfilled or not
bool cpuAllocation(vector >& request,
int capacity)
{
vector > v;
for (int i = 0; i < request.size(); i++) {
// Pushing fromi,
// numPassengersi
v.push_back({ request[i][1],
request[i][0] });
// Pushing toi,
// -numPassengersi
v.push_back({ request[i][2],
-request[i][0] });
}
sort(v.begin(), v.end());
int curr = 0;
for (int i = 0; i < v.size(); i++) {
curr += v[i].second;
if (curr > capacity)
return false;
}
return true;
}
// Driver Code
int main()
{
vector > request{ { 2, 1, 5 },
{ 3, 3, 7 } };
int capacity = 5;
bool res = cpuAllocation(request, capacity);
if (res == true)
cout << "TRUE";
else
cout << "FALSE";
return 0;
}
Java
// Java Program for the above approach
//include
//include
import java.util.*;
class GFG {
static class pair implements Comparable {
int first, second;
pair(int s, int e) {
first = s;
second = e;
}
// Function to sort the vector elements
// ascending for first element
// and if first element equal
// then descending for second element
public int compareTo(pair a) {
return this.first > a.first ? 1 : 0;
}
}
// Function to check whether all requests
// can be fulfilled or not
static boolean cpuAllocation(int[][] request, int capacity) {
Vector v = new Vector<>();
for (int i = 0; i < request.length; i++) {
// Pushing fromi,
// numPassengersi
v.add(new pair(request[i][1], request[i][0]));
// Pushing toi,
// -numPassengersi
v.add(new pair(request[i][2], -request[i][0]));
}
Collections.sort(v);
int curr = 0;
for (int i = 0; i < v.size(); i++) {
curr += v.get(i).second;
if (curr > capacity)
return false;
}
return true;
}
// Driver Code
public static void main(String[] args) {
int[][] request = { { 2, 1, 5 }, { 3, 3, 7 } };
int capacity = 5;
boolean res = cpuAllocation(request, capacity);
if (res == true)
System.out.print("TRUE");
else
System.out.print("FALSE");
}
}
// This code is contributed by 29AjayKumar
C#
// C# Program for the above approach
//include
//include
using System;
using System.Collections.Generic;
public class GFG {
class pair : IComparable
{
public int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
public int CompareTo(pair p)
{
return this.first > p.first ? 1 : 0;
}
}
// Function to check whether all requests
// can be fulfilled or not
static bool cpuAllocation(int[,] request, int capacity) {
List v = new List();
for (int i = 0; i < request.GetLength(0); i++) {
// Pushing fromi,
// numPassengersi
v.Add(new pair(request[i,1], request[i,0]));
// Pushing toi,
// -numPassengersi
v.Add(new pair(request[i,2], -request[i,0]));
}
v.Sort();
int curr = 0;
for (int i = 0; i < v.Count; i++) {
curr += v[i].second;
if (curr > capacity)
return false;
}
return true;
}
// Driver Code
public static void Main(String[] args)
{
int[,] request = { { 2, 1, 5 }, { 3, 3, 7 } };
int capacity = 5;
bool res = cpuAllocation(request, capacity);
if (res == true)
Console.Write("TRUE");
else
Console.Write("FALSE");
}
}
// This code is contributed by shikhasingrajput
Javascript
TRUE
时间复杂度: O(N*log(N))
辅助空间: O(N)