Skip to content

Commit 0c90f18

Browse files
committed
feat: leetcode contest 345
1 parent 1fb3e28 commit 0c90f18

4 files changed

+107
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} k
4+
* @return {number[]}
5+
*/
6+
const circularGameLosers = function (n, k) {
7+
const s = new Set(); let cur = 1; let j = 1
8+
while (true) {
9+
if (s.has(cur)) break
10+
s.add(cur)
11+
cur = ((cur + k * (j++) - 1) % n) + 1
12+
}
13+
const ans = []
14+
for (let i = 1; i <= n; i++) {
15+
if (!s.has(i)) ans.push(i)
16+
}
17+
return ans
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {number[]} derived
3+
* @return {boolean}
4+
*/
5+
const doesValidArrayExist = function (d) {
6+
let c = 0
7+
for (const x of d) {
8+
c ^= x
9+
}
10+
return c === 0
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} edges
4+
* @return {number}
5+
*/
6+
const countCompleteComponents = function (n, edges) {
7+
const fa = Array(n).fill(0).map((_, idx) => idx)
8+
const get = (x) => {
9+
if (fa[x] === x) return x
10+
return fa[x] = get(fa[x])
11+
}
12+
const merge = (x, y) => {
13+
const fx = get(x); const fy = get(y)
14+
fa[fx] = fy
15+
}
16+
const e = {}
17+
for (const [x, y] of edges) {
18+
e[x] ??= new Set()
19+
e[x].add(y)
20+
e[y] ??= new Set()
21+
e[y].add(x)
22+
merge(x, y)
23+
}
24+
const f = []; const fs = {}
25+
for (let i = 0; i < n; i++) {
26+
s = get(i)
27+
f.push(s)
28+
fs[s] ??= []
29+
fs[s].push(i)
30+
}
31+
// console.log(fs)
32+
let ans = 0
33+
for (const t of Object.values(fs)) {
34+
let ok = true
35+
for (let i = 0; i < t.length; i++) {
36+
if (!ok) break
37+
for (let j = i + 1; j < t.length; j++) {
38+
if (!e[t[i]].has(t[j])) {
39+
ok = false
40+
break
41+
}
42+
}
43+
}
44+
ans += ok
45+
}
46+
return ans
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number}
4+
*/
5+
const maxMoves = function (g) {
6+
const cache = {}
7+
const m = g.length; const n = g[0].length
8+
const dx = [0, -1, 1]; const dy = [1, 1, 1]
9+
const dfs = (i, j) => {
10+
if (cache[`${i}_${j}`]) return cache[`${i}_${j}`]
11+
if (j === 0) return cache[`${i}_${j}`] = 0
12+
let s = -1
13+
for (let k = 0; k < 3; k++) {
14+
const x = i - dx[k]; const y = j - dy[k]
15+
if (x >= 0 && x < m && y >= 0 && y < n && g[i][j] > g[x][y]) {
16+
s = Math.max(s, dfs(x, y))
17+
// console.log(x, y, i, j, cache, s)
18+
}
19+
}
20+
if (s === -1) return cache[`${i}_${j}`] = -1
21+
return cache[`${i}_${j}`] = s + 1
22+
}
23+
let ans = 0
24+
for (let i = 0; i < m; i++) {
25+
for (let j = 0; j < n; j++) {
26+
// console.log(i, j, dfs(i, j))
27+
ans = Math.max(ans, dfs(i, j))
28+
}
29+
}
30+
return ans
31+
}

0 commit comments

Comments
 (0)