-
-
Notifications
You must be signed in to change notification settings - Fork 567
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
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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):"); | ||
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"); | ||
} | ||
} | ||
|
||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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!