Skip to content

Commit f1ef439

Browse files
committed
feat: leetcode contest 294
1 parent 22f35bf commit f1ef439

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {string} s
3+
* @param {character} letter
4+
* @return {number}
5+
*/
6+
var percentageLetter = function(s, letter) {
7+
const n = s.length
8+
let cnt = 0
9+
for (const ch of s) {
10+
if (ch === letter) cnt++
11+
}
12+
return Math.floor(cnt / n * 100)
13+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} capacity
3+
* @param {number[]} rocks
4+
* @param {number} additionalRocks
5+
* @return {number}
6+
*/
7+
const maximumBags = function (c, r, a) {
8+
const x = []
9+
const n = c.length
10+
for (let i = 0; i < n; i++) {
11+
x.push(c[i] - r[i])
12+
}
13+
x.sort((a, b) => a - b)
14+
let cnt = 0
15+
for (const y of x) {
16+
if (a < y) break
17+
a -= y
18+
cnt++
19+
}
20+
return cnt
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[][]} stockPrices
3+
* @return {number}
4+
*/
5+
const minimumLines = function (s) {
6+
const n = s.length
7+
s.sort((a, b) => a[0] - b[0])
8+
let ans = 0
9+
for (let i = 0; i < n;) {
10+
if (i === n - 1) {
11+
break
12+
} else {
13+
const p = [s[i + 1][0] - s[i][0], s[i + 1][1] - s[i][1]]; let j = i + 2
14+
while (j < n && (BigInt(p[0]) * BigInt(s[j][1] - s[i][1])) === (BigInt(p[1]) * BigInt(s[j][0] - s[i][0]))) {
15+
j++
16+
}
17+
ans++
18+
// console.log(i, p, j-1)
19+
i = j - 1
20+
}
21+
}
22+
return ans
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[]} strength
3+
* @return {number}
4+
*/
5+
const totalStrength = function (s) {
6+
const n = s.length
7+
s.unshift(-1), s.push(-1)
8+
const sum = [0]; const ss = [0]; const P = 1e9 + 7; const Pn = BigInt(1e9 + 7)
9+
for (let i = 1; i <= n; i++) sum[i] = (sum[i - 1] + s[i]) % P
10+
for (let i = 1; i <= n; i++) ss[i] = (ss[i - 1] + sum[i]) % P
11+
const L = []; const R = []; let stk = [0]
12+
for (let i = 1; i <= n; i++) {
13+
while (s[stk[stk.length - 1]] >= s[i]) stk.pop()
14+
L[i] = stk[stk.length - 1] + 1
15+
stk.push(i)
16+
}
17+
stk = [n + 1]
18+
for (let i = n; i >= 1; i--) {
19+
while (s[stk[stk.length - 1]] > s[i]) stk.pop()
20+
R[i] = stk[stk.length - 1] - 1
21+
stk.push(i)
22+
}
23+
let ans = 0n
24+
25+
for (let i = 1; i <= n; i++) {
26+
const tmp = BigInt(i - L[i] + 1) * BigInt((ss[R[i]] - ss[i - 1] + P) % P) % Pn - BigInt(R[i] - i + 1) * BigInt((ss[i - 1] - (ss[L[i] - 2] ?? 0) + P) % P) % Pn + Pn
27+
ans = (ans + tmp * BigInt(s[i])) % Pn
28+
}
29+
30+
return ans
31+
}

0 commit comments

Comments
 (0)