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 Job Sequencing Problem in Java #6741

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions code/job_sequencing.java
@@ -0,0 +1,52 @@
import java.util.*;

class Job {
char id;
int deadline;
int profit;

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

public class JobSequencing {
public static void main(String[] args) {
List<Job> jobs = new ArrayList<>();
jobs.add(new Job('a', 2, 100));
jobs.add(new Job('b', 1, 19));
jobs.add(new Job('c', 2, 27));
jobs.add(new Job('d', 1, 25));
jobs.add(new Job('e', 3, 15));

// Sort the jobs in decreasing order of profit
jobs.sort((j1, j2) -> Integer.compare(j2.profit, j1.profit));

int maxDeadline = 0;
for (Job job : jobs) {
if (job.deadline > maxDeadline) {
maxDeadline = job.deadline;
}
}

char[] result = new char[maxDeadline];
Arrays.fill(result, ' ');

int totalProfit = 0;

for (Job job : jobs) {
for (int i = job.deadline - 1; i >= 0; i--) {
if (result[i] == ' ') {
result[i] = job.id;
totalProfit += job.profit;
break;
}
}
}

System.out.println("Maximized Profit: " + totalProfit);
System.out.println("Job Sequence: " + new String(result));
}
}