Minimum Garden Perimeter to Collect Enough Apples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
    def minimumPerimeter(self, neededApples: int) -> int:
        # Start with side length 1 and incrementally increase it
        side_length = 1

        # Current apples will store the number of apples inside or on the perimeter of the plot
        current_apples = 0

        # Loop until the current_apples is less than neededApples
        while current_apples < neededApples:
            # The number of apples added to the plot at each step is 12 * side_length * side_length
            current_apples += 12 * side_length * side_length

            # If the current_apples is greater or equal to neededApples, break the loop
            if current_apples >= neededApples:
                break

            # Increment the side length
            side_length += 1

        # The perimeter is 8 * side_length
        return 8 * side_length