Codlility---MinPerimeterRectangle
An integer N is given, representing the area of some rectangle.
The area of a rectangle whose sides are of length A and B is A * B, and theperimeter is 2 * (A + B).
The goal is to find the minimal perimeter of any rectangle whose area equals N. The sides of this rectangle should be only integers.
For example, given integer N = 30, rectangles of area 30 are:
- (1, 30), with a perimeter of 62,
- (2, 15), with a perimeter of 34,
- (3, 10), with a perimeter of 26,
- (5, 6), with a perimeter of 22.
Write a function:
class Solution { public int solution(int N); }
that, given an integer N, returns the minimal perimeter of any rectangle whose area is exactly equal to N.
For example, given an integer N = 30, the function should return 22, as explained above.
Assume that:
- N is an integer within the range [1..1,000,000,000].
Complexity:
- expected worst-case time complexity is O(sqrt(N));
- expected worst-case space complexity is O(1).
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N) {
// write your code in Java SE 8
int max_perimeter = 2*(1+N);
for(int i=2; i < Math.sqrt(N)+1; i++) {
if(N%i == 0) {
max_perimeter = Math.min(max_perimeter, 2*(i+N/i));
}
}
return max_perimeter;
}
}
https://codility.com/demo/results/training8VZXU6-PUT/
随机推荐
- Risk Adaptive Information Flow Based Access Control
Systems and methods are provided to manage risk associated with access to information within a given ...
- Android二维码功能实现
最近二维码真是越来越火了,随便电视上.网络上.商场里,到处都是二维码.而内嵌二维码扫描功能的软件也越来越多,QQ.微信.UC浏览器等等应用都可以对着二维码扫一扫,感觉我们自己的应用里不加上二维码扫描功 ...
- VS编译环境中TBB配置和C++中lambda表达式
TBB(Thread Building Blocks),线程构建模块,是由Intel公司开发的并行编程开发工具,提供了对Windows,Linux和OSX平台的支持. TBB for Windows ...
- wxWindows
用C++编写跨平台程序 中文版说明 本教程由Gxl117翻译并将继续维护,这是本教程的第一稿,假设发现错误请与我(Email:gxl117@yahoo.com.cn)联系让我能及时修正它.之后还会对这 ...
- AI2XAML's Bug
原文:AI2XAML's Bug My picture is like this: I use Adobe Illustator CS to draw the outline of that, I s ...
- codeforces Round #259(div2) D解决报告
D. Little Pony and Harmony Chest time limit per test 4 seconds memory limit per test 256 megabytes i ...
- StreamDM:基于Spark Streaming、支持在线学习的流式分析算法引擎
StreamDM:基于Spark Streaming.支持在线学习的流式分析算法引擎 streamDM:Data Mining for Spark Streaming,华为诺亚方舟实验室开源了业界第一 ...
- ASP.NET Core 静态文件 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 静态文件 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 静态文件 前几章节中,我们学习了 ASP.NET Core 的中间件 ...
- Android 4.0屏蔽式多点触摸
比方这张图.我想不接或者接单,二者仅仅能点一个,不能同一时候点击,否则会造成混乱.我们仅仅要在嵌套他们俩的布局中增加这么一段话: android:splitMotionEvents="fal ...
- 设置oracle密码不过期,修改用户密码
1. 查看用户名使用的profile select username,profile from dba_usersSELECT * FROM dba_profiles WHERE profile='D ...