Skip to content

Commit f874fde

Browse files
committed
New Problem "Perfect Squares"
1 parent 1ba3b95 commit f874fde

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LeetCode
1313
|287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [C++](./algorithms/cpp/findTheDuplicateNumber/findTheDuplicateNumber.cpp)|Hard|
1414
|285|[Inorder Successor in BST](https://leetcode.com/problems/inorder-successor-in-bst/) ♥ | [Java](./algorithms/java/src/inorderSuccessorInBST/inorderSuccessorInBST.java)|Medium|
1515
|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [C++](./algorithms/cpp/moveZeroes/moveZeroes.cpp)|Easy|
16+
|279|[Perfect Squares](https://leetcode.com/problems/perfect-squares/) | [C++](./algorithms/cpp/perfectSquares/PerfectSquares.cpp)|Medium|
1617
|278|[First Bad Version](https://leetcode.com/problems/first-bad-version/)| [C++](./algorithms/cpp/firstBadVersion/FirstBadVersion.cpp), [Java](./algorithms/java/src/firstBadVersion/firstBadVersion.java)|Easy|
1718
|274|[H-Index](https://leetcode.com/problems/h-index/)| [C++](./algorithms/cpp/h-Index/h-Index.cpp)|Medium|
1819
|273|[Integer to English Words](https://leetcode.com/problems/integer-to-english-words/)| [C++](./algorithms/cpp/integerToEnglishWords/IntegerToEnglishWords.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Source : https://leetcode.com/problems/perfect-squares/
2+
// Author : Hao Chen
3+
// Date : 2015-10-25
4+
5+
/***************************************************************************************
6+
*
7+
* Given a positive integer n, find the least number of perfect square numbers (for
8+
* example, 1, 4, 9, 16, ...) which sum to n.
9+
*
10+
* For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2
11+
* because 13 = 4 + 9.
12+
*
13+
* Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating
14+
* all test cases.
15+
*
16+
***************************************************************************************/
17+
18+
19+
class Solution {
20+
public:
21+
22+
int numSquares(int n) {
23+
return numSquares_dp_opt(n); //12ms
24+
return numSquares_dp(n); //232ms
25+
}
26+
27+
/*
28+
* Dynamic Programming
29+
* ===================
30+
* dp[0] = 0
31+
* dp[1] = dp[0]+1 = 1
32+
* dp[2] = dp[1]+1 = 2
33+
* dp[3] = dp[2]+1 = 3
34+
* dp[4] = min{ dp[4-1*1]+1, dp[4-2*2]+1 }
35+
* = min{ dp[3]+1, dp[0]+1 }
36+
* = 1
37+
* dp[5] = min{ dp[5-1*1]+1, dp[5-2*2]+1 }
38+
* = min{ dp[4]+1, dp[1]+1 }
39+
* = 2
40+
* dp[6] = min{ dp[6-1*1]+1, dp[6-2*2]+1 }
41+
* = min{ dp[5]+1, dp[2]+1 }
42+
* = 3
43+
* dp[7] = min{ dp[7-1*1]+1, dp[7-2*2]+1 }
44+
* = min{ dp[6]+1, dp[3]+1 }
45+
* = 4
46+
* dp[8] = min{ dp[8-1*1]+1, dp[8-2*2]+1 }
47+
* = min{ dp[7]+1, dp[4]+1 }
48+
* = 2
49+
* dp[9] = min{ dp[9-1*1]+1, dp[9-2*2]+1, dp[9-3*3] }
50+
* = min{ dp[8]+1, dp[5]+1, dp[0]+1 }
51+
* = 1
52+
* dp[10] = min{ dp[10-1*1]+1, dp[10-2*2]+1, dp[10-3*3] }
53+
* = min{ dp[9]+1, dp[6]+1, dp[1]+1 }
54+
* = 2
55+
* ....
56+
*
57+
* So, the dynamic programm formula is
58+
*
59+
* dp[n] = min{ dp[n - i*i] + 1 }, n - i*i >=0 && i >= 1
60+
*
61+
*/
62+
int numSquares_dp(int n) {
63+
if ( n <=0 ) return 0;
64+
65+
int *dp = new int[n+1];
66+
dp[0] = 0;
67+
68+
for (int i=1; i<=n; i++ ) {
69+
int m = n;
70+
for (int j=1; i-j*j >= 0; j++) {
71+
m = min (m, dp[i-j*j] + 1);
72+
}
73+
dp[i] = m;
74+
}
75+
76+
return dp[n];
77+
delete [] dp;
78+
}
79+
80+
//using cache to optimize the dp algorithm
81+
int numSquares_dp_opt(int n) {
82+
if ( n <=0 ) return 0;
83+
84+
static vector<int> dp(1, 0);
85+
if (dp.size() >= (n+1) ) return dp[n];
86+
87+
for (int i=dp.size(); i<=n; i++ ) {
88+
int m = n;
89+
for (int j=1; i-j*j >= 0; j++) {
90+
m = min (m, dp[i-j*j] + 1);
91+
}
92+
dp.push_back(m);
93+
}
94+
95+
return dp[n];
96+
}
97+
};
98+

0 commit comments

Comments
 (0)