forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMLE_dp_nsqrtn.py
More file actions
31 lines (26 loc) · 787 Bytes
/
Copy pathMLE_dp_nsqrtn.py
File metadata and controls
31 lines (26 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: illuz <iilluzen[at]gmail.com>
# File: MLE_dp_nsqrtn.py
# Create Date: 2015-10-26 11:03:22
import math
class Solution(object):
def helper(self, n):
if self.result[n] != 0:
return self.result[n]
if n in self.square:
return 1
self.result[n] = min(1 + self.helper(n - sqr) for sqr in self.square if sqr <= n)
return self.result[n]
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
self.result = [0 for _ in xrange(n + 1)]
self.square = [s**2 for s in xrange(1, int(math.sqrt(n))+1)]
return self.helper(n)
s = Solution()
print(s.numSquares(12))
print(s.numSquares(13))
print(s.numSquares(7168))