Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JobSequencing in Java #1778

Merged
merged 5 commits into from
Oct 27, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions archive/j/java/JobSequencing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import java.util.*;

/** Invalid input exception to handle errors. */
class InvalidInputException extends Exception {}

public class JobSequencing {

public static class Job {
int deadline;
int profit;

public Job(int profit, int deadline) {
this.profit = profit;
this.deadline = deadline;
}
}

private static List<Job> createJobList(List<Integer> deadlines, List<Integer> profits) {
List<Job> jobs = new ArrayList<>();
for (int i = 0; i < deadlines.size() ; i++ ) {
jobs.add(new Job(profits.get(i), deadlines.get(i)));
}

return jobs;
}

public static class Sorted implements Comparator {

public int compare(Object o1, Object o2) {
Job j1 = (Job)o1;
Job j2 = (Job)o2;

if (j1.profit != j2.profit) {
return j2.profit - j1.profit;
} else {
return j2.deadline - j1.deadline;
}
}
}

private static int getMaxProfit(List<Job> jobs, int size) {
int maxProfit = 0;
Sorted sorter = new Sorted();
jobs.sort(sorter);

TreeSet<Integer> ts = new TreeSet<>();

for (int i = 0; i < size; i++) {
ts.add(i);
}

for (int i = 0; i < size; i++) {
Integer x = ts.floor(jobs.get(i).deadline - 1);
System.out.println("x: " + x);

if (x != null) {
maxProfit += jobs.get(i).profit;
ts.remove(x);
}
}
return maxProfit;
}

private static List<Integer> getList(String arg) {
List<Integer> list = new ArrayList<>();
if (arg.length() > 0) {
for (String p : arg.split(", ")) {
list.add(Integer.parseInt(p.trim()));
}
}
return list;
}

public static void main(String[] args) {

try {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter job profits separated by \", \" (e.g., 25, 15, 10, 5):");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of leveraging a Scanner, can you grab the inputs off the command line. That way, we can test it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code to get direct input from the command line. Hopefully it works this time!

List<Integer> profits = getList(scanner.nextLine());
System.out.print("Please enter job deadlines separated by \", \" (e.g., 3, 1, 2, 2):");
List<Integer> deadlines = getList(scanner.nextLine());

// null, empty or too many input
if (profits == null || profits.size() < 1 ) {
throw new InvalidInputException();
}

//Check if two lists are different Lengths
if (profits.size() != deadlines.size()) {
throw new InvalidInputException();
}

List<Job> jobs = createJobList(deadlines, profits);
System.out.println(getMaxProfit(jobs, jobs.size()));

} catch (NumberFormatException | InvalidInputException e) {
System.err.println("Usage: please provide a list of profits and a list of deadlines");
}
}

}
1 change: 1 addition & 0 deletions archive/j/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Welcome to Sample Programs in Java!
- [Game of Life in Java](https://github.com/jrg94/sample-programs/issues/108)
- [Hello World in Java](https://therenegadecoder.com/code/hello-world-in-java/)
- [Insertion Sort in Java](https://sample-programs.therenegadecoder.com/projects/insertion-sort/)
- [Job Sequencing in Java](https://sample-programs.therenegadecoder.com/projects/job-sequencing-with-deadlines/)
- [Longest Common Sub Sequence in Java](https://github.com/TheRenegadeCoder/sample-programs/issues/1364)
- [Merge Sort in Java](https://sample-programs.therenegadecoder.com/projects/merge-sort/)
- [Prime Number in Java](https://github.com/TheRenegadeCoder/sample-programs/issues/1372)
Expand Down