当前位置:网站首页>492.构造矩形
492.构造矩形
2022-07-02 05:50:00 【有时候。】
1. 题目描述
作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的。 所以,现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面。要求:
①你设计的矩形页面必须等于给定的目标面积。
② 宽度 W 不应大于长度 L ,换言之,要求 L >= W 。
③ 长度 L 和宽度 W 之间的差距应当尽可能小。
返回一个 数组 [L, W],其中 L 和 W 是你按照顺序设计的网页的长度和宽度。
输入: 4
输出: [2, 2]
解释: 目标面积是 4, 所有可能的构造方案有 [1,4], [2,2], [4,1]。
但是根据要求2,[1,4] 不符合要求; 根据要求3,[2,2] 比 [4,1] 更能符合要求. 所以输出长度 L 为 2, 宽度 W 为 2。
2. 解题思路
关键点在于长度 L 和宽度 W 之间的差距应当尽可能小,显然当L=W时差距最小,所以从sqrt(area)开始往左查找,第一个能被面积整除的就是所求的W。
- python3
class Solution:
def constructRectangle(self, area: int) -> List[int]:
wide = int(sqrt(area))
while area % wide != 0:
wide -= 1
return [int(area/wide), wide]
- c++
class Solution {
public:
vector<int> constructRectangle(int area) {
int wide = (int)sqrt(area);
while (area % wide != 0){
wide--;
}
return vector<int>{
area/wide, wide};
}
}
边栏推荐
猜你喜欢
ThreadLocal memory leak
PHP development and testing WebService (soap) -win
软件测试基础篇
“簡單”的無限魔方
Visual studio import
Go language web development is very simple: use templates to separate views from logic
Record sentry's path of stepping on the pit
centos8安裝mysql8.0.22教程
File contains vulnerabilities (II)
Practice C language advanced address book design
随机推荐
File contains vulnerabilities (II)
Get the details of the next largest number
Here comes a new chapter in the series of data conversion when exporting with easyexcel!
Uva548 tree
Test case
1037 Magic Coupon
c语言中的几个关键字
“簡單”的無限魔方
mysql的约束总结
线程池概述
centos8安裝mysql8.0.22教程
With an amount of $50billion, amd completed the acquisition of Xilinx
Oled12864 LCD screen
“简单”的无限魔方
软件测试基础篇
《CGNF: CONDITIONAL GRAPH NEURAL FIELDS》阅读笔记
Zzuli:1065 count the number of numeric characters
数理统计与机器学习
The Hong Kong Stock Exchange learned from US stocks and pushed spac: the follow-up of many PE companies could not hide the embarrassment of the world's worst stock market
GRBL 软件:简单解释的基础知识