carloscn / structstudy

Leetcode daily trainning by using C/C++/RUST programming.
4 stars 1 forks source link

leetcode492:构造矩形(construct-the-rectangle) #107

Open carloscn opened 1 year ago

carloscn commented 1 year ago

问题描述

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

你设计的矩形页面必须等于给定的目标面积。
宽度 W 不应大于长度 L ,换言之,要求 L >= W 。
长度 L 和宽度 W 之间的差距应当尽可能小。

返回一个 数组 [L, W],其中 L 和 W 是你按照顺序设计的网页的长度和宽度。

示例1: 输入: 4 输出: [2, 2] 解释: 目标面积是 4, 所有可能的构造方案有 [1,4], [2,2], [4,1]。 但是根据要求2,[1,4] 不符合要求; 根据要求3,[2,2] 比 [4,1] 更能符合要求. 所以输出长度 L 为 2, 宽度 W 为 2。

示例 2: 输入: area = 37 输出: [37,1]

示例 3: 输入: area = 122122 输出: [427,286]

提示:

1 <= area <= 107

来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/construct-the-rectangle

carloscn commented 1 year ago

问题分析

从sqrt(area)开始找,直到1为止,找到的第一个area的因数即为宽。最好往左边找,往右边的话数字太多,会很慢。

static int32_t con_rec(int64_t area, size_t *l_o, size_t *w_o) 
{
    int32_t ret = 0;
    int64_t dist = 0;
    int64_t i = 0;
    int64_t mid = sqrt(area);

    UTILS_CHECK_PTR(l_o);
    UTILS_CHECK_PTR(w_o);

    for (i = mid; i > 0; i --) {
        if (0 == area % i) {
            dist = i;
            *l_o = area / dist;
            *w_o = dist;
            goto finish;
        }
    }

finish:
    return ret;
}
carloscn commented 1 year ago

code:

https://github.com/carloscn/structstudy/blob/master/c_programming/array/36_construct-the-rectangle_492.c

result:

图片