<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)492:構(gòu)造矩形

          共 1297字,需瀏覽 3分鐘

           ·

          2022-01-09 20:48

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?構(gòu)造矩形,我們先來看題面:
          https://leetcode-cn.com/problems/construct-the-rectangle/

          A web developer needs 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:

          The area of the rectangular web page you designed must equal to the given target area.
          The width W should not be larger than the length L, which means L >= W.
          The difference between length L and width W should be as small as possible.
          Return an array [L, W] where L and W are the length and width of the web page you designed in sequence.


          作為一位web開發(fā)者, 懂得怎樣去規(guī)劃一個(gè)頁(yè)面的尺寸是很重要的。現(xiàn)給定一個(gè)具體的矩形頁(yè)面面積,你的任務(wù)是設(shè)計(jì)一個(gè)長(zhǎng)度為 L 和寬度為 W 且滿足以下要求的矩形的頁(yè)面。要求:
          1. 你設(shè)計(jì)的矩形頁(yè)面必須等于給定的目標(biāo)面積。
          2. 寬度 W 不應(yīng)大于長(zhǎng)度 L,換言之,要求 L >= W 。
          3. 長(zhǎng)度 L 和寬度 W 之間的差距應(yīng)當(dāng)盡可能小。
          你需要按順序輸出你設(shè)計(jì)的頁(yè)面的長(zhǎng)度 L 和寬度 W。

          示例? ? ? ? ? ? ? ? ? ? ? ? ?


          輸入: 4
          輸出: [2, 2]
          解釋: 目標(biāo)面積是 4, 所有可能的構(gòu)造方案有 [1,4], [2,2], [4,1]
          但是根據(jù)要求2[1,4]?不符合要求; 根據(jù)要求3[2,2]?比 [4,1]?更能符合要求. 所以輸出長(zhǎng)度 L?為 2, 寬度 W?為 2

          說明:

          給定的面積不大于 10,000,000?且為正整數(shù)。
          你設(shè)計(jì)的頁(yè)面的長(zhǎng)度和寬度必須都是正整數(shù)。


          解題

          這一題的解法比較獨(dú)特,我們觀察題目給出的條件 L >= W ,通過這一點(diǎn) 我們可以得出 W * W <= L * W = area

          所以有 w * w <= area

          那么我們可以利用Math提供的平方根函數(shù)對(duì)area 開方得出 w 可能出現(xiàn)的最大值,隨后遞減 w 的值,直到它能夠被 area 取模為0;此時(shí)的 w 就是符合題目條件的最接近 L 的 W;

          class?Solution?{
          ????public?int[] constructRectangle(int?area) {
          ???int?w = (int)Math.sqrt(area);
          ????????while(area % w != 0) w--;

          ????????return?new?int[]{area / w, w};
          ????}
          }


          好了,今天的文章就到這里,如果覺得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

          上期推文:

          LeetCode1-480題匯總,希望對(duì)你有點(diǎn)幫助!

          LeetCode刷題實(shí)戰(zhàn)481:神奇字符串

          LeetCode刷題實(shí)戰(zhàn)482:密鑰格式化

          LeetCode刷題實(shí)戰(zhàn)483:最小好進(jìn)制

          LeetCode刷題實(shí)戰(zhàn)484:尋找排列

          LeetCode刷題實(shí)戰(zhàn)485:最大連續(xù) 1 的個(gè)數(shù)

          LeetCode刷題實(shí)戰(zhàn)486:預(yù)測(cè)贏家

          LeetCode刷題實(shí)戰(zhàn)487:最大連續(xù)1的個(gè)數(shù) II

          LeetCode刷題實(shí)戰(zhàn)488:祖瑪游戲

          LeetCode刷題實(shí)戰(zhàn)489:掃地機(jī)器人

          LeetCode刷題實(shí)戰(zhàn)490:迷宮


          瀏覽 19
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  20120av天堂 | 日韩国产精品久久久久亚州 | 亲子乱伦一区二区 | 大香蕉网伊人 | 大鸡巴操黑丝 |