Minimum Garden Perimeter to Collect Enough Apples LeetCode solution in Python

1
class Solution:
2
def minimumPerimeter(self, nap: int) -> int:
3

4
# here for n = 2 , there are two series :
5
# (1) Diagnal points for n=3 , diagnal apples = 2*n = 6
6
# (2) there is series = 2,3,3 = 2+ (sigma(3)-sigma(2))*2
7

8
# how to solve:
9

10
# here 3 = sigma(n+(n-1))-sigma(n) = sigma(2*n-1)-sigma(n) = 0.5*2n*(2n-1)-0.5*n*n-1
11
# (3) so our final 2,3,3 = 3*2+2 = (0.5*2n*(2n-1)-0.5*n*n-1)*2+n
12
# (4) so final 2,3,3 = 3*n*n - 2*n
13
# (5) we have 4 times repitation of (2,3,3) = 4*(2,3,3) = 4*(3*n*n - 2*n) = 12*n*n - 8*n
14
# (6) we have 4 diagnal points so their sum(4 diagnal) = 4*(2*n)
15
# (7) so final sum(total) = 4 diagnal sum + 4(2,3,3) = 4(2*n) + 12*n*n - 8*n = 12*n*n
16

17
# so at nth distance we have total 12*n*n apples at the circumfrance
18

19
# so net sum = sigma(12*n*n) = 2*n*(n+1)*(2*n+1)
20

21
n = 1
22
val = 2 * n * (n + 1) * (2 * n + 1)
23
while val < nap:
24
n += 1
25
val = val = 2 * n * (n + 1) * (2 * n + 1)
26
return n * 8

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0