leetcode330. Patching Array

本文探讨了如何在已排序的正整数数组中添加最少的元素,使数组能组合成1到n之间的所有整数。通过核心思路的阐述与代码实现,详细解释了如何高效地解决这一问题。

题目要求

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

Example 1:

Input: nums = [1,3], n = 6
Output: 1 
Explanation:
Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.
Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3].
Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6].
So we only need 1 patch.
Example 2:

Input: nums = [1,5,10], n = 20
Output: 2
Explanation: The two patches can be [2, 4].
Example 3:

Input: nums = [1,2,2], n = 5
Output: 0

假设有一个有序的正整数数组nums和一个整数n,最少添加几个元素到这个数组中,使得从1-n的所有整数都可以由这个数组中的值的或是几个值的和构成。

思路和代码

这里的核心思路在于如何找到一个规律,使得我们可以在前一个规律的基础上添加一个数达到下一个范围。假设当前的数组可以组成从[1...k]的所有整数,那么我们下一步如果往数组中添加x,则数组就可以组成从[1...k+x]的所有整数。因此,为了只打最少的补丁,我们会希望每一次往数组中添加的元素所能够到达的范围越远越好,因此我们会patch一个k+1上去从而使得数组最远扩展到[1...2k+1]。如果当前数组中存在一个数组m位于[k+1, 2k+1]这个范围中,则我们的数组可以再次扩展到[1...2k+m+1]。

    public int minPatches(int[] nums, int n) {
        int count = 0;
        long max = 0;
        int index = 0;
        while(max < n) {
            if(index<nums.length && max + 1 >= nums[index]) {
                max += nums[index++];
            } else {
                count++;
                max += max + 1;
            }
            
        }
        return count;
    }

这里需要注意的细节是数组的溢出。这里用long型避免数组值的溢出。

clipboard.png
想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值