Construct the Rectangle

I chose the following challenge from LeetCode: https://leetcode.com/problems/construct-the-rectangle/ and gave it a try.

f interested please follow the link to get the requirements.

The following is a screen capture of the console using the sample data:

area:        4 = [L:        2 W:        2]

Please note that the challenge only requires the method constructRectangle(). The test cases were generated by me.

Additional output from the console for additional tests follows:

T: 20

area:  4645697 = [L:     6571 W:      707]

area:  2005647 = [L:    95507 W:       21]

area:  2214302 = [L:     1702 W:     1301]

area:  4747927 = [L:  4747927 W:        1]

area:  2370446 = [L:     2249 W:     1054]

area:  5307843 = [L:  1769281 W:        3]

area:  2757876 = [L:     2244 W:     1229]

area:   304737 = [L:      647 W:      471]

area:  2263556 = [L:   565889 W:        4]

area:  5881768 = [L:    15643 W:      376]

area:  5462574 = [L:     2374 W:     2301]

area:  7389674 = [L:     2797 W:     2642]

area:  9255165 = [L:   617011 W:       15]

area:  7937901 = [L:     8563 W:      927]

area:  3970482 = [L:     2326 W:     1707]

area:  7263076 = [L:     7793 W:      932]

area:  3450115 = [L:     2185 W:     1579]

area:  2690452 = [L:    10039 W:      268]

area:  8628231 = [L:   169181 W:       51]

area:  6237533 = [L:    18509 W:      337]

My solution in Java using Eclipse IDE:

import java.util.Random;

import java.util.Scanner;

public class Solution {

// **** ****

final static int     MAX_AREA      = 10000000;

/**

*

*/

static int[] constructRectangle(int area) {

// **** ****

int dif, l, side, w;

int[] dim     = new int[2];

int bestW     = 0;

int bestL     = 0;

int minDif    = Integer.MAX_VALUE;

side = (int)Math.sqrt(area);

//            System.out.println(“<<< side: ” + side);

for (l = 1; l <= side; l++) {

// **** rectangle dimensions must match area ****

if ((area % l) != 0) {

continue;

}

// **** ****

w      = area / l;

dif = Math.abs(l – w);

//                   System.out.printf(“<<< l: %d w: %d dif: %d\n”, l, w, dif);

// **** minimum difference between L and W ****

if (dif < minDif) {

minDif = dif;

// **** L >= W ****

if (w > l) {

bestL = w;

bestW = l;

} else {

bestL = l;

bestW = w;

}

}

}

// **** ****

dim[0] = bestL;

dim[1] = bestW;

// **** ****

return dim;

}

/**

*

* @param args

*/

public static void main(String[] args) {

Random rand = new Random();

Scanner sc    = new Scanner(System.in);

System.out.print(“T: “);

int T  = sc.nextInt();

int area;

for (int t = 0; t < T; t++) {

// **** ****

area = rand.nextInt(MAX_AREA) + 1;

//                   area = 4;

// **** ****

int[] dims = constructRectangle(area);

if (area != dims[0] * dims[1]) {

System.out.println(“<<< INCORRECT dimensions !!!”);

System.exit(-1);

} else {

System.out.printf(“area: %8d = [L: %8d W: %8d]\n”, area, dims[0], dims[1]);

}

}

sc.close();

}

}

If you have comments or questions regarding this or any other entry in this blog please do not hesitate and send me a message. I will not use your name unless you explicitly allow me to do so.

John

john.canessa@gmail.com

Follow me on Twitter: @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.