Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 04f0eb1

Browse files
authoredNov 25, 2021
Update 1802-maximum-value-at-a-given-index-in-a-bounded-array.js
1 parent 9497bf0 commit 04f0eb1

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
 

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

+28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
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+
maxSum -= n;
9+
let level = 1;
10+
let left = index;
11+
let right = index;
12+
13+
while (maxSum - (right - left + 1) >= 0) {
14+
if (left === 0 && right === n - 1) break
15+
maxSum -= right - left + 1;
16+
if (left - 1 >= 0) left--
17+
if (right + 1 <= n - 1) right++;
18+
level++;
19+
}
20+
21+
if (maxSum) level += ~~(maxSum / n)
22+
23+
return level;
24+
}
25+
26+
// another
27+
28+
129
/**
230
* @param {number} n
331
* @param {number} index

0 commit comments

Comments
 (0)
Please sign in to comment.