-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum_CPU_LOAD.cpp
62 lines (46 loc) · 1.54 KB
/
Maximum_CPU_LOAD.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
struct Job
{
int s,e,load;
};
bool startcompare(Job a, Job b)
{
return a.s<b.s;
}
struct endcompare
{
bool operator()(Job a , Job b)
{
return a.e>b.e;
}
};
int MaximumCpuLoad(vector<Job> &Jobs)
{
sort(Jobs.begin(),Jobs.end(),startcompare);
int maxCPUload=0;
int currentCPUload=0;
priority_queue<Job,vector<Job>,endcompare> minheap;
for(auto job: Jobs)
{
while(!minheap.empty() && job.s>minheap.top().e)
{
currentCPUload-=minheap.top().load;
minheap.pop();
}
minheap.push(job);
currentCPUload+=job.load;
maxCPUload=max(maxCPUload,currentCPUload);
}
return maxCPUload;
}
int main()
{
vector<Job> input= {{6,7,10},{8,12,15},{2,4,11}};
cout<<MaximumCpuLoad(input);
}
/* Time complexity #
The time complexity of the above algorithm is O(N*logN), where ‘N’ is the total number of jobs. This is due to the sorting that we did in the beginning. Also, while iterating the jobs, we might need to poll/offer jobs to the priority queue. Each of these operations can take O(logN). Overall our algorithm will take O(NlogN).
Space complexity #
The space complexity of the above algorithm will be O(N)O(N), which is required for sorting. Also, in the worst case, we have to insert all the jobs into the priority queue (when all jobs overlap) which will also take O(N)O(N) space. The overall space complexity of our algorithm is O(N).*/