-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerfectSquares.java
41 lines (34 loc) · 942 Bytes
/
PerfectSquares.java
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
41
package org.sean.dynamicpro;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/***
* 279. Perfect Squares
*/
public class PerfectSquares {
public int numSquares(int n) {
int sqr = (int) Math.sqrt(n);
if (sqr * sqr == n) {
return 1;
}
List<Integer> squares = new ArrayList<>();
for (int i = 1; i <= sqr; i++) {
squares.add(i * i);
}
int[] dp = new int[n + 1]; // min elements summed to dp[i]
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int i = 1; i <= n; i++) {
for (int elem : squares) {
if (elem == i) {
dp[i] = 1;
} else if (elem < i) {
dp[i] = Math.min(dp[i], dp[i - elem] + 1);
} else {
break;
}
}
}
return dp[n];
}
}