-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathJobSequencing.java
66 lines (54 loc) · 2 KB
/
JobSequencing.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
66
package com.thealgorithms.greedyalgorithms;
import java.util.ArrayList;
import java.util.Arrays;
// Problem Link: https://en.wikipedia.org/wiki/Job-shop_scheduling
public final class JobSequencing {
private JobSequencing() {
}
// Define a Job class that implements Comparable for sorting by profit in descending order
static class Job implements Comparable<Job> {
char id;
int deadline;
int profit;
// Compare jobs by profit in descending order
@Override
public int compareTo(Job otherJob) {
return otherJob.profit - this.profit;
}
Job(char id, int deadline, int profit) {
this.id = id;
this.deadline = deadline;
this.profit = profit;
}
}
// Function to print the job sequence
public static String findJobSequence(ArrayList<Job> jobs, int size) {
Boolean[] slots = new Boolean[size];
Arrays.fill(slots, Boolean.FALSE);
int[] result = new int[size];
// Iterate through jobs to find the optimal job sequence
for (int i = 0; i < size; i++) {
for (int j = jobs.get(i).deadline - 1; j >= 0; j--) {
if (!slots[j]) {
result[j] = i;
slots[j] = Boolean.TRUE;
break;
}
}
}
// Create a StringBuilder to build the job sequence string
StringBuilder jobSequenceBuilder = new StringBuilder();
jobSequenceBuilder.append("Job Sequence: ");
for (int i = 0; i < jobs.size(); i++) {
if (slots[i]) {
jobSequenceBuilder.append(jobs.get(result[i]).id).append(" -> ");
}
}
// Remove the trailing " -> " from the job sequence
if (jobSequenceBuilder.length() >= 4) {
jobSequenceBuilder.setLength(jobSequenceBuilder.length() - 4);
}
// Return the job sequence as a string
return jobSequenceBuilder.toString();
}
}