## aj

BAN USER
Comments (2)

Reputation 0

Page:

1

Comment hidden because of low score. Click to expand.

Comment hidden because of low score. Click to expand.

0

of 0 vote

public int calculateSquares(int n)

{

if(n<0){return -1;}

HashMap<Integer, Integer> leastSquaredmap = new HashMap();

map.put(0,1);

map.put(1,1);

return calculateSquaresHelper(n, map);

}

public int calculateSquaresHelper(int n, HashMap<Integer, Integer> map)

{

if(map!=null && map.contains(n))

{

return map.get(n);

}

//number is a perfect square

if(Math.sqrt(n) % 1 ==0)

{

map.put(n,1);

}

int min = Math.INT_MAX;

for(int i=1; i<=n; i++)

{

int j=n-i;

int minI = calculateSquaresHelper(i,map);

int minJ = calculateSquaresHelper(j,map);

if(minI+minJ < min)

{

min = minI+minJ;

}

}

map.put(n, min);

return min;

}

Page:

1

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window

- aj March 10, 2016