-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJobScheduling.java
65 lines (53 loc) · 1.72 KB
/
JobScheduling.java
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
63
64
65
/*https://practice.geeksforgeeks.org/problems/job-sequencing-problem-1587115620/1/*/
class ProfitComparator implements Comparator<Job>
{
public int compare(Job j1, Job j2)
{
if (j1.profit < j2.profit) return 1;
if (j1.profit > j2.profit) return -1;
return 0;
}
}
class Solution
{
int[] JobScheduling(Job arr[], int n)
{
//add everything to minheap and find out the maximum deadline possible
int maxDeadline = Integer.MIN_VALUE;
PriorityQueue<Job> pq = new PriorityQueue<Job>(new ProfitComparator());
for (int i = 0; i < n; ++i)
{
pq.add(arr[i]);
if (arr[i].deadline > maxDeadline) maxDeadline = arr[i].deadline;
}
int[] result = new int[2];
int[] timeline = new int[maxDeadline];
//till we have more elements
while (!pq.isEmpty())
{
//remove the root
Job job = pq.poll();
int iterator = job.deadline-1;
//traverse the timeline in reverse direction
while (iterator >= 0)
{
//when a space is found
if (timeline[iterator] == 0)
{
//store the job
timeline[iterator] = job.id;
//increment the count
++result[0];
//increase the profit
result[1] += job.profit;
//stop the loop
break;
}
--iterator;
}
//when the timeline is full, stop the loop
if (result[0] == timeline.length) break;
}
return result;
}
}