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
    2. The web page's width and length you designed must be positive integers.

这道题让我们根据面积来求出矩形的长和宽,要求长和宽的差距尽量的小,那么就是说越接近正方形越好。那么我们肯定是先来判断一下是不是正方行,对面积开方,如果得到的不是整数,说明不是正方形。那么我们取最近的一个整数,看此时能不能整除,如果不行,就自减1,再看能否整除。最坏的情况就是面积是质数,最后减到了1,那么返回结果即可,参见代码如下:

解法一:

class Solution {
public:
vector<int> constructRectangle(int area) {
int r = sqrt(area);
while (area % r != ) --r;
return {area / r, r};
}
};

如果我们不想用开方运算sqrt的话,那就从1开始,看能不能整除,循环的终止条件是看平方值是否小于等于面积,参见代码如下:

解法二:

class Solution {
public:
vector<int> constructRectangle(int area) {
int r = ;
for (int i = ; i * i <= area; ++i) {
if (area % i == ) r = i;
}
return {area / r, r};
}
};

参考资料:

https://discuss.leetcode.com/topic/76469/3ms-concise-c

https://discuss.leetcode.com/topic/76314/3-line-clean-and-easy-understand-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Construct the Rectangle 构建矩形的更多相关文章

  1. 492 Construct the Rectangle 构建矩形

    详见:https://leetcode.com/problems/construct-the-rectangle/description/ C++: class Solution { public: ...

  2. LeetCode——Construct the Rectangle

    LeetCode--Construct the Rectangle Question For a web developer, it is very important to know how to ...

  3. [LeetCode] 85. Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...

  4. LeetCode Construct the Rectangle

    原题链接在这里:https://leetcode.com/problems/construct-the-rectangle/ 题目: For a web developer, it is very i ...

  5. Leetcode492.Construct the Rectangle构造矩形

    作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 1. 你设计的矩形页面必 ...

  6. LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)

    题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的 ...

  7. 【leetcode】492. Construct the Rectangle

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

  8. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  9. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

随机推荐

  1. 深入理解Android中View

    文章目录   [隐藏] 一.View是什么? 二.View创建的一个概述: 三.View的标志(Flag)系统 四.MeasureSpec 五.几个重要方法简介 5.1 onFinishInflate ...

  2. Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版

    一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...

  3. alpha-咸鱼冲刺day2-紫仪

    总汇链接 一,合照 emmmmm.自然是没有的. 二,项目燃尽图 三,项目进展 今天并没有什么进展,弄了好久好像也只研究出怎么把JS的功能块插入进去.html的信息提交这些还不知道要怎么弄. 四,问题 ...

  4. android批量打包

    http://blog.csdn.net/johnny901114/article/details/48714849

  5. css中的em 简单教程 -- 转

    先附上原作的地址: https://www.w3cplus.com/css/px-to-em 习惯性的复制一遍~~~~ -------------------------------我是分界线---- ...

  6. sublime的使用技巧

    ctr+shift+d是复制当前行当下一行2.使用Sublime text 3 编写代码是一种享受,使用Sublime text 3 格式化HTML代码,需要安装插件,具体安装步骤如下:1.打开菜单- ...

  7. Mego(03) - ORM框架的新选择

    前言 从之前的两遍文章可以看出ORM的现状. Mego(01) - NET中主流ORM框架性能对比 Mego(02) - NET主流ORM框架分析 首先我们先谈下一个我们希望的ORM框架是什么样子的: ...

  8. Jenkins中展示HTML测试报告

    背景:测试报告是用reportNG生成的,属于java自动化测试项目. 1)         安装插件 首先要安装HTML Publisher plugin,这个在插件管理里面搜索并安装即可,如下我已 ...

  9. Python内置函数(44)——len

    英文文档: len(s) Return the length (the number of items) of an object. The argument may be a sequence (s ...

  10. hibernate_exercise-many- to-one(1)

    多对一关系 1.创建t_user表.t_group表 2.在eclipse中创建对应的实体类 package com.eneity; public class User { private int i ...