Skip to content

Instantly share code, notes, and snippets.

@RitamChakraborty
Last active July 29, 2019 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RitamChakraborty/d9b415e9c46b7f4ff2fe61c7779dcfb7 to your computer and use it in GitHub Desktop.
Save RitamChakraborty/d9b415e9c46b7f4ff2fe61c7779dcfb7 to your computer and use it in GitHub Desktop.
Job Sequencing with Deadline algorithm
import java.util.Arrays;
import java.util.Collections;
public class JobSquencingWithDeadlines {
public static void main(String[] args) {
int maxDeadline = 7;
Integer[] profits = {35, 30, 25, 20, 15, 12, 5};
Integer[] deadlines = {3, 4, 4, 2, 3, 1, 2};
System.out.println(getMaxProfit(profits, deadlines, maxDeadline));
}
private static int getMaxProfit(Integer[] profits, Integer[] deadlines, int maxDeadline) {
maxDeadline = Collections.max(Arrays.asList(deadlines)) < maxDeadline
? Collections.max(Arrays.asList(deadlines)) : maxDeadline;
int[] slots = new int[maxDeadline];
int maxProfit = 0;
for (int i = 0; i < profits.length; i++) {
if (maxDeadline == 0) {
break;
} else {
int deadline = deadlines[i] - 1;
while (deadline >= 0) {
if (slots[deadline] == 0) {
slots[deadline] = i + 1;
maxProfit += profits[i];
break;
} else {
deadline--;
}
}
}
}
System.out.println(Arrays.toString(slots));
return maxProfit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment