Skip to content
LC-0279 Medium LeetCode

279. Perfect Squares

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 56% Topics: Math, Dynamic Programming, Breadth-First Search
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n * sqrt(n))
# Space: O(n)

class Solution(object):
    _num = [0]
    def numSquares(self, n):
        """
        :type n: int
        :rtype: int
        """
        num = self._num
        while len(num) <= n:
            num += min(num[-i*i] for i in xrange(1, int(len(num)**0.5+1))) + 1,
        return num[n]

Solution from kamyu104/LeetCode-Solutions · MIT