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

updated job sequencing #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
36 changes: 26 additions & 10 deletions 15_Greedy/JobSequencing.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,41 @@ public Job(int id, int deadline, int profit) {
}

public static void main(String args[]) {
int jobsInfo[][] = {{4, 20}, {1, 10}, {1, 40}, {1, 30}};
int jobsInfo[][] = {{4, 60}, {1, 10}, {1, 40}, {1, 30}};
ArrayList<Job> jobs = new ArrayList<>();

for(int i=0; i<jobsInfo.length; i++) {

int maxtime=0;

for(int i=0; i<jobsInfo.length; i++) {
jobs.add(new Job(i, jobsInfo[i][0], jobsInfo[i][1]));
maxtime=Math.max(maxtime, jobsInfo[i][0]);
}

Collections.sort(jobs, (a, b) -> b.profit - a.profit);

boolean slot[]=new boolean[maxtime+1];
for(int i=0;i<=maxtime;i++){
slot[i]=false;

}

ArrayList<Integer> ans = new ArrayList<>();

ans.add(jobs.get(0).id);
int time = 1;
for(int i=1; i<jobs.size(); i++) {
int count=0;

for(int i=0; i<jobs.size(); i++) {
Job j = jobs.get(i);
if(time < j.deadline) {
ans.add(j.id);
time++;
}
for(int k=j.deadline;k>0;k--) {
if(slot[k]==false) {
slot[k]=true;
ans.add(j.id);
count++;
break;
}
}
}

System.out.println("No. of jobs to be done="+count);

for(int i=0; i<ans.size(); i++) {
System.out.print(ans.get(i)+" ");
Expand Down