1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實戰(zhàn)330:按要求補齊數(shù)組

        共 2566字,需瀏覽 6分鐘

         ·

        2021-07-27 01:04

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

        今天和大家聊的問題叫做 按要求補齊數(shù)組,我們先來看題面:
        https://leetcode-cn.com/problems/patching-array/

        Given a sorted integer array nums and an integer n, add/patch elements to the array such that any number in the range [1, n] inclusive can be formed by the sum of some elements in the array.

        Return the minimum number of patches required.

        給定一個已排序的正整數(shù)數(shù)組 nums,和一個正整數(shù) n 。從 [1, n] 區(qū)間內(nèi)選取任意個數(shù)字補充到 nums 中,使得 [1, n] 區(qū)間內(nèi)的任何數(shù)字都可以用 nums 中某幾個數(shù)字的和來表示。請輸出滿足上述要求的最少需要補充的數(shù)字個數(shù)。

        示例


        示例 1:

        輸入: nums = [1,3], n = 6
        輸出: 1
        解釋:
        根據(jù) nums 里現(xiàn)有的組合 [1], [3], [1,3],可以得出 1, 3, 4。
        現(xiàn)在如果我們將 2 添加到 nums 中, 組合變?yōu)? [1], [2], [3], [1,3], [2,3], [1,2,3]。
        其和可以表示數(shù)字 1, 2, 3, 4, 5, 6,能夠覆蓋 [1, 6] 區(qū)間里所有的數(shù)。
        所以我們最少需要添加一個數(shù)字。

        示例 2:

        輸入: nums = [1,5,10], n = 20
        輸出: 2
        解釋: 我們需要添加 [2, 4]。

        示例 3:

        輸入: nums = [1,2,2], n = 5
        輸出: 0


        解題


        維護一個邊界值range,使得數(shù)組內(nèi)數(shù)字可達范圍為[1,range),當(dāng)i < nums.length且range >= nums[i] 時說明此時數(shù)組中的值仍然在可達范圍中,那么只需要range = range + nums[i]讓目前的可達范圍變成[1,range + nums[i])即可,否則要讓range += range來使得此時的可達范圍變成[1, 2range) -> [1,range) ∪ [1 + range, range + range),直到range > n后可退出循環(huán) 。

        class Solution {
            public int minPatches(int[] nums, int n) {
                int i = 0, count = 0;
                long range = 1;

                while(range <= n) {
                    if(i < nums.length && nums[i] <= range) range += nums[i++];
                    else {
                        range += range;
                        count++;
                    }
                }
                
                return count;
            }
        }


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

        上期推文:

        LeetCode1-320題匯總,希望對你有點幫助!

        LeetCode刷題實戰(zhàn)321:拼接最大數(shù)

        LeetCode刷題實戰(zhàn)322:零錢兌換

        LeetCode刷題實戰(zhàn)323:無向圖中連通分量的數(shù)目

        LeetCode刷題實戰(zhàn)324:擺動排序 II

        LeetCode刷題實戰(zhàn)325:和等于 k 的最長子數(shù)組長度

        LeetCode刷題實戰(zhàn)326:3的冪
        LeetCode刷題實戰(zhàn)327:區(qū)間和的個數(shù)
        LeetCode刷題實戰(zhàn)328:奇偶鏈表
        LeetCode刷題實戰(zhàn)329:矩陣中的最長遞增路徑

        瀏覽 49
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            五月色综合 | 成人无码视频免费观看 | 猛男 大 粗 猛 爽h男人味69XX | 精品无码一区二区三区狠狠 | 色女孩综合| 啪啪啪啪网 | 自由日本语亚洲人高潮 | 中美一级网站 | 99精品久久久久久中文字幕 | 草莓秋葵菠萝蜜黄瓜榴莲视频 |