-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPerfectSquare.java
More file actions
64 lines (53 loc) · 1.46 KB
/
PerfectSquare.java
File metadata and controls
64 lines (53 loc) · 1.46 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*https://leetcode.com/problems/perfect-squares/*/
class Solution {
/*int[] sArr;
int count;
LinkedHashSet<Integer> squares;
public int numSquares(int n) {
squares = new LinkedHashSet<Integer>();
int odd = 3, square = 1;
while (square <= n)
{
squares.add(square);
square += odd;
odd += 2;
}
sArr = new int[squares.size()];
int i = 0;
Iterator it = squares.iterator();
while (it.hasNext())
sArr[i++] = (Integer)it.next();
count = Integer.MAX_VALUE;
recur(n,sArr.length-1,0);
return count;
}
public void recur(int n, int index, int c)
{
if (n == 0 || squares.contains(n))
{
if (c < count) count = n == 0 ? c : c+1;
return;
}
else if (n < 0) return;
int newC;
for (int i = index; i >= 0; --i)
recur(n-sArr[i],index,c+1);
}*/
public int numSquares(int n) {
int[] dp = new int[n+1];
for(int i=1;i<=n;i++){
dp[i]=n;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
int sq = j*j;
if(sq>i)
break;
if(dp[i] > (1+dp[i-sq])){
dp[i] = 1+dp[i-sq];
}
}
}
return dp[n];
}
}