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:

  1. The given area won't exceed 10,000,000 and is a positive integer

思路就是先找sqrt, 然后依次往下减, 一旦可以被整除, 就输出.

Code

class Solution:
def constructRectangle(self, area):
width = int(math.sqrt(area))
while area%width:
width -= 1
return area//width, width

[LeetCode] 492. Construct the Rectangle_Easy tag: Math的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [LeetCode] 598. Range Addition II_Easy tag: Math

    做个基本思路可以用 brute force, 但时间复杂度较高. 因为起始值都为0, 所以肯定是左上角的重合的最小的长方形就是结果, 所以我们求x, y 的最小值, 最后返回x*y. Code    ...

  4. [LeetCode] 367. Valid Perfect Square_Easy tag:Math

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  5. [LeetCode] 172. Factorial Trailing Zeroes_Easy tag: Math

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  6. [LeetCode] 728. Self Dividing Numbers_Easy tag: Math

    A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is ...

  7. [LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  8. 【leetcode】492. Construct the Rectangle

    problem 492. Construct the Rectangle 参考 1. Leetcode_492. Construct the Rectangle; 完

  9. (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

随机推荐

  1. yum install 下载后保存rpm包

    keepcache=0 更改为1下载RPM包 不会自动删除 vi /etc/yum.conf [main] cachedir=/var/cache/yum/$basearch/$releasever ...

  2. 使用Zookeeper命令的简单操作步骤

    (1) 使用ls命令查看当前Zookeeper中所包含的内容:ls / [zk: localhost:2181(CONNECTED) 1] ls / [zookeeper] [zk: localhos ...

  3. SCSS快速入门

    SCSS快速入门 1. 使用变量sass引入了变量.通过变量名来引用它们,使用$符号来标识变量.且在CSS中并无他用,不会导致与现存或未来的css语法冲突. 1-1. 变量声明sass变量的声明和cs ...

  4. A crawler that sent a DELETE request to every resource it encountered

    RESTful Web APIs_2013 The crawler simulates a very curious but not very picky human. Give it a URL t ...

  5. 2012年蓝桥杯省赛A组c++第2题(暴力求解古堡算式)

    /* 古堡算式 福尔摩斯到某古堡探险,看到门上写着一个奇怪的算式: ABCDE * ? = EDCBA 他对华生说:“ABCDE应该代表不同的数字,问号也代表某个数字!” 华生:“我猜也是!” 于是, ...

  6. [filesystem][archlinux][disk encryption][btrfs] btrfs

    fork from here http://www.cnblogs.com/hugetong/p/6914248.html boot分区,MBR加密:https://wiki.archlinux.or ...

  7. hdfs mapreduce hbase

    参考资料:http://www.cnblogs.com/sharpxiajun/p/5585613.html 大数据时代的数据量是超大规模的,传统的关系数据库已经很难存储和管理这些数据了,为了存储海量 ...

  8. 使用uibesizerpath + Cashaplayer画椭圆

    使用uibesizerpath Cashaplayer画椭圆: + (void)drawOvalAnimSourceView:(UIView *)sourceView { //view是曲线的背景vi ...

  9. python找出数组中第二大的数

    #!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:找出数组中第2大的数字 ''' def find_Second_large_ ...

  10. jquery基础学习之样式篇(一)

    一.安装与使用 官网下载,然后引入 <script src="js/jquery-3.3.1.js"></script>,这是生产版本,开发版本替换成min ...