Skip to content

Commit 1330ac6

Browse files
committed
feat: leetcode contest 297
1 parent 6612fe5 commit 1330ac6

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[][]} brackets
3+
* @param {number} income
4+
* @return {number}
5+
*/
6+
const calculateTax = function (b, inn) {
7+
let ans = 0
8+
for (let i = 0; i < b.length; i++) {
9+
const [x, y] = b[i]
10+
if (inn >= x) ans += (x - (b[i - 1]?.[0] ?? 0)) * y / 100
11+
else {
12+
ans += (inn - (b[i - 1]?.[0] ?? 0)) * y / 100
13+
break
14+
}
15+
}
16+
return ans
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @param {number[][]} moveCost
4+
* @return {number}
5+
*/
6+
var minPathCost = function(g, mc) {
7+
const m = g.length, n = g[0].length
8+
const dp = Array(m).fill(0).map(() => [])
9+
for (let i = 0; i < n; i++) dp[0][i] = g[0][i]
10+
let ans = 1e10
11+
for (let i = 1; i < m; i++) {
12+
for (let j = 0; j < n; j++) {
13+
dp[i][j] = 1e10
14+
for (let k = 0; k < n; k++) {
15+
dp[i][j] = Math.min(dp[i][j], dp[i-1][k] + mc[g[i-1][k]][j] + g[i][j])
16+
}
17+
if (i === m-1) ans = Math.min(ans, dp[i][j])
18+
}
19+
}
20+
return ans
21+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} cookies
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var distributeCookies = function(ck, k) {
7+
let ans = 1e10
8+
const n = ck.length
9+
const dfs = (x, s) => {
10+
let max = Math.max(...s)
11+
if (max >= ans) return
12+
if (x === n) {
13+
ans = Math.min(ans, max)
14+
return
15+
}
16+
for (let i = 0; i < k; i++) {
17+
s[i] += ck[x]
18+
dfs(x+1, s)
19+
s[i] -= ck[x]
20+
}
21+
}
22+
dfs(0, Array(k).fill(0))
23+
return ans
24+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {string[]} ideas
3+
* @return {number}
4+
*/
5+
const distinctNames = function (is) {
6+
const s = new Set(is)
7+
// cnt[x][y] 表示把首字母从 x 改成 y 以后就不在 ideas 里的字符串数量
8+
const cnt = {}
9+
for (const t of is) {
10+
const old = t[0]
11+
cnt[old] ??= {}
12+
for (let i = 0; i < 26; i++) {
13+
const now = String.fromCharCode('a'.charCodeAt(0) + i)
14+
cnt[old][now] ??= 0
15+
if (!s.has(now + t.slice(1))) cnt[old][now]++
16+
}
17+
}
18+
let ans = 0
19+
for (const t of is) {
20+
for (let i = 0; i < 26; i++) {
21+
const y = String.fromCharCode('a'.charCodeAt(0) + i)
22+
if (s.has(y + t.slice(1))) continue
23+
ans += cnt[y]?.[t[0]] ?? 0
24+
}
25+
}
26+
return ans
27+
}
28+
29+
/*
30+
["aaa","baa","caa","bbb","cbb","dbb"]
31+
2
32+
*/

0 commit comments

Comments
 (0)