C solution take O(n^1/2) time:


  • 0
    M

    int* constructRectangle(int area, int* returnSize) {
    int i,diff;
    int *result = (int )malloc(2sizeof(int));
    result[0] = area;
    result[1] = 1;
    diff = area - 1;
    *returnSize = 2;
    for( i = 2;i<=sqrt(area);i++)
    {
    if(area%i == 0 && area/i-i<diff)
    {
    result[0] = area/i;
    result[1] = i;
    }
    }
    return result;
    }


Log in to reply
 

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