Skip to content

Commit a9bf29e

Browse files
authored
Update 1802-maximum-value-at-a-given-index-in-a-bounded-array.js
1 parent 163b834 commit a9bf29e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

1802-maximum-value-at-a-given-index-in-a-bounded-array.js

+27
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} index
4+
* @param {number} maxSum
5+
* @return {number}
6+
*/
7+
const maxValue = function(n, index, maxSum) {
8+
let res = 1, l = index, r = index
9+
maxSum -= n
10+
11+
while(l > 0 || r < n - 1) {
12+
const len = r - l + 1
13+
if(maxSum >= len) {
14+
maxSum -= len
15+
res++
16+
} else break
17+
if(l > 0) l--
18+
if(r < n - 1) r++
19+
}
20+
res += ~~(maxSum / n)
21+
22+
return res
23+
}
24+
25+
// another
26+
27+
128
/**
229
* @param {number} n
330
* @param {number} index

0 commit comments

Comments
 (0)