-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathlc279.java
More file actions
30 lines (29 loc) · 837 Bytes
/
lc279.java
File metadata and controls
30 lines (29 loc) · 837 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
package code;
/*
* 279. Perfect Squares
* 题意:给定一个数,求该数最少可以由几个平方数的和组成
* 难度:Medium
* 分类:Math, Dynamic Programming, Breadth-first Search
* 思路:dp[i] = dp[i-j*j] +1
* Tips:lc322
*/
import java.util.Arrays;
public class lc279 {
public static void main(String[] args) {
System.out.println(numSquares(12));
}
public static int numSquares(int n) {
int[] dp = new int[n];
Arrays.fill(dp,Integer.MAX_VALUE);
for (int i = 1; i <= n ; i++) { //两个for循环
for (int j=1; j*j<=i ; j++) {
if(j*j==i)
dp[i-1] = 1;
if(j*j<i){
dp[i-1] = Math.min(dp[i-1-j*j]+1,dp[i-1]);
}
}
}
return dp[n-1];
}
}