[go: up one dir, main page]

0% found this document useful (0 votes)
8 views3 pages

Da 2

da2

Uploaded by

jerryt2714
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Da 2

da2

Uploaded by

jerryt2714
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Abhishek Chavan | 122B1B047

Assignment-2
package com;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

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

public class da2{


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of jobs: ");
int n = sc.nextInt();
ArrayList<Integer> profits = new ArrayList<>(n);
ArrayList<Integer> deadlines = new ArrayList<>(n);
System.out.println("Enter the profit for each job: ");
for(int i=0;i<n;i++){
profits.add(sc.nextInt());
}
System.out.println("Enter the deadline for each job: ");
for(int i=0;i<n;i++){
deadlines.add(sc.nextInt());
}
ArrayList<Job> jobs = new ArrayList<>(n);
for(int i=0;i<n;i++){
jobs.add(new Job(profits.get(i), deadlines.get(i)));
}
Collections.sort(jobs, (Job a, Job b) -> Integer.compare(b.profit, a.profit));

int[] profitsArray = new int[n];


int[] deadlinesArray = new int[n];
for(int i=0;i<n;i++){
Abhishek Chavan | 122B1B047

profitsArray[i] = jobs.get(i).profit;
deadlinesArray[i] = jobs.get(i).deadline;
}
int maxi = findMax(deadlinesArray);
boolean[] flag = new boolean[maxi + 1];
int totalProfit = 0;

for(int i=0;i<n;i++){
if(!flag[deadlinesArray[i]]){
flag[deadlinesArray[i]] = true;
totalProfit += profitsArray[i];
}else{
int k=deadlinesArray[i];
while(k >= 1){
if(!flag[k]){
flag[k] = true;
totalProfit += profitsArray[i];
break;
}
k--;
}
}
}
System.out.println("The Total Profit is: " + totalProfit);
sc.close();
}

private static int findMax(int[] array){


int max = array[0];
for(int i=1;i<array.length; i++){
if(array[i]>max){
max = array[i];
}
}
return max;
}
}
Abhishek Chavan | 122B1B047

You might also like