Class Solution
java.lang.Object
g1801_1900.s1802_maximum_value_at_a_given_index_in_a_bounded_array.Solution
1802 - Maximum Value at a Given Index in a Bounded Array.<p>Medium</p>
<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>
<ul>
<li><code>nums.length == n</code></li>
<li><code>nums[i]</code> is a <strong>positive</strong> integer where <code>0 <= i < n</code>.</li>
<li><code>abs(nums[i] - nums[i+1]) <= 1</code> where <code>0 <= i < n-1</code>.</li>
<li>The sum of all the elements of <code>nums</code> does not exceed <code>maxSum</code>.</li>
<li><code>nums[index]</code> is <strong>maximized</strong>.</li>
</ul>
<p>Return <code>nums[index]</code> <em>of the constructed array</em>.</p>
<p>Note that <code>abs(x)</code> equals <code>x</code> if <code>x >= 0</code>, and <code>-x</code> otherwise.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> n = 4, index = 2, maxSum = 6</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong> nums = [1,2, <strong>2</strong> ,1] is one array that satisfies all the conditions. There are no arrays that satisfy all the conditions and have nums[2] == 3, so 2 is the maximum nums[2].</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> n = 6, index = 1, maxSum = 10</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= maxSum <= 10<sup>9</sup></code></li>
<li><code>0 <= index < n</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
maxValue
public int maxValue(int n, int index, int maxSum)
-