Skip to content

Commit bb7660e

Browse files
committed
feat: leetcode contest 313
1 parent cc71bd8 commit bb7660e

5 files changed

+142
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number} a
3+
* @param {number} b
4+
* @return {number}
5+
*/
6+
const commonFactors = function (a, b) {
7+
let ans = 0
8+
for (let i = 1; i <= Math.floor(Math.sqrt(a)); i++) {
9+
if (a % i === 0) {
10+
if (b % i === 0)ans++
11+
if (a / i !== i && b % (a / i) === 0)ans++
12+
}
13+
}
14+
return ans
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number}
4+
*/
5+
const maxSum = function (g) {
6+
const m = g.length; const n = g[0].length
7+
let ans = 0
8+
for (let i = 0; i + 2 < m; i++) {
9+
for (let j = 0; j + 2 < n; j++) {
10+
let t = 0
11+
for (let l = i; l <= i + 2; l++) {
12+
for (let s = j; s <= j + 2; s++) {
13+
t += g[l][s]
14+
}
15+
}
16+
t -= g[i + 1][j] + g[i + 1][j + 2]
17+
18+
ans = Math.max(ans, t)
19+
}
20+
}
21+
return ans
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number} num1
3+
* @param {number} num2
4+
* @return {number}
5+
*/
6+
const minimizeXor = function (num1, num2) {
7+
let s = num1.toString(2)
8+
let t = num2.toString(2)
9+
const n = Math.max(s.length, t.length)
10+
// console.log('s', s, typeof s, 't', t)
11+
s = s.padStart(n, '0').split('').map(Number)
12+
t = t.padStart(n, '0').split('').map(Number)
13+
const cnt = t.reduce((p, c) => p + c, 0)
14+
const ans = Array(n).fill(-1)
15+
let used = 0
16+
17+
for (let i = 0; i < n && used < cnt; i++) {
18+
if (s[i] === 1) ans[i] = 1, used++
19+
}
20+
for (let i = n - 1; i >= 0 && used < cnt; i--) {
21+
if (ans[i] === -1) ans[i] = 1, used++
22+
}
23+
for (let i = 0; i < n; i++) {
24+
if (ans[i] === -1) ans[i] = 0
25+
}
26+
return parseInt(ans.join(''), 2)
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const deleteString = function (s) {
6+
// 最好以 1 为开始下标,比较好处理 p 数组
7+
const n = s.length
8+
s = ' ' + s
9+
10+
const P = 131
11+
// 三哈希(双哈希的拓展)避免冲突
12+
const Q = 1e4 + 7; const Q1 = 1e5 + 7; const Q2 = 1e6 + 7
13+
const f = [0]; const p = [1]
14+
const f1 = [0]; const p1 = [1]
15+
const f2 = [0]; const p2 = [1]
16+
for (let i = 1; i <= n; i++) {
17+
f[i] = (f[i - 1] * P % Q + (s[i].charCodeAt(0) - 'a'.charCodeAt(0))) % Q
18+
p[i] = p[i - 1] * P % Q
19+
20+
f1[i] = (f1[i - 1] * P % Q1 + (s[i].charCodeAt(0) - 'a'.charCodeAt(0))) % Q1
21+
p1[i] = p1[i - 1] * P % Q1
22+
23+
f2[i] = (f2[i - 1] * P % Q2 + (s[i].charCodeAt(0) - 'a'.charCodeAt(0))) % Q2
24+
p2[i] = p2[i - 1] * P % Q2
25+
}
26+
const hash = (i, j) => {
27+
return ((f[j] - f[i - 1] * p[j - i + 1] % Q + Q) % Q) &&
28+
((f1[j] - f1[i - 1] * p1[j - i + 1] % Q1 + Q1) % Q1) &&
29+
((f2[j] - f2[i - 1] * p2[j - i + 1] % Q2 + Q2) % Q2)
30+
}
31+
32+
const dp = Array(n + 1).fill(1)
33+
34+
for (let i = n; i >= 1; i--) {
35+
for (let len = 1; i + (len * 2) - 1 <= n; len++) {
36+
// [i, j]; [j+1, k]
37+
const j = i + len - 1; const k = i + (len * 2) - 1
38+
if (hash(i, j) === hash(j + 1, k)) {
39+
dp[i] = Math.max(dp[i], dp[j + 1] + 1)
40+
}
41+
}
42+
}
43+
return dp[1]
44+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const deleteString = function (s) {
6+
// 最好以 1 为开始下标,比较好处理 p 数组
7+
const n = s.length
8+
s = ' ' + s
9+
10+
const P = 131n
11+
const Q = 1n << 64n
12+
const f = [0n]; const p = [1n]
13+
for (let i = 1; i <= n; i++) {
14+
f[i] = (f[i - 1] * P % Q + (BigInt(s[i].charCodeAt(0) - 'a'.charCodeAt(0)))) % Q
15+
p[i] = p[i - 1] * P % Q
16+
}
17+
const hash = (i, j) => {
18+
// 乘完之后的取模非常重要,漏掉就会 WA(出现较小负数导致 + 一个 Q 不够)
19+
return (f[j] - f[i - 1] * p[j - i + 1] % Q + Q) % Q
20+
}
21+
22+
const dp = Array(n + 1).fill(1)
23+
24+
for (let i = n; i >= 1; i--) {
25+
for (let len = 1; i + (len * 2) - 1 <= n; len++) {
26+
// [i, j]; [j+1, k]
27+
const j = i + len - 1; const k = i + (len * 2) - 1
28+
if (hash(i, j) === hash(j + 1, k)) {
29+
dp[i] = Math.max(dp[i], dp[j + 1] + 1)
30+
}
31+
}
32+
}
33+
return dp[1]
34+
}

0 commit comments

Comments
 (0)