Leetcode 1235. 规划兼职工作

1235. 规划兼职工作

Leetcode 1235. 规划兼职工作_第1张图片
思路参照b站详解 正月点灯笼up的

class Solution {
    public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
        // startTime endTime profit previous
        int[][] combine = new int[startTime.length][4];
        for(int i = 0; i < startTime.length; i++){
            combine[i][0] = startTime[i];
            combine[i][1] = endTime[i];
            combine[i][2] = profit[i];
            combine[i][3] = -1;
        }
        Arrays.sort(combine, (o1, o2) -> o1[1] - o2[1]);
        for(int i = 0; i < combine.length; i++){
            for(int j = i - 1; j >= 0; j--){
                if(combine[j][1] <= combine[i][0]){
                    combine[i][3] = j;
                    break;
                }
            }
        }
        int[] dp = new int[combine.length];
        dp[0] = combine[0][2];
        for(int i = 1; i < dp.length; i++){
            int temp = combine[i][3] >= 0 ? dp[combine[i][3]] + combine[i][2] : combine[i][2];
            dp[i] = Math.max(dp[i - 1], temp);
        }
        return dp[dp.length - 1];
    }
}

你可能感兴趣的:(Leetcode,leetcode,java,动态规划,算法)