Skip to content

Commit e5abe66

Browse files
committed
feat: leetcode contest 271
1 parent 4561148 commit e5abe66

6 files changed

+163
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {string} rings
3+
* @return {number}
4+
*/
5+
const countPoints = function (ri) {
6+
const r = ri.split('')
7+
const cnt = []
8+
for (let i = 0; i < r.length; i += 2) {
9+
const c = r[i]; const l = Number(r[i + 1])
10+
if (!cnt[l]) cnt[l] = new Set()
11+
cnt[l].add(c)
12+
}
13+
let ans = 0
14+
for (const s of cnt) {
15+
if (s?.size >= 3) ans++
16+
}
17+
return ans
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const subArrayRanges = function (nums) {
6+
const n = nums.length
7+
let ans = 0n
8+
for (let i = 0; i < n; i++) {
9+
// 作为最大值的次数
10+
let j = i - 1; let k = i + 1
11+
while (j >= 0 && nums[j] <= nums[i]) j--
12+
while (k < n && nums[k] < nums[i]) k++
13+
const cnt1 = k - j - 1 + (i - j - 1) * (k - i - 1)
14+
let x = i - 1; let y = i + 1
15+
while (x >= 0 && nums[x] > nums[i]) x--
16+
while (y < n && nums[y] >= nums[i]) y++
17+
const cnt2 = y - x - 1 + (i - x - 1) * (y - i - 1)
18+
// console.log(i, nums[i], cnt1, cnt2)
19+
ans += BigInt((cnt1 - cnt2) * nums[i])
20+
}
21+
return Number(ans)
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[]} plants
3+
* @param {number} capacityA
4+
* @param {number} capacityB
5+
* @return {number}
6+
*/
7+
const minimumRefill = function (p, a, b) {
8+
const n = p.length
9+
let ca = a; let cb = b
10+
let ans = 0
11+
for (let i = 0, j = n - 1; i <= j; i++, j--) {
12+
if (i === j) {
13+
// console.log(ca, cb, p[i])
14+
if (ca >= cb && ca >= p[i]) ca -= p[i]
15+
else if (ca < cb && cb >= p[i]) cb -= p[i]
16+
else ans++
17+
} else {
18+
// A
19+
if (p[i] > ca) ca = a, ans++
20+
ca -= p[i]
21+
// B
22+
if (p[j] > cb) cb = b, ans++
23+
cb -= p[j]
24+
}
25+
}
26+
return ans
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @param {number[][]} fruits
3+
* @param {number} startPos
4+
* @param {number} k
5+
* @return {number}
6+
*/
7+
const maxTotalFruits = function (f, s, k) {
8+
const sum = []
9+
sum[-1] = 0
10+
const n = f.length
11+
for (let i = 0, j = 0; j < n; i++) {
12+
if (f[j][0] === i) {
13+
sum[i] = sum[i - 1] + f[j][1]
14+
j++
15+
} else {
16+
sum[i] = sum[i - 1]
17+
}
18+
}
19+
while (sum.length - 1 < s) sum.push(sum[sum.length - 1])
20+
const m = sum.length - 1
21+
let ans = 0
22+
for (let x = Math.max(0, s - k); x <= s; x++) {
23+
let l = s; let r = m
24+
const valid = (y) => {
25+
const dx = s - x; const dy = y - s
26+
if (2 * dx + dy <= k || dx + 2 * dy <= k) return true
27+
return false
28+
}
29+
while (l < r) {
30+
const mid = l + r + 1 >> 1
31+
if (valid(mid)) l = mid
32+
else r = mid - 1
33+
}
34+
// console.log(x, l)
35+
ans = Math.max(ans, sum[l] - sum[x - 1])
36+
}
37+
return ans
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {character[][]} matrix
3+
* @return {number}
4+
*/
5+
const maximalSquare = function (ma) {
6+
const m = ma.length; const n = ma[0].length
7+
const sum = [...Array(m + 1)].map(() => Array(n + 1).fill(0))
8+
for (let i = 1; i <= m; i++) {
9+
for (let j = 1; j <= n; j++) {
10+
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + Number(ma[i - 1][j - 1])
11+
}
12+
}
13+
let ans = 0
14+
for (let i0 = 1; i0 <= m; i0++) {
15+
for (let j0 = 1; j0 <= n; j0++) {
16+
for (let i1 = i0; i1 <= m; i1++) {
17+
const j1 = j0 + i1 - i0
18+
const area = sum[i1][j1] - sum[i1][j0 - 1] - sum[i0 - 1][j1] + sum[i0 - 1][j0 - 1]
19+
if (area === (i1 - i0 + 1) * (j1 - j0 + 1)) {
20+
// console.log(i0, j0, i1, j1, area)
21+
ans = Math.max(ans, area)
22+
}
23+
}
24+
}
25+
}
26+
return ans
27+
}
+31-20
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
1-
function primePalindrome (n: number): number {
2-
const z: number[] = []
3-
for (let x = 1; x <= 2e4; x++) {
4-
const y: Array<number|string> = [
5-
String(x) + String(x).split('').reverse().join('')
6-
]
7-
for (let i = 0; i < 10; i++) {
8-
y.push(String(x) + String(i) + String(x).split('').reverse().join(''))
1+
const s = []
2+
const getS = () => {
3+
if (s.length) return
4+
const isPrime = (a) => {
5+
if (a === 1) return false
6+
for (let i = 2; i <= a / i; i++) {
7+
if (a % i === 0) return false
98
}
10-
if (x < 10) y.push(x)
11-
z.push(...y.map(Number))
9+
return true
1210
}
11+
for (let i = 1; i < 1e4; i++) {
12+
if (i < 10 && isPrime(i)) s.push(i)
13+
Bg
14+
// 偶数长
15+
const a = Number(String(i) + String(i).split('').reverse().join(''))
16+
if (isPrime(a)) s.push(a)
1317

14-
z.sort((a, b) => a - b)
15-
for (const val of z) {
16-
if (isPrime(val) && val >= n) return val
17-
}
18-
return -1
19-
function isPrime (n: number): boolean {
20-
if (n === 1) return false
21-
for (let i = 2; i <= Math.sqrt(n); i++) {
22-
if (n % i === 0) return false
18+
// 奇数长
19+
for (let j = 0; j < 10; j++) {
20+
const a = Number(String(i) + String(j) + String(i).split('').reverse().join(''))
21+
if (isPrime(a)) s.push(a)
2322
}
24-
return true
2523
}
24+
s.sort((a, b) => a - b)
25+
}
26+
27+
function primePalindrome (n: number): number {
28+
getS()
29+
let l = 0; let r = s.length - 1
30+
// console.log(s[r])
31+
while (l < r) {
32+
const mid = l + r >> 1
33+
if (s[mid] >= n) r = mid
34+
else l = mid + 1
35+
}
36+
return s[l]
2637
};

0 commit comments

Comments
 (0)