forked from selfboot/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path279_PerfectSquares.py
executable file
·40 lines (34 loc) · 1016 Bytes
/
279_PerfectSquares.py
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
32
33
34
35
36
37
38
39
40
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
# Dynamic Programming with static variable
class Solution(object):
# Since dp is a static vector, we have already calculated the result
# during previous function calls and we can just return the result now.
_dp = [0]
def numSquares(self, n):
dp = self._dp
while len(dp) <= n:
i = len(dp)
min_count = 2 ** 31 - 1
for j in range(1, int(i**0.5) + 1):
min_count = min(min_count, dp[i-j*j]+1)
dp.append(min_count)
return dp[n]
# Dynamic Programming
# Easy to undersrtand but unfortually "Time Limit Exceeded"
class Solution_2(object):
def numSquares(self, n):
dp = [0] * (n+1)
for i in range(1, n+1):
min_count = 2 ** 31 - 1
for j in range(1, int(i**0.5) + 1):
min_count = min(min_count, dp[i-j*j]+1)
dp[i] = min_count
return dp[n]
"""
1
12
13
156
"""