We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fd60fdd commit c5255bfCopy full SHA for c5255bf
perfect_squares.py
@@ -0,0 +1,17 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+class Solution:
4
+ # @param {int} n a positive integer
5
+ # @return {int} an integer
6
+ def numSquares(self, n):
7
+ # Write your code here
8
+ # Python效率问题,代码翻译成Java可通过。
9
+ cache = [2147483647] * (n + 1)
10
+ cache[0] = 0
11
+ for i in xrange(n + 1):
12
+ for j in xrange(1, n + 1):
13
+ if (i + j * j) <= n:
14
+ cache[i + j * j] = min(cache[i + j * j], cache[i] + 1)
15
+ else:
16
+ break
17
+ return cache[-1]
0 commit comments