<p>In a garden represented as an infinite 2D grid, there is an apple tree planted at <strong>every</strong> integer coordinate. The apple tree planted at an integer coordinate <code>(i, j)</code> has <code>|i| + |j|</code> apples growing on it.</p>

<p>You will buy an axis-aligned <strong>square plot</strong> of land that is centered at <code>(0, 0)</code>.</p>

<p>Given an integer <code>neededApples</code>, return <em>the <strong>minimum perimeter</strong> of a plot such that <strong>at least</strong></em><strong> </strong><code>neededApples</code> <em>apples are <strong>inside or on</strong> the perimeter of that plot</em>.</p>

<p>The value of <code>|x|</code> is defined as:</p>

<ul>
	<li><code>x</code> if <code>x &gt;= 0</code></li>
	<li><code>-x</code> if <code>x &lt; 0</code></li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/08/30/1527_example_1_2.png" style="width: 442px; height: 449px;" />
<pre>
<strong>Input:</strong> neededApples = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong> A square plot of side length 1 does not contain any apples.
However, a square plot of side length 2 has 12 apples inside (as depicted in the image above).
The perimeter is 2 * 4 = 8.
</pre>

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

<pre>
<strong>Input:</strong> neededApples = 13
<strong>Output:</strong> 16
</pre>

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

<pre>
<strong>Input:</strong> neededApples = 1000000000
<strong>Output:</strong> 5040
</pre>

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

<ul>
	<li><code>1 &lt;= neededApples &lt;= 10<sup>15</sup></code></li>
</ul>

class Solution:
    # 0, 2, 4, 
    # 0, 12, 60
    # side length 2: 2 * (2 + 1 + 2) + 2 * (1) = 2 * 2 + 
    # side length 4: 2 * (4 + 3 + 2 + 3 + 4) + 2 * (3 + 2 + 3)
    # side length x: 4 * (x + x - 1 + ... + (x//2 + 1)) + x + 4 * (x - 1 + ... + (x//2 + 1)) + x
    # = 6x + 8 * (x - 1 + ... + (x//2 + 1))
    # example: x = 2 => 12
    # x = 4 => 24 + 8 * (3) = 48
    # x - 1 + ... + (x//2 + 1) 
    # = (x - 1 +... + 1) - (x//2 + ... + 1)
    # = x*(x - 1)//2 - (x//2)(x//2 + 1)//2
    # = (x(x-1) - (x//2)(x//2 + 1))//2
    # 
    def minimumPerimeter(self, neededApples: int) -> int:
        prev = 0
        currSideLength = 0
        while prev < neededApples:
            currSideLength += 2
            toAdd = 3 * (currSideLength ** 2)
            prev += toAdd
        return currSideLength * 4

749