-
Notifications
You must be signed in to change notification settings - Fork 0
/
GFG Job Sequencing Problem.txt
45 lines (42 loc) · 1.13 KB
/
GFG Job Sequencing Problem.txt
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
class Solution
{
//Function to find the maximum profit and the number of jobs done.
int[] JobScheduling(Job arr[], int n)
{
// Your code here
int lastTime = 0;
for(Job j: arr)
lastTime = Math.max(lastTime, j.deadline);
Arrays.sort(arr, (a,b) -> b.profit-a.profit);
int[] result = new int[lastTime];
int jobProfit = 0;
int countJobs = 0;
for(int i=0; i<n; i++)
{
for( int j=arr[i].deadline-1; j>=0; j--)
{
if(result[j]==0)
{
result[j] = arr[i].id;
countJobs++;
jobProfit += arr[i].profit;
break;
}
}
}
// for(int res: result)
// System.out.print(res+" -> ");
// System.out.println();
return new int[]{countJobs, jobProfit};
}
} // TC: O(n + nlog + n*m) SC: O(m)
/*
class Job {
int id, profit, deadline;
Job(int x, int y, int z){
this.id = x;
this.deadline = y;
this.profit = z;
}
}
*/