-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobScheduling.java
More file actions
89 lines (74 loc) · 2.24 KB
/
Copy pathJobScheduling.java
File metadata and controls
89 lines (74 loc) · 2.24 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*
https://www.geeksforgeeks.org/job-sequencing-using-disjoint-set-union/?ref=lbp
*/
public class JobScheduling {
static class Job {
char id;
int profit;
int deadline;
Job(char a, int d, int p) {
this.id = a;
this.profit = p;
this.deadline = d;
}
@Override
public String toString() {
return "Job{" +
"id=" + id +
", profit=" + profit +
", deadline=" + deadline +
'}';
}
}
static class DisjointSet {
int[] parent;
DisjointSet(int length) {
this.parent = new int[length + 1];
for (var i = 0; i < parent.length; i++) {
parent[i] = i;
}
}
int find(int slot) {
if (slot == parent[slot])
return slot;
return parent[slot] = find(parent[slot]);
}
void merge(int par, int child) {
this.parent[child] = par;
}
}
int maxTimeSlot(List<Job> jobs) {
int ans = Integer.MIN_VALUE;
for (var job : jobs) {
ans = Math.max(ans, job.deadline);
}
return ans;
}
void scheduleJobs(List<Job> jobs) {
Collections.sort(jobs, (j1, j2) -> {return (j1.profit > j2.profit) ? -1 : 1;});
int maxSlot = maxTimeSlot(jobs);
DisjointSet disjointSet = new DisjointSet(maxSlot);
for (var job : jobs) {
int availableSlot = disjointSet.find(job.deadline);
if (availableSlot > 0) {
disjointSet.merge(disjointSet.find(availableSlot - 1), availableSlot);
System.out.print(job + " ");
}
}
}
public static void main(String[] args) {
ArrayList<Job> arr =new ArrayList<Job>();
arr.add(new Job('a',2,100));
arr.add(new Job('b',1,19));
arr.add(new Job('c',2,27));
arr.add(new Job('d',1,25));
arr.add(new Job('e',3,15));
JobScheduling jobScheduling =
new JobScheduling();
jobScheduling.scheduleJobs(arr);
}
}