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.
1var minDifficulty = function(jobDifficulty, d) {
2 const n = jobDifficulty.length;
3 if (n < d) return -1;
4 const dp = Array.from({ length: d + 1 }, () => Array(n + 1).fill(Infinity));
5 dp[0][0] = 0;
6
7 for (let day = 1; day <= d; ++day) {
8 for (let job = day; job <= n; ++job) {
9 let maxDifficulty = 0;
10 for (let k = job; k >= day; --k) {
11 maxDifficulty = Math.max(maxDifficulty, jobDifficulty[k - 1]);
12 dp[day][job] = Math.min(dp[day][job], dp[day - 1][k - 1] + maxDifficulty);
13 }
14 }
15 }
16 return dp[d][n];
17};
18
19// Usage Example:
20// let jobDifficulty = [6, 5, 4, 3, 2, 1];
21// let d = 2;
22// console.log(minDifficulty(jobDifficulty, d)); // Output: 7
23
This JavaScript solution involves setting up a DP array using Array.from to create the DP table. It iterates through possible configurations of job partitions over days similarly to other languages but adheres to JavaScript syntax.
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.
1using System;
2using System.Linq;
3
4public class Solution {
5 int[,] memo;
6
7 private int Dfs(int[] jobDifficulty, int d, int idx) {
8 if (d == 1) {
9 return jobDifficulty.Skip(idx).Max();
10 }
11 if (memo[d, idx] != -1) return memo[d, idx];
12
13 int maxDiff = 0, result = int.MaxValue;
14 for (int i = idx; i <= jobDifficulty.Length - d; ++i) {
15 maxDiff = Math.Max(maxDiff, jobDifficulty[i]);
16 result = Math.Min(result, maxDiff + Dfs(jobDifficulty, d - 1, i + 1));
17 }
18 return memo[d, idx] = result;
19 }
20
21 public int MinDifficulty(int[] jobDifficulty, int d) {
22 int n = jobDifficulty.Length;
23 if (n < d) return -1;
24 memo = new int[d + 1, n];
25 for (int i = 0; i <= d; i++)
26 for (int j = 0; j < n; j++)
27 memo[i, j] = -1;
28 return Dfs(jobDifficulty, d, 0);
29 }
30
31 // Example usage:
32 // public static void Main(string[] args) {
33 // Solution sol = new Solution();
34 // int[] jobDifficulty = {6,5,4,3,2,1};
35 // int d = 2;
36 // Console.WriteLine(sol.MinDifficulty(jobDifficulty, d)); // Output: 7
37 // }
38}
The C# solution implements the recursive DFS approach with memoization using a 2D array that store results for subproblems. It chooses optimal job partition points by balancing the maximum difficulty for each subproblem.