Skip to content

Commit 872851b

Browse files
committed
feat: leetcode contest 299
1 parent 82fea6e commit 872851b

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number[][]} edges
4+
* @return {number}
5+
*/
6+
const minimumScore = function (nums, edges) {
7+
const n = nums.length
8+
const e = Array(n).fill(0).map(() => [])
9+
for (const [x, y] of edges) {
10+
e[x].push(y)
11+
e[y].push(x)
12+
}
13+
let xAll = 0; let xBC, xA; let ret = 1e10
14+
for (const a of nums) xAll ^= a
15+
for (let i = 0; i < n; i++) {
16+
for (const j of e[i]) {
17+
// 删除边 i-j, j 所在的子树作为单调的一块
18+
// 计算以 i 为根,不含 j 的子树的异或和
19+
xBC = dfs(i, -1, j)
20+
xA = xAll ^ xBC
21+
// 在 B 中枚举第二条被删除的边
22+
dfs2(i, -1, j)
23+
}
24+
}
25+
return ret
26+
27+
function dfs (x, fa, ban) {
28+
let ans = nums[x]
29+
for (const y of e[x]) {
30+
if (y !== fa && y !== ban) ans ^= dfs(y, x, ban)
31+
}
32+
return ans
33+
}
34+
function dfs2 (x, fa, ban) {
35+
let ans = nums[x]
36+
for (const y of e[x]) {
37+
if (y !== fa && y !== ban) {
38+
const xC = dfs2(y, x, ban)
39+
ans ^= xC
40+
const xB = xBC ^ xC
41+
ret = Math.min(ret, Math.max(xA, xB, xC) - Math.min(xA, xB, xC))
42+
}
43+
}
44+
return ans
45+
}
46+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums1
3+
* @param {number[]} nums2
4+
* @return {number}
5+
*/
6+
var maximumsSplicedArray = function(a1, a2) {
7+
function solve(a1, a2) {
8+
const n = a1.length, d = [], dp = []
9+
dp[-1] = 0
10+
let max = -1e10, sum = 0
11+
for (let i = 0; i < n; i++) {
12+
d[i] = a2[i] - a1[i]
13+
sum += a1[i]
14+
dp[i] = Math.max(dp[i-1], 0) + d[i]
15+
max = Math.max(max, dp[i])
16+
}
17+
if (max > 0) sum += max
18+
return sum
19+
}
20+
21+
return Math.max(solve(a1, a2), solve(a2, a1))
22+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var countHousePlacements = function(n) {
6+
const dp = [], P = 1e9 + 7
7+
dp[0] = [1, 0]
8+
for (let i = 1; i <= n; i++) {
9+
dp[i] = []
10+
// i 处不放房子
11+
dp[i][0] = (dp[i-1][0] + dp[i-1][1]) % P
12+
// i 处放房子
13+
dp[i][1] = dp[i-1][0] % P
14+
}
15+
const x = BigInt((dp[n][0] + dp[n][1]) % P)
16+
return (x * x) % BigInt(P)
17+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {boolean}
4+
*/
5+
var checkXMatrix = function(g) {
6+
const n = g.length
7+
let ok = true
8+
for (let i = 0; i < n; i++) {
9+
for (let j = 0; j < n; j++) {
10+
if (i === j || (i+j) === n-1) {
11+
if (g[i][j] === 0) ok = false
12+
} else {
13+
if (g[i][j] !== 0) ok = false
14+
}
15+
}
16+
}
17+
return ok
18+
};

0 commit comments

Comments
 (0)