⏹️Perfect Sqaures
Question (LC.279)
Examples
I: n = 12
O: 3 (4 + 4 + 4)
I: n = 13
O: 2 (9 + 4)Approach
Brute Force Recursion
def numSquares(self, n: int) -> int:
min_count = self.search_squares_helper(n)
return min_count
def search_squares_helper(self, n: int) -> int:
# base case
if n == 0:
return 0
# recursive calls
float_root = math.sqrt(n)
biggest_root = int(math.floor(float_root))
min_count = 10001
for root in range(biggest_root, 0, -1):
count = 1 + self.search_squares_helper(n - root**2)
min_count = min(count, min_count)
return min_countMemoized Recursion
DP
Reading
Last updated