[LeetCode&Python] Problem 492. Construct the Rectangle
For a web developer, it is very important to know how to design a web page's size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:
1. The area of the rectangular web page you designed must equal to the given target area.
2. The width W should not be larger than the length L, which means L >= W.
3. The difference between length L and width W should be as small as possible.
You need to output the length L and the width W of the web page you designed in sequence.
Example:
Input: 4
Output: [2, 2]
Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1].
But according to requirement 2, [1,4] is illegal; according to requirement 3, [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2.
Note:
- The given area won't exceed 10,000,000 and is a positive integer
- The web page's width and length you designed must be positive integers.
class Solution(object):
def constructRectangle(self, area):
"""
:type area: int
:rtype: List[int]
"""
min1=area-1
ans=[area,1]
i=2
while i*i<=area:
if area%i==0:
if area/i-i<min1:
min1=area/i-i
ans[0]=area/i
ans[1]=i
i+=1
return ans
[LeetCode&Python] Problem 492. Construct the Rectangle的更多相关文章
- [LeetCode&Python] Problem 427. Construct Quad Tree
We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...
- [LeetCode&Python] Problem 606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- 【leetcode】492. Construct the Rectangle
problem 492. Construct the Rectangle 参考 1. Leetcode_492. Construct the Rectangle; 完
- 【LeetCode】492. Construct the Rectangle 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 python解法 日期 题目地址:ht ...
- LeetCode - 492. Construct the Rectangle
For a web developer, it is very important to know how to design a web page's size. So, given a speci ...
- [LeetCode&Python] Problem 836. Rectangle Overlap
A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...
- LeetCode: 492 Construct the Rectangle(easy)
题目: or a web developer, it is very important to know how to design a web page's size. So, given a sp ...
- [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode&Python] Problem 387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
随机推荐
- JBOSS禁用delete和put方法教程
一.背景说明(与此节修复没多大关系可跳过) 今天应用报扫描出“启用不安全的HTTP方法”漏洞需要进行修复,看后边还有IIS的修复建议:一边不满怎么用IIS一边研究了具体操作半天,由于IIS不同版本操作 ...
- redis sentinel哨兵模式集群搭建教程
1.环境说明 我们将使用192.168.220.128.192.168.220.129两台机器搭建sentinel交叉主从为例 当前我们已在192.168.220.128上按redis安装教程安装了r ...
- html5游戏之Box2d物理引擎集成
前面两章我们已经研究了如何使用Box2d来模拟游戏世界,这一章就把所有的东西拼凑在一起,最终完成我们的游戏. 一.定义物体 典型的物体: {type:'ground',name:'dirt',x:50 ...
- python 类方法中参数使用默认值的方法
class A(): __init__(self, **arg): self.__dict__.update(arg) def M(self, config=None, section= ...
- day13-类的重写和类的私有方法
类的重写 在python中 有时需要进行重写,重写是继承机制中的一个重要部分, 可以重写一般方法也可以重写构造方法,构造方法是用来初始化新创建对象的状态. class A : def hello(se ...
- Delphi I/O error 103 错误
http://stackoverflow.com/questions/634587/delphi-why-do-i-sometimes-get-an-i-o-error-103-with-this-c ...
- L1-056 猜数字
一群人坐在一起,每人猜一个 100 以内的数,谁的数字最接近大家平均数的一半就赢.本题就要求你找出其中的赢家. 输入格式: 输入在第一行给出一个正整数N(≤104).随后 N 行,每行给出一个玩 ...
- centos7安装配置zabbix4.0
zabbix01 198.8.8.211 zabbix-server4.0 zabbix02 198.8.8.212 zabbix-agent4.0 一:zabbix服务端环境 ...
- 第二节 java流程控制(判断结构+选择结构)
Java的判断结构: 1.if(条件表达式){ 执行语句 }: 2.if(条件表达式){ 执行语句 }else{ 执行语句 } 3. if(条件表达式){ 执行语句 }else if(条件表达式){ ...
- python-函数参数不固定
默认参数的应用场景: 1.默认安装 2.数据库连接,默认端口号 参数不固定的情况: 参数组: args:将接收的位置参数转换成元组 def test(*args): print(args) ...