A web developer needs to know how to design a web page's size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:
W should not be larger than the length L, which means L >= W.L and width W should be as small as possible.Return an array [L, W] where L and W are the length and width of the web page you designed in sequence.
Example 1:
Input: area = 4 Output: [2,2] Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1]. But according to requirement 2, [1,4] is illegal; according to requirement 3, [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2.
Example 2:
Input: area = 37 Output: [37,1]
Example 3:
Input: area = 122122 Output: [427,286]
Constraints:
1 <= area <= 107The brute force approach involves iterating over all possible width values starting from 1 up to the square root of the area. For each width that is a divisor of the area, calculate the length as area divided by this width. Since the width increases and we're calculating length from it, L >= W condition is inherently satisfied. The pair with the smallest difference between L and W is the result.
The solution starts by iterating from the square root of the area down to 1. For each potential width, it checks if it divides the area perfectly. If it does, it computes the corresponding length and returns the pair since it assures L >= W by starting from a high width and going down.
JavaScript
Java
C#
C++
C
Time Complexity: O(sqrt(n)), where n is the area because we only check divisors up to the square root.
Space Complexity: O(1), as no additional space dependent on input size is used.
We optimize the brute force by reducing unnecessary checks. Instead of starting from a width value of 1, directly jump to sqrt(area) and move inwards. This ensures that we're only exploring potential dimensions that are likely close together, thereby reducing unnecessary loops. Utilize integer calculations which grant more consistent performance on larger numbers.
Improveys the initial search space by starting specifically at sqrt(area) and uses a decremental loop. This assures the detection of the closest dimension pair while reducing iteration count.
JavaScript
Java
C#
C++
C
Time Complexity: O(sqrt(n)), due to the similar bounds of iteration depending on the size of 'area'.
Space Complexity: O(1)
| Approach | Complexity |
|---|---|
| Brute Force Factorization | Time Complexity: O(sqrt(n)), where n is the area because we only check divisors up to the square root. |
| Optimized Factor Search | Time Complexity: O(sqrt(n)), due to the similar bounds of iteration depending on the size of 'area'. |
LARGEST RECTANGLE IN HISTOGRAM - Leetcode 84 - Python • NeetCode • 287,868 views views
Watch 9 more video solutions →Practice Construct the Rectangle with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor