MinPerimeterRectangle

preview_player
Показать описание
MinPerimeterRectangle is a Codility task where the goal is to find the minimum perimeter of a rectangle covering a specified area. The sides of the rectangle need to consist of whole numbers only.

This is my Java solution to MinPerimeterRectangle that scores 100%.
Рекомендации по теме
Комментарии
Автор

Hello Dave,
I modified your code a bit and the following worked for me:
class Solution {
public int solution(int N) {
for(int i= (int)Math.ceil(Math.sqrt(N)); i>=1; i--) {
if((N % i) == 0){
return (i*2 + (N/i)*2);
}
}
return 0;
}
}
So, in essence, if the value of "i" is able to return zero as remainder then we have found the first side which is closest to the square root of the given area N and then the 2nd side will be N/i.
Thanks for your code which helped a lot.

abhishekomprakash