C++_AC


  • 0
    class Solution {
    public:
    vector<int> constructRectangle(int area) {
        if(area <= 0) return {};
        vector<int> res(2);
        int L = area, W = 1;
        int dif = area - 1;
        for(int i = 1; i <= sqrt(area); ++i){
            int l = area/i;
            if(l * i == area && l >= i && l - i < dif){
                L = l;
                W = i;
                dif = l - i;
            }
        }
        res[0] = L, res[1] = W;
        return res;
    }
    };

  • 0

    @jasonshieh said in C++_AC:

    l >= i && l - i < dif

    Since it's looping from bottom up, the above condition is always true anyway. Actually, you could loop top down from square root to return faster.


Log in to reply
 

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