236ms c++


  • 0
    B
    class Solution {
    public:
        int numSquares(int n)
        {
            if (n==0) return 0;
            vector<int> per;
            int f[n+5]={0};
            f[1]=1;
            for(int i=2;i<=n;i++)
            {
                int minV=INT_MAX;
                int j=1;
                while (j*j<=i)
                {
                    if (j*j==i)
                    {
                        minV=1;break;
                    }
                    minV=min(minV,f[i-j*j]+1);
                    j++;
                }
                f[i]=minV;
            }
            return f[n];
        }
    };
    

Log in to reply
 

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