O(n^1/2) without sqrt calling


  • 1
    Y

    class Solution {
    public:
    vector<int> constructRectangle(int area) {
    vector<int> res;
    res.push_back(area);
    res.push_back(1);
    int target = 0;

        for(int l = 1 ; l * l<=area ; l++)
        {
            if(area % l == 0)
            {
                    res[0] = l;
                    res[1] = area / l;
            }
    
        }
        
        if(res[0] < res[1])
            std::swap(res[0],res[1]);
        return res;
    }
    

    };


Log in to reply
 

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