Java implementation LeetCode 492 rectangular structure

492. rectangular structure

As a web developer, you know how to plan the size of a page is very important. Now given a specific rectangular area of ​​the page, your task is to design a length L and a width W satisfying the following requirements of rectangular pages. Claim:

  1. Your page must be equal to the design of rectangular given target area.

  2. The width W should not exceed a length L, in other words, requires L> = W.

  3. The gap between the length L and width W should be as small as possible.
    You need to output length L and width design your page order W.

Example:

Input: 4
Output: [2, 2]
Explanation: the target area is 4, the configuration program has all possible [1,4], [2,2], [4,1].
However, according to claims 2, [1,4] does not meet the requirements; according to claim 3, [2,2] meet the requirements of more than [1/4] the length L of the output 2, the width W 2.
Description:

Not larger than a given positive integer and 10,000,000.
The length and width of your page must be designed are positive integers.

class Solution {
      public int[] constructRectangle(int area) {
        int sqrt=(int)Math.sqrt(area); 
        while( area%sqrt!=0 ){ 
            sqrt--;
        }
        return new int[]{area/sqrt,sqrt};
    }
}
Released 1590 original articles · won praise 20000 + · views 2.49 million +

Guess you like

Origin blog.csdn.net/a1439775520/article/details/105012390