Skip to content

Commit 4b0a92e

Browse files
committed
feat: leetcode contest 317
1 parent 0439c1c commit 4b0a92e

7 files changed

+189
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const averageValue = function (nums) {
6+
let sum = 0; let cnt = 0
7+
for (const a of nums) {
8+
if (a % 2 === 0 && a % 3 === 0) sum += a, cnt += 1
9+
}
10+
if (cnt === 0) return 0
11+
return Math.floor(sum / cnt)
12+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {string[]} creators
3+
* @param {string[]} ids
4+
* @param {number[]} views
5+
* @return {string[][]}
6+
*/
7+
const mostPopularCreator = function (cs, is, vs) {
8+
const n = cs.length; const cnt = {}; const by = {}
9+
for (let i = 0; i < n; i++) {
10+
if (cnt[cs[i]] == null) cnt[cs[i]] = 0
11+
if (by[cs[i]] == null) by[cs[i]] = []
12+
cnt[cs[i]] += vs[i]
13+
by[cs[i]].push([is[i], vs[i]])
14+
}
15+
const hv = Math.max(...Object.values(cnt))
16+
for (const x of Object.keys(by)) {
17+
by[x].sort((a, b) => {
18+
if (a[1] === b[1]) return a[0] < b[0] ? -1 : 1
19+
return -(a[1] - b[1])
20+
})
21+
}
22+
// console.log(JSON.stringify(by))
23+
const ans = []; const has = {}
24+
for (let i = 0; i < n; i++) {
25+
if (cnt[cs[i]] === hv && !has[cs[i]]) {
26+
ans.push([cs[i], by[cs[i]][0][0]])
27+
has[cs[i]] = 1
28+
}
29+
}
30+
return ans
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} target
4+
* @return {number}
5+
*/
6+
const makeIntegerBeautiful = function (n, target) {
7+
const a = String(n).split('').map(Number)
8+
const m = a.length
9+
const sum = a.reduce((p, c) => p + c, 0)
10+
if (sum <= target) return 0
11+
let tmp = 0; let bs = 0
12+
for (let i = 0; i < m; i++) {
13+
const j = m - 1 - i
14+
tmp += a[j] * Math.pow(10, i)
15+
bs += a[j]
16+
const t = Math.pow(10, i + 1)
17+
if (sum - bs + 1 <= target) {
18+
return t - tmp
19+
}
20+
}
21+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @param {number[]} queries
12+
* @return {number[]}
13+
*/
14+
const treeQueries = function (root, queries) {
15+
const map = {}
16+
17+
const num_ch = (node) => {
18+
let cnt = 0
19+
if (node.left) cnt++
20+
if (node.right) cnt++
21+
return cnt
22+
}
23+
const dfs = (node, p = null) => {
24+
if (node == null) return
25+
map[node.val] = node
26+
if (p && num_ch(p) === 1) node.lp = p.lp, node.lpn = p.lpn + 1
27+
else node.lp = node, node.lpn = 0
28+
// console.log(node.val, node.lp?.val)
29+
dfs(node.left, node)
30+
dfs(node.right, node)
31+
node.height = Math.max(node.left?.height ?? 0, node.right?.height ?? 0) + 1
32+
node.parent = p
33+
}
34+
root.lp = root
35+
root.lpn = 0
36+
dfs(root)
37+
38+
const ans = []
39+
for (const q of queries) {
40+
let node = map[q].lp
41+
node.newHeight = map[q].lpn
42+
// console.log(node, node.newHeight)
43+
while (node && node.parent) {
44+
const p = node.parent
45+
let sible = null
46+
for (const x of [p.left, p.right]) if (x !== node) sible = x
47+
p.newHeight = Math.max(node.newHeight, sible?.height ?? 0) + 1
48+
49+
// console.log(node.val, node.newHeight, sible?.val, sible?.height, p.val, p.newHeight)
50+
node = p
51+
}
52+
ans.push(root.newHeight - 1)
53+
}
54+
return ans
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[][]} queries
3+
* @return {number[]}
4+
*/
5+
const waysToFillArray = function (queries) {
6+
const ans = []; const N = 1e4 + 15; const P = BigInt(1e9 + 7)
7+
const c = Array(N).fill(0).map(() => Array(15).fill(0n))
8+
const v = {}; const p = []
9+
for (let i = 2; i <= N; i++) {
10+
if (v[i]) continue
11+
p.push(i)
12+
for (let j = i; j <= N / i; j++) v[i * j] = 1
13+
}
14+
for (let i = 0; i < N; i++) {
15+
c[i][0] = 1n
16+
for (let j = 1; j <= Math.min(i, 14); j++) {
17+
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % P
18+
}
19+
}
20+
for (let [n, k] of queries) {
21+
let s = 1n
22+
for (let i = 0; p[i] <= k; i++) {
23+
let t = 0
24+
while (k % p[i] === 0) t++, k /= p[i]
25+
s = s * c[n + t - 1][t] % P
26+
}
27+
ans.push(Number(s))
28+
}
29+
return ans
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const countDifferentSubsequenceGCDs = function (nums) {
6+
const g = []
7+
const gcd = (a, b) => b ? gcd(b, a % b) : a
8+
for (const x of nums) {
9+
for (let y = 1; y <= Math.sqrt(x); y++) {
10+
if (x % y === 0) {
11+
if (g[y] == null) g[y] = x
12+
else g[y] = gcd(g[y], x)
13+
if (y != x / y) {
14+
if (g[x / y] == null) g[x / y] = x
15+
else g[x / y] = gcd(g[x / y], x)
16+
}
17+
}
18+
}
19+
}
20+
let ans = 0
21+
for (let i = 1; i <= 2e5; i++) {
22+
if (g[i] == i) ans++
23+
}
24+
return ans
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
const countPrimes = function (n) {
6+
const seen = new Uint8Array(n); const p = []
7+
for (let i = 2; i < n; i++) {
8+
if (!seen[i]) p.push(i)
9+
for (let j = 0; p[j] <= n / i; j++) {
10+
seen[p[j] * i] = 1
11+
if (i % p[j] === 0) break
12+
}
13+
}
14+
return p.length
15+
}

0 commit comments

Comments
 (0)