Skip to content

Commit 5df09c7

Browse files
authored
Merge branch 'sachuverma:master' into patch-2
2 parents 7eff602 + 85783d0 commit 5df09c7

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Given an integer n, return the least number of perfect square numbers that sum to n.
3+
4+
A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1, 4, 9, and 16 are perfect squares while 3 and 11 are not.
5+
6+
7+
8+
Example 1:
9+
10+
Input: n = 12
11+
Output: 3
12+
Explanation: 12 = 4 + 4 + 4.
13+
14+
15+
Example 2:
16+
17+
Input: n = 13
18+
Output: 2
19+
Explanation: 13 = 4 + 9.
20+
21+
22+
Constraints:
23+
24+
1 <= n <= 104
25+
*/
26+
27+
class Solution {
28+
public:
29+
int numSquares(int n) {
30+
vector<int> count(n+1,INT_MAX);
31+
32+
count[0]=0;
33+
for(int i=1;i<=n;i++) {
34+
for(int j=1;j*j<=i;j++) {
35+
count[i]=min(count[i],count[i-j*j]+1);
36+
}
37+
}
38+
39+
return count[n];
40+
41+
42+
}
43+
};

Leetcode Daily Challenge/October-2021/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
| 9. | [Word Search II](https://leetcode.com/problems/word-search-ii/) | [cpp](./9.%20Word%20Search%20II.cpp) |
1414
| 10. | [Bitwise And of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [cpp](./10.%20Bitwise%20AND%20of%20Numbers%20Range.cpp) |
1515
| 11. | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/) | [cpp](./11.%20Diameter%20of%20Binary%20Tree.cpp) |
16+
| 14. | [Perfect Squares](https://leetcode.com/problems/perfect-squares/) | [cpp](./14.%20Perfect%20Squares.cpp) |

0 commit comments

Comments
 (0)