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/
随机推荐
- Codeforces 39J Spelling Check hash
主题链接:点击打开链接 意甲冠军: 特定2弦 选择中删除一个字符串的第一个字母,得2个字符串全然同样 问哪些位置能够选 思路: hash求前缀后缀.然后枚举位置 #include <cstdio ...
- Android中判断网络是否连接并提示设置
/** * 判断网络是否连通 * @param context * @return */ public static boolean isNetworkConnected(Context contex ...
- POJ3280 Cheapest Palindrome 【DP】
Cheapest Palindrome Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6013 Accepted: 29 ...
- spring-boot-quartz, 依赖spring-boot-parent good
/** * state的值代表该任务触发器的状态: STATE_BLOCKED 4 // 运行 STATE_COMPLETE 2 //完成那一刻,不过一般不用这个判断Job状态 STATE_ERROR ...
- solr+ Eclipse 4.3+ tomcat 7.5 +winds7(一)
这种方法是我自己依据对tomcat运行项目流程和solr的运行流程来自己弄的,所以有点麻烦,请到原地址查看心血谢谢:http://blog.csdn.net/chunlei_zhang/article ...
- python 快速排序 完整
两头开始 以第一个为基准,从有往左,找第一个比基准数 大的,然后交换 从左往右,找第一个比基准数晓得,然后交换 遍历剩下的 基准数 左边的数们 以及 基准数 右边的数们 def quick_so ...
- Opencv中K均值算法(K-Means)及其在图像分割中的应用
K均值(K-Means)算法是一种无监督的聚类学习算法,他尝试找到样本数据的自然类别,分类是K由用户自己定义,K均值在不需要任何其他先验知识的情况下,依据算法的迭代规则,把样本划分为K类.K均值是最常 ...
- C#中的MessageBox消息对话框
关键字:C# MessageBox 消息对话框 在程序中,我们经常使用消息对话框给用户一定的信息提示,如在操作过程中遇到错误或程序异常,经常会使用这种方式给用于以提示.在C#中,MessageBox消 ...
- Chrome扩展,应用开发学习笔记之2---恶搞百度一下
Chrome扩展,应用开发学习笔记之2 恶搞百度一下 前面我们介绍了一个最简单的chrome扩展时钟,如今我来介绍一下一个恶搞百度一下的chrome扩展程序. 前面说过,manifest.json文件 ...
- 2019 renew 博客目录
.net && .net core Microsoft.AspNet.SignalR实现弹幕(即时通讯) C#调用JS httpclient POST请求(urlencoded) 二维 ...