LeetCode Daily Question 1723. The shortest time to complete all tasks

LeetCode Daily Question 1723. The shortest time to complete all tasks


Problem Description

insert image description here

Brief idea

It was almost past 12 o'clock before I remembered that I didn't check in today. Open it up and see, it's red... It's a CV meal when you go up, goodbye.

the code

class Solution {
    
    
public:
    int minimumTimeRequired(vector<int> &jobs, int k) {
    
    
        int n = jobs.size();
        int len = 1 << n;

        // sum[i]表示工作分配情况为i所花费的时间
        vector<int> sum(len);
        sum[0] = 0;
        for (int i = 1; i < len; ++i) {
    
    
            int x = __builtin_ctz(i);
            int y = i - (1 << x);
            sum[i] = sum[y] + jobs[x];
        }

        // dp[i][j]表示第i个worker分配完工作,分配情况为j的最少时间
        vector<vector<int>> dp(k, vector<int>(len));
        for (int i = 0; i < len; ++i) {
    
    
            dp[0][i] = sum[i];
        }

        for (int i = 1; i < k; ++i) {
    
    
            for (int j = 0; j < len; ++j) {
    
    
                int minCost = 0x3f3f3f3f;
                for (int x = j; x; x = (x - 1) & j) {
    
    
                    minCost = min(minCost, max(dp[i - 1][x], sum[j - x]));
                }
                dp[i][j] = minCost;
            }
        }
        return dp[k - 1][len - 1];
    }
};

Guess you like

Origin blog.csdn.net/qq_45438600/article/details/116547904