-
Notifications
You must be signed in to change notification settings - Fork 6
/
PerfectSquares.java
40 lines (29 loc) · 987 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
package com.shivaprasad.february.day45;
public class PerfectSquares {
public static void main(String[] args) {
System.out.println(numSquares(13));
}
public static int numSquares(int n) {
double d = Math.sqrt(n);
int s = (int) Math.floor(d);
if(n == s*s)
return 1;
int[][] squares = new int[s+1][n+1];
return minSquares(2,n,squares);
}
static int minSquares(int currentIndex,int s,int[][] memo)
{
if(s< currentIndex*currentIndex)
return s;
if(memo[currentIndex][s]!=0)
return memo[currentIndex][s];
int consider = 10001;
if(currentIndex * currentIndex <= s)
{
consider = 1 + minSquares(currentIndex,s-currentIndex*currentIndex,memo);
}
int notConsider = minSquares(currentIndex + 1,s,memo);
memo[currentIndex][s] = Math.min(consider,notConsider);
return memo[currentIndex][s];
}
}