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/
随机推荐
- Lettcode_104_Maximum Depth of Binary Tree
本文研究的总结,欢迎转载,但请注明出处:http://blog.csdn.net/pistolove/article/details/41964475 Maximum Depth of Binary ...
- Struts2——(3)ValueStack(值栈)
一.ValueStack 主要用于存储请求相关信息,内部结构如下 root区:被称为根存储区,是一个栈结构,栈顶元素为当前请求的Action对象. context区:被称为变量存储区,是一个Map结构 ...
- 前端常见算法JS实现
算法是程序的灵魂,一个优秀的前端工程师对算法也是要有所了解的. 排序算法 1. 冒泡排序 //冒泡排序 function bubbleSort(arr){ var i = j = 0; for(i=1 ...
- andriod 实现新浪、QQ场地、朋友微信圈、微信朋友分享功能
前言:在自己的学习过程中的一些操作,分享一些理解. 下面将说明什么: 下载链接:http://download.csdn.net/detail/u014608640/7490357 首先.我们须要去S ...
- HDU 1143 Tri Tiling (递推)
Tri Tiling Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- node lesson4--eventproxy不懂
var express = require('express'); var superagent = require('superagent'); var cheerio = require('che ...
- Android系统联系人全特效实现(下),字母表快速滚动
在上一篇文章中,我和大家一起实现了类似于Android系统联系人的分组导航和挤压动画功能,不过既然文章名叫做<Android系统联系人全特效实现>,那么没有快速滚动功能显然是称不上&quo ...
- C# WPF 低仿网易云音乐(PC)歌词控件
原文:C# WPF 低仿网易云音乐(PC)歌词控件 提醒:本篇博客记录了修改的过程,废话比较多,需要项目源码和看演示效果的直接拉到文章最底部~ 网易云音乐获取歌词的api地址 http://music ...
- Swagger 生成API文档
1.打开程序包管理控制台输入: Install-Package Swashbuckle 2.打开App_Start文件夹下的SwaggerConfig.cs文件找到 c.IncludeXmlComme ...
- 黑科技 —— OTG 线
OTG:On-The-Go的缩写: OTG 可将鼠标.键盘等一切终端(terminal)连接起来: OTG 技术就是实现在没有 Host 的情况下,实现从设备间的数据传送.(支持传送,显然也支持要求更 ...