当前位置:网站首页>492. Construction rectangle
492. Construction rectangle
2022-07-02 06:00:00 【occasionally.】
1. Title Description
As a web developer , It's important to know how to plan the size of a page . therefore , Now give a specific rectangular page area , Your task is to design a length of L And the width is W A rectangular page that meets the following requirements . requirement :
① The rectangular page you design must be equal to the given target area .
② Width W Should not be greater than length L , In other words , requirement L >= W .
③ length L Width and width W The gap should be as small as possible .
Return to one Array [L, W], among L and W Is the length and width of the web pages you design in order .
Input : 4
Output : [2, 2]
explain : The target area is 4, All possible construction schemes are [1,4], [2,2], [4,1].
But on request 2,[1,4] Unqualified ; According to the requirements 3,[2,2] Than [4,1] Better meet the requirements . So the output length L by 2, Width W by 2.
2. Their thinking
The key is length L Width and width W The gap should be as small as possible , Obviously L=W Minimum time difference , So from sqrt(area) Start looking left , The first thing that can be divided by area is what you want 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};
}
}
边栏推荐
- Vscode paste image plugin saves image path settings
- Yyds dry inventory what is test driven development
- Conglin environmental protection rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, with an annual profit of more than 200million yuan
- php读文件(读取文件内含有某字符串的指定行)
- uni-app开发中遇到的问题(持续更新)
- Redis Key-Value数据库 【秒杀】
- php数组转化为xml
- 【論文翻譯】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
- 运动健身的一些心得经验
- Ti millimeter wave radar learning (I)
猜你喜欢
随机推荐
DRM display framework as I understand it
File contains vulnerability (I)
Lambda 表达式 和 方法引用
Shenji Bailian 3.52-prim
JWT tool class
php父类(parent)
[C language] simple implementation of mine sweeping game
[C language] screening method for prime numbers
Stc8h8k series assembly and C51 actual combat - keys allow key counting (using falling edge interrupt control)
uni-app开发中遇到的问题(持续更新)
Oled12864 LCD screen
使用sha256文件验证下载的文件
Memcached installation
格式校验js
Some descriptions of Mipi protocol of LCD
Reading notes of cgnf: conditional graph neural fields
495.提莫攻击
使用HBuilderX的一些常用功能
[paper translation] gcnet: non local networks meet squeeze exception networks and beyond
深度学习分类网络 -- AlexNet