Check if CPU will process given requests successfully or not
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++
// C++ Program for the above approach #include <algorithm> #include <iostream> #include <vector> using namespace std; // Function to check whether all requests // can be fulfilled or not bool cpuAllocation(vector<vector< int > >& request, int capacity) { vector<pair< int , int > > 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<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
// Java Program for the above approach import java.util.*; class GFG { static class pair implements Comparable<pair> { 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<pair> 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 |
Python3
# Python Program for the above approach class pair: def __init__( self , s, e): self .first = s self .second = e # Function to check whether all requests # can be fulfilled or not def cpuAllocation(request, capacity): v = [] for i in range ( len (request)): # Pushing fromi, # numPassengersi v.append(pair(request[i][ 1 ], request[i][ 0 ])) # Pushing toi, # -numPassengersi 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 # Driver Code request = [[ 2 , 1 , 5 ], [ 3 , 3 , 7 ]] capacity = 5 res = cpuAllocation(request, capacity) if (res = = True ): print ( "TRUE" ) else : print ( "FALSE" ) # This code is contributed by Lovely Jain |
C#
// C# Program for the above approach 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; } } // Function to check whether all requests // can be fulfilled or not static bool cpuAllocation( int [,] request, int capacity) { List<pair> v = new List<pair>(); 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
<script> // Javascript Program for the above approach // Function to check whether all requests // can be fulfilled or not function cpuAllocation(request, capacity) { let v = []; for (let i = 0; i < request.length; i++) { // Pushing fromi, // numPassengersi v.push([request[i][1], request[i][0]]); // Pushing toi, // -numPassengersi 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 ; } // Driver Code 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" ); // This code is contributed by gfgking. </script> |
TRUE
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Please Login to comment...