Task description

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).
 
 
Solution

 
Programming language used: Java
Code: 00:48:19 UTC, java, final, score:  100
// 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/

随机推荐

  1. visual studio 2015安装win10 SDK文件校验错误的解决方法

    这段时间想看一下UWP开发,于是下载了visual 2015 update1社区版,但是在安装完成时提示,windows 10 sdk文件校验码错误,如下图(图片是借用的,我当时没有截屏,错误号是一样 ...

  2. 关于js的window.open()

    window.open是javascript函数,该函数的作用是打开一个新窗口或这改变原来的窗口,不过一般用来的是打开新窗口,因为修改原来的网页地址,可以有另一个函数,那就是window.locati ...

  3. QComboBox实现复选功能(三种方法:嵌套QListWidget, 设置QStandardItemModel, 设置Delegate)

    今天介绍一下一个小东西 — 如何让QComboBox实现复选功能?   需求: 下拉列表有复选功能 不可编辑 显示所有选中项   关于QComboBox的复选功能有几种方案: QStandardIte ...

  4. MyEclipse使用汇总——MyEclipse10设备SVN插入

    一.下载SVN插件subclipse 下载地址:folderID=2240" style="color:rgb(7,93,179)">http://subclips ...

  5. zedboard之GPIO驱动器(离FPGA直到LINUX申请书)

    笔者:xiabodan   资源: http://blog.csdn.net/xiabodan/article/details/24308373 1 EDK 大家知道我们在EDK中建立GPIO然后倒出 ...

  6. 使用Python破解验证码

    Keywords: python captcha Most people don’t know this but my honours thesis was about using a compute ...

  7. windows 的使用 —— 注册表(软件的安装和卸载)

    win + r(run):输入 regedit(register edit)进入: 1. 网络连接 比如一些 vpn 安装之后,会对网络连接进行一定的修改,这样在 vpn 工具删除之后,仍然无法消除修 ...

  8. python 教程 第十一章、 异常

    第十一章. 异常 1)    try/except/else格式 try: s = raw_input('--> ') except EOFError: print 'Why did you d ...

  9. Matlab随笔之模拟退火算法

    问题描述: 我方有一个基地,经度和纬度为( 70,40).假设我方飞机的速度为 1000 公里/小时. 我方派一架飞机从基地出发,侦察完敌方所有目标,再返回原来的基地.在敌方每一目 标点的侦察时间不计 ...

  10. WCF服务的IIS托管(应用程序)

    基本思路 建立与发布参考网站托管 在IIS中某一网站,选择添加应用程序   访问服务uri:http://localhost/wcfAppTest/Service1.svcwcfAppTest/Ser ...