File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
maximum-value-at-a-given-index-in-a-bounded-array Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,8 @@ Step 2. Add the dependency
51
51
52
52
https://leetcode.cn/problems/path-with-minimum-effort
53
53
54
+ https://leetcode.cn/problems/maximum-value-at-a-given-index-in-a-bounded-array/
55
+
54
56
https://leetcode.cn/problems/last-day-where-you-can-still-cross/
55
57
56
58
https://leetcode.cn/problems/bricks-falling-when-hit/
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments