|
| 1 | +# [1802. Maximum Value at a Given Index in a Bounded Array (Medium)](https://leetcode.com/problems/maximum-value-at-a-given-index-in-a-bounded-array/) |
| 2 | + |
| 3 | +<p>You are given three positive integers <code>n</code>, <code>index</code> and <code>maxSum</code>. You want to construct an array <code>nums</code> <strong>(0-indexed) </strong>that satisfies the following conditions:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><code>nums.length == n</code></li> |
| 7 | + <li><code>nums[i]</code> is a <strong>positive</strong> integer where <code>0 <= i < n</code>.</li> |
| 8 | + <li><code>abs(nums[i] - nums[i+1]) <= 1</code> where <code>0 <= i < n-1</code>.</li> |
| 9 | + <li>The sum of all the elements of <code>nums</code> does not exceed <code>maxSum</code>.</li> |
| 10 | + <li><code>nums[index]</code> is <strong>maximized</strong>.</li> |
| 11 | +</ul> |
| 12 | + |
| 13 | +<p>Return <code>nums[index]</code> of the constructed array.</p> |
| 14 | + |
| 15 | +<p>Note that <code>abs(x)</code> equals <code>x</code> if <code>x >= 0</code>, and <code>-x</code> otherwise.</p> |
| 16 | + |
| 17 | +<p> </p> |
| 18 | +<p><strong>Example 1:</strong></p> |
| 19 | + |
| 20 | +<pre><strong>Input:</strong> n = 4, index = 2, maxSum = 6 |
| 21 | +<strong>Output:</strong> 2 |
| 22 | +<strong>Explanation:</strong> The arrays [1,1,<strong>2</strong>,1] and [1,2,<strong>2</strong>,1] satisfy all the conditions. There are no other valid arrays with a larger value at the given index. |
| 23 | +</pre> |
| 24 | + |
| 25 | +<p><strong>Example 2:</strong></p> |
| 26 | + |
| 27 | +<pre><strong>Input:</strong> n = 6, index = 1, maxSum = 10 |
| 28 | +<strong>Output:</strong> 3 |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p> </p> |
| 32 | +<p><strong>Constraints:</strong></p> |
| 33 | + |
| 34 | +<ul> |
| 35 | + <li><code>1 <= n <= maxSum <= 10<sup>9</sup></code></li> |
| 36 | + <li><code>0 <= index < n</code></li> |
| 37 | +</ul> |
| 38 | + |
| 39 | +**Related Topics**: |
| 40 | +[Binary Search](https://leetcode.com/tag/binary-search/), [Greedy](https://leetcode.com/tag/greedy/) |
| 41 | + |
| 42 | +## Solution 1. Binary Answer |
| 43 | + |
| 44 | +```cpp |
| 45 | +// OJ: https://leetcode.com/problems/maximum-value-at-a-given-index-in-a-bounded-array/ |
| 46 | +// Author: github.com/lzl124631x |
| 47 | +// Time: O(log(maxSum)) |
| 48 | +// Space: O(1) |
| 49 | +class Solution { |
| 50 | + long count(long len, long M) { |
| 51 | + return M >= len ? (2 * M - len + 1) * len / 2 : ((M - 1) * M / 2 + len); |
| 52 | + } |
| 53 | + bool valid(long n, long i, long maxSum, long M) { |
| 54 | + long left = count(i + 1, M), right = count(n - i, M); |
| 55 | + return left + right - M <= maxSum; |
| 56 | + } |
| 57 | +public: |
| 58 | + int maxValue(int n, int index, int maxSum) { |
| 59 | + int L = 1, R = maxSum; |
| 60 | + while (L <= R) { |
| 61 | + int M = (L + R) / 2; |
| 62 | + if (valid(n, index, maxSum, M)) L = M + 1; |
| 63 | + else R = M - 1; |
| 64 | + } |
| 65 | + return R; |
| 66 | + } |
| 67 | +}; |
| 68 | +``` |
0 commit comments