3 line C++ clean solution with Explanation

  • 8
    1. Because question requires L, D as close as possible, I start the finding from the middle point which is sqrt(area).
    2. when the Area divide Width have remainder 0, it should be the solution
    vector<int> constructRectangle(int area) {
            for(int mid = sqrt(area); mid>0; mid--)
                if (!(area%mid))
                    return {area/mid, mid};

Log in to reply

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