Question: package project005; import java.util.*; class Job{ int start; int finish; int profit; public Job(int s, int e, int val){ start = s; finish = e;
package project005; import java.util.*; class Job{ int start; int finish; int profit; public Job(int s, int e, int val){ start = s; finish = e; profit = val; } } // Time Complexity: O(n^2) class Project005{ public static int[] jobIndexes; public static int k; public int latestNonConflictingJob(Job[] jobs, int n){ for(int i = n - 1; i >= 0; i--){ if(jobs[i].finish <= jobs[n].start) return i; } return -1; } // Memoization public int maxProfitRecur(Job[] jobs, int n, int[] dp){ if(n < 0) return 0; if(dp[n] != -1) return dp[n]; // include nth job or not int profitIncludingCur = jobs[n].profit; int nextJob = latestNonConflictingJob(jobs, n); if(nextJob != -1) profitIncludingCur += maxProfitRecur(jobs, nextJob, dp); int profitExcludingProfit = maxProfitRecur(jobs, n-1, dp); dp[n] = Math.max(profitIncludingCur, profitExcludingProfit); return dp[n]; } public int maxProfitMemoization(Job[] jobs){ int n = jobs.length; // Sort by finish time Arrays.sort(jobs, new Comparator
Set up a method to display the indexes of the jobs used. Above is the code that I have so far.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
