3 ms solution in C++. Beats 100% of other solutions


  • 0
    P
    class Solution {
    public:
        vector<int> constructRectangle(int area) {
            
            vector<int> res (2,0);
            if(!area)
                return res;
                
            int l = area;
            int w = 1;
            int i = 2;
          
            while(l >= w) {
                
                if ((l*w) == area) {
                    res[0] = l;
                    res[1] = w;
                    
                    if (l == w)
                        break;
                }
                l = area / i;
                if (l)
                    w = area / l;
                else
                    return res;
                    
                i++;
                
            }
            
            return res;
            
        }
    };
    

Log in to reply
 

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