This approach uses a dynamic programming (DP) table to calculate the minimum difficulty schedule. The DP table is constructed such that dp[i][j]
represents the minimum difficulty for scheduling the first i
jobs in j
days. We aim to fill this DP table using a bottom-up approach, iterating over all possible job and day combinations. For each combination, we calculate the scenario where the last day covers a range of jobs and obtains the minimum difficulty possible.
Time Complexity: O(n2d), where n
is the length of jobDifficulty
and d
is the number of days.
Space Complexity: O(nd), due to the storage of minimum difficulties in the DP table.
1from typing import List
2
3class Solution:
4 def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
5 n = len(jobDifficulty)
6 if n < d:
7 return -1
8 dp = [[float('inf')] * (n + 1) for _ in range(d + 1)]
9 dp[0][0] = 0
10
11 for day in range(1, d + 1):
12 for job in range(day, n + 1):
13 max_diff = 0
14 for k in range(job, day - 1, -1):
15 max_diff = max(max_diff, jobDifficulty[k - 1])
16 dp[day][job] = min(dp[day][job], dp[day - 1][k - 1] + max_diff)
17
18 return dp[d][n]
19
20# Usage Example:
21# sol = Solution()
22# jobDifficulty = [6, 5, 4, 3, 2, 1]
23# d = 2
24# print(sol.minDifficulty(jobDifficulty, d)) # Output: 7
25
The Python solution constructs and updates the DP table efficiently using list comprehensions and nested loops, reflecting the same bottom-up approach to calculate the minimal schedule difficulty for given jobs.
This approach is based on recursion with memoization. The main idea is to use a recursive function to find the minimum difficulty from a given starting index, splitting the jobs over the remaining days. For each recursive call, consider different partitions for the first day's job and compute the best possible schedule using range maximum queries and storing results to avoid redundant calculations.
Time Complexity: O(n2d), with memoization reducing redundant calculations.
Space Complexity: O(nd), storing calculated subproblems and recursion frames.
1#include <vector>
2#include <algorithm>
3#include <cstring>
4using namespace std;
5
6class Solution {
7 int memo[11][301];
8
9 int dfs(vector<int>& jobDifficulty, int n, int d, int idx) {
10 if (d == 1) {
11 return *max_element(jobDifficulty.begin() + idx, jobDifficulty.end());
12 }
13 if (memo[d][idx] != -1) return memo[d][idx];
14
15 int maxDiff = 0, result = INT_MAX;
16 for (int i = idx; i <= n - d; ++i) {
17 maxDiff = max(maxDiff, jobDifficulty[i]);
18 result = min(result, maxDiff + dfs(jobDifficulty, n, d - 1, i + 1));
19 }
20 return memo[d][idx] = result;
21 }
22
23public:
24 int minDifficulty(vector<int>& jobDifficulty, int d) {
25 int n = jobDifficulty.size();
26 if (n < d) return -1;
27 memset(memo, -1, sizeof(memo));
28 return dfs(jobDifficulty, n, d, 0);
29 }
30};
31
32// Usage Example:
33// int main() {
34// Solution sol;
35// vector<int> jobDifficulty = {6,5,4,3,2,1};
36// int d = 2;
37// int result = sol.minDifficulty(jobDifficulty, d);
38// // Output: 7
39// }
40
The C++ solution uses a recursive DFS with memoization to handle subproblems efficiently. It makes decisions based on the maximum job difficulty for every potential partition from the current index onward.