JAVA recursive solution. short code. [8ms]


  • 0
    Q

    '''
    public class Solution {
    int minLen;
    public int numSquares(int n) {
    minLen = n;
    helper(n, n, 1);
    return minLen;
    }
    public void helper(int n, int maxNum, int level){
    if(level>=minLen) return;
    int sqrtNum = (int) Math.floor(Math.sqrt(n));
    int rem = n - sqrtNumsqrtNum;
    if(rem==0){
    if(minLen>level){
    minLen = level;
    }
    }
    int temp = Math.min(maxNum,sqrtNum);
    for(int i = temp; i >= (int)Math.floor(Math.sqrt(n/minLen)); i--){
    helper(n-i
    i, i, level+1);
    }

    }
    

    }
    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.