g0401_0500.s0492_construct_the_rectangle.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0401_0500.s0492_construct_the_rectangle;
// #Easy #Math
public class Solution {
/*
Algorithm:
- start with an index i from the square root all the way to 1;
- if at any time, area % i == 0 (so i is a divisor of area), then it's the closest solution.
*/
public int[] constructRectangle(int area) {
int low = (int) Math.sqrt(area);
while (low > 0) {
if (area % low == 0) {
return new int[] {area / low, low};
}
low--;
}
return new int[] {0, 0};
}
}