Last active
July 29, 2019 19:46
-
-
Save RitamChakraborty/d9b415e9c46b7f4ff2fe61c7779dcfb7 to your computer and use it in GitHub Desktop.
Job Sequencing with Deadline algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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