<p>You are given three positive integers:&nbsp;<code>n</code>, <code>index</code>, and <code>maxSum</code>. You want to construct an array <code>nums</code> (<strong>0-indexed</strong>)<strong> </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 &lt;= i &lt; n</code>.</li>
	<li><code>abs(nums[i] - nums[i+1]) &lt;= 1</code> where <code>0 &lt;= i &lt; 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 &gt;= 0</code>, and <code>-x</code> otherwise.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 4, index = 2,  maxSum = 6
<strong>Output:</strong> 2
<strong>Explanation:</strong> nums = [1,2,<u><strong>2</strong></u>,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].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 6, index = 1,  maxSum = 10
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= maxSum &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= index &lt; n</code></li>
</ul>

class Solution:
    def maxValue(self, n: int, k: int, maxSum: int) -> int:
        # each element must have value at least 1
        # build a pyramid shape from the ground, increasing in length by 2 (1 on each side)
        # pyramid length maxes out at arr length, and just goes up like tower
        # the peak should be located at given index "k"
        # 1112111
        # 1123211
        # 1234321
        # 2345432 and so on
        
        # 345432 -> n=6, maxSum=21~26, k=2
        
        # 11234 -> n=5, maxSum=12, k=4
        # 112321111 -> n=5, maxSum=16, k=3
        if n == maxSum:
            return 1

        maxSum -= n
        left = k
        right = k
        total = 0
        tallest = 1
        while left >= 0 and right < n:
            total += right - left + 1
            if total > maxSum:
                return tallest
            tallest += 1
            left -= 1
            right += 1
        
        if total == maxSum:
            return tallest

        right -= 1
        left += 1   
        length_base =  right - left  + 1 # take a backstep to where things were in-bound
        # distance left until pyramid hits the other wall
        dist = max(left - 0, n - 1 - right)
        for i in range(dist):
            length_base += 1
            total += length_base
            if total > maxSum:
                return tallest
            tallest += 1
        
        # maxxed out both ends
        # can go up by n each time
        tallest += (maxSum - total) // n
        return tallest
            
        
279