动态规化
class Solution {
class job {
int start, end, profit;
job (int s, int e, int p) {
start = s;
end = e;
profit = p;
}
}
public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
int n = startTime.length;
int[] dp = new int[n+1];
job[] jobs = new job[n];
for (int i = 0; i < n; i++)
jobs[i] = new job(startTime[i], endTime[i], profit[i]);
Arrays.sort(jobs, Comparator.comparingInt(o->o.end));
for(int i=0;i<n;i++)
dp[i]=jobs[i].profit;
for (int i = 1; i < n; i++) {
dp[i] = Math.max(dp[i-1], jobs[i].profit);
for (int j = i - 1; j >= 0; j--) {
if (jobs[j].end <= jobs[i].start) {
dp[i] = Math.max(dp[i], dp[j] + jobs[i].profit);
break;
}
}
}
return dp[n-1];
}
}
参考
动态规化 C++
动态规化 java