Skip to content

Commit ca8c2bd

Browse files
committed
feat: problems of next week
1 parent 9d57afb commit ca8c2bd

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[]} jobs
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const minimumTimeRequired = function (jobs, k) {
7+
const n = jobs.length
8+
const cache = {}
9+
const time = (i) => {
10+
if (cache[i] != null) return cache[i]
11+
let ans = 0
12+
for (let k = 0; k < n; k++) {
13+
if (i >> k & 1) ans += jobs[k]
14+
}
15+
return cache[i] = ans
16+
}
17+
const dp = Array(k + 1).fill(0).map(() => Array(1 << n).fill(1e10))
18+
dp[0][0] = 0
19+
for (let i = 1; i <= k; i++) {
20+
dp[i][0] = 0
21+
for (let j = 1; j < 1 << n; j++) {
22+
// j 的所有子集
23+
for (let k = j; ; k = (k - 1) & j) {
24+
dp[i][j] = Math.min(
25+
dp[i][j],
26+
Math.max(dp[i - 1][j - k], time(k))
27+
)
28+
if (k === 0) break
29+
}
30+
}
31+
}
32+
return dp[k][(1 << n) - 1]
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {number}
4+
*/
5+
const longestIncreasingPath = function (matrix) {
6+
const m = matrix.length; const n = matrix[0].length
7+
const dx = [0, 0, 1, -1]
8+
const dy = [1, -1, 0, 0]
9+
const valid = (i, j) => i >= 0 && i < m && j >= 0 && j < n
10+
const cache = {}
11+
const get = (i, j) => {
12+
const key = `${i}_${j}`
13+
if (cache[key] != null) return cache[key]
14+
let ans = 1
15+
for (let k = 0; k < 4; k++) {
16+
const x = i + dx[k]
17+
const y = j + dy[k]
18+
if (valid(x, y) && matrix[i][j] > matrix[x][y]) {
19+
ans = Math.max(ans, get(x, y) + 1)
20+
}
21+
}
22+
return cache[key] = ans
23+
}
24+
let ans = 0
25+
for (let i = 0; i < m; i++) {
26+
for (let j = 0; j < n; j++) {
27+
ans = Math.max(ans, get(i, j))
28+
}
29+
}
30+
return ans
31+
}

0 commit comments

Comments
 (0)