Small DP Solution, Java


  • 2
    public int numSquares(int n) {
            int [] dp = new int[n+1];
            dp[0]=0;
            for(int i=1;i<=n;i++){
                int min=Integer.MAX_VALUE;
                for(int j=1;j<=Math.sqrt(i);j++){
                    min = Math.min(min,dp[i-(j*j)]+1);
                }
                dp[i]=min;
            }
            return dp[n];
        }
    

Log in to reply
 

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