Skip to content

Commit cade42f

Browse files
authored
Create 1335-minimum-difficulty-of-a-job-schedule.js
1 parent cba8825 commit cade42f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} jobDifficulty
3+
* @param {number} d
4+
* @return {number}
5+
*/
6+
const minDifficulty = function (jobDifficulty, d) {
7+
if (jobDifficulty.length < d) return -1
8+
const cache = {}
9+
const dfs = (start, numDays) => {
10+
if (numDays === d) {
11+
return start === jobDifficulty.length ? 0 : Infinity
12+
}
13+
const key = `${start}-${numDays}`
14+
if (cache[key] !== undefined) return cache[key]
15+
const end = jobDifficulty.length - d + numDays
16+
let result = Infinity
17+
let max = -Infinity
18+
for (let i = start; i <= end; i++) {
19+
max = Math.max(max, jobDifficulty[i])
20+
result = Math.min(result, max + dfs(i + 1, numDays + 1))
21+
}
22+
return (cache[key] = result)
23+
}
24+
return dfs(0, 0)
25+
}

0 commit comments

Comments
 (0)