Beautiful 8 Lines Java Solution


  • 29
    C
    public class Solution {
        public int numSquares(int n) {
            int[] record = new int[n+1];
            for(int i=0;i<=n;i++){
                record[i] = i;
                for(int j=1;j*j<=i;j++){
                    record[i] = Math.min(record[i-j*j]+1,record[i]);
                }
            }
            return record[n];
        }
    }

Log in to reply
 

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