当前位置:网站首页>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};
}
}
边栏推荐
猜你喜欢

How vite is compatible with lower version browsers

MySQL transaction and isolation level

all3dp. All Arduino projects in com website (2022.7.1)

Shenji Bailian 3.53-kruskal

"Simple" infinite magic cube

Memcached installation

Redis Key-Value数据库【初级】

Eco express micro engine system has supported one click deployment to cloud hosting

Fundamentals of software testing

CNN可视化技术 -- CAM & Grad-CAM详解及pytorch简洁实现
随机推荐
500. 键盘行
15 C language advanced dynamic memory management
深度学习分类网络 -- AlexNet
Web page user step-by-step operation guide plug-in driver js
PHP gets CPU usage, hard disk usage, and memory usage
[whether PHP has soap extensions installed] a common problem for PHP to implement soap proxy: how to handle class' SoapClient 'not found in PHP
Lambda 表达式 和 方法引用
PHP array to XML
Oled12864 LCD screen
Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond
Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
TypeScript的泛型和泛型约束
Spark概述
Go learning notes integration
1036 Boys vs Girls
PHP development and testing WebService (soap) -win
数据回放伴侣Rviz+plotjuggler
How to change the IP address of computer mobile phone simulator
Generics and generic constraints of typescript