Check if CPU will process given requests successfully or not
Last Updated :
05 May, 2023
Given an integer capacity the maximum number of the processes handled by a CPU at any given time and a 2-D array request[][] is given, each request has three parameters:
- a number of processes requiring CPU,
- start time,
- finish time.
The task is to check if the CPU will entertain all the requests successfully or not. If yes return TRUE else FALSE. The start time of the CPU is 0 initially.
Examples:
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
Approach: The idea is to keep track of CPU utilization at every point in time. If CPU utilization crosses its maximum limit then, stop the program with the FALSE return. Let curr be the number of processes currently in the CPU. So initially curr = 0 for request[i]
- at fromi time curr will be increased by numProcessi
- at toi time curr will be decreased by numProcessi
at any point of time, if curr>capacity, just return false. Follow the steps below to solve the problem:
- Initialize a vector of pair v[].
- Iterate over the range [0, request.size()) using the variable i and perform the following tasks:
- Push the pair {request[i][1], request[i][0]} to the vector v[].
- Push the pair {request[i][2], -request[i][0]} to the vector v[].
- Sort the vector v[].
- Initialize the variable curr as 0.
- Iterate over the range [0, v.size()) using the variable i and perform the following tasks:
- Add v[i].second to the variable curr.
- If curr is greater than capacity, then return false.
- After performing the above steps, return true as the answer.
Below is the implementation of the above approach:
C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool cpuAllocation(vector<vector< int > >& request,
int capacity)
{
vector<pair< int , int > > v;
for ( int i = 0; i < request.size(); i++) {
v.push_back({ request[i][1],
request[i][0] });
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 ;
}
int main()
{
vector<vector< int > > 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
import java.util.*;
class GFG {
static class pair implements Comparable<pair> {
int first, second;
pair( int s, int e) {
first = s;
second = e;
}
public int compareTo(pair a) {
return this .first > a.first ? 1 : 0 ;
}
}
static boolean cpuAllocation( int [][] request, int capacity) {
Vector<pair> v = new Vector<>();
for ( int i = 0 ; i < request.length; i++) {
v.add( new pair(request[i][ 1 ], request[i][ 0 ]));
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 ;
}
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" );
}
}
|
Python3
class pair:
def __init__( self , s, e):
self .first = s
self .second = e
def cpuAllocation(request, capacity):
v = []
for i in range ( len (request)):
v.append(pair(request[i][ 1 ], request[i][ 0 ]))
v.append(pair(request[i][ 2 ], - request[i][ 0 ]))
v.sort(key = lambda x: (x.first, - x.second))
curr = 0
for i in range ( len (v)):
curr + = v[i].second
if (curr > capacity):
return False
return True
request = [[ 2 , 1 , 5 ], [ 3 , 3 , 7 ]]
capacity = 5
res = cpuAllocation(request, capacity)
if (res = = True ):
print ( "TRUE" )
else :
print ( "FALSE" )
|
C#
using System;
using System.Collections.Generic;
public class GFG {
class pair : IComparable<pair>
{
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;
}
}
static bool cpuAllocation( int [,] request, int capacity) {
List<pair> v = new List<pair>();
for ( int i = 0; i < request.GetLength(0); i++) {
v.Add( new pair(request[i,1], request[i,0]));
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 ;
}
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" );
}
}
|
Javascript
<script>
function cpuAllocation(request, capacity)
{
let v = [];
for (let i = 0; i < request.length; i++)
{
v.push([request[i][1], request[i][0]]);
v.push([request[i][2], -request[i][0]]);
}
v.sort((a, b) => a[0] - b[0])
curr = 0;
for (let i = 0; i < v.length; i++) {
curr += v[i][1];
if (curr > capacity)
return false ;
}
return true ;
}
let request = [[2, 1, 5], [3, 3, 7]]
let capacity = 5;
let res = cpuAllocation(request, capacity);
if (res == true )
document.write( "TRUE" );
else
document.write( "FALSE" );
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...