前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode330. Patching Array

leetcode330. Patching Array

作者头像
眯眯眼的猫头鹰
发布2019-03-13 16:47:57
5530
发布2019-03-13 16:47:57
举报
文章被收录于专栏:眯眯眼猫头鹰的小树杈

题目要求

代码语言:javascript
复制
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]。

代码语言:javascript
复制
    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型避免数组值的溢出。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目要求
  • 思路和代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档