当前位置:网站首页>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};
}
}
边栏推荐
- Thunder on the ground! Another domestic 5g chip comes out: surpass Huawei and lead the world in performance?
- 《CGNF: CONDITIONAL GRAPH NEURAL FIELDS》阅读笔记
- 文件包含漏洞(一)
- Visual studio import
- Oled12864 LCD screen
- Vscode paste image plugin saves image path settings
- PHP inner class name is the same as the inner class method name
- Fabric. JS iText set italics manually
- PHP read file (read JSON file, convert to array)
- Addchild() and addattribute() functions in PHP
猜你喜欢

Practice C language advanced address book design

ThreadLocal memory leak

PHP development and testing WebService (soap) -win

Reading notes of cgnf: conditional graph neural fields

Win10 copy files, save files... All need administrator permission, solution
![[personal test] copy and paste code between VirtualBox virtual machine and local](/img/ce/eaf0bd9eff6551d450964da72e0b63.jpg)
[personal test] copy and paste code between VirtualBox virtual machine and local

mysql事务和隔离级别

Fabric. JS iText set italics manually

我所理解的DRM显示框架

3D printer G code command: complete list and tutorial
随机推荐
文件包含漏洞(二)
Zzuli: maximum Convention and minimum common multiple
Youth training camp -- database operation project
文件包含漏洞(一)
H5 jump applet
测试 - 用例篇
File contains vulnerability (I)
[technical notes-08]
Huawei Hongmeng OS, is it OK?
[personal test] copy and paste code between VirtualBox virtual machine and local
vite如何兼容低版本浏览器
生成二维码
1035 Password
Software testing - concept
idea开发工具常用的插件合集汇总
“简单”的无限魔方
金融门户相关信息
PHP read file (read JSON file, convert to array)
Reflection of the soul of the frame (important knowledge)
Yyds dry inventory what is test driven development