Skip to content

Commit 149a8f0

Browse files
committed
https://leetcode.cn/problems/maximum-value-at-a-given-index-in-a-bounded-array/
1 parent 0abe91a commit 149a8f0

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Step 2. Add the dependency
5151

5252
https://leetcode.cn/problems/path-with-minimum-effort
5353

54+
https://leetcode.cn/problems/maximum-value-at-a-given-index-in-a-bounded-array/
55+
5456
https://leetcode.cn/problems/last-day-where-you-can-still-cross/
5557

5658
https://leetcode.cn/problems/bricks-falling-when-hit/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function maxValue(n: number, index: number, maxSum: number): number {
2+
let left = index;
3+
let right = n - index - 1;
4+
if (left > right) {
5+
const temp = left;
6+
left = right;
7+
right = temp;
8+
}
9+
10+
let upper =
11+
((left + 1) * (left + 1) - 3 * (left + 1)) / 2 +
12+
left +
13+
1 +
14+
(left + 1) +
15+
((left + 1) * (left + 1) - 3 * (left + 1)) / 2 +
16+
right +
17+
1;
18+
if (upper >= maxSum) {
19+
const a = 1;
20+
const b = -2;
21+
const c = left + right + 2 - maxSum;
22+
return Math.floor((-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a));
23+
}
24+
25+
upper =
26+
((2 * (right + 1) - left - 1) * left) / 2 +
27+
(right + 1) +
28+
((right + 1) * (right + 1) - 3 * (right + 1)) / 2 +
29+
right +
30+
1;
31+
if (upper >= maxSum) {
32+
const a = 1.0 / 2;
33+
const b = left + 1 - 3.0 / 2;
34+
const c = right + 1 + ((-left - 1) * left) / 2 - maxSum;
35+
return Math.floor((-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a));
36+
} else {
37+
const a = left + right + 1;
38+
const b = (-left * left - left - right * right - right) / 2 - maxSum;
39+
return Math.floor(-b / a);
40+
}
41+
}
42+
export default maxValue;

0 commit comments

Comments
 (0)