leetcode330:Patching Array(贪心)

330. Patching Array

My Submissions
Total Accepted: 5548  Total Submissions: 19333  Difficulty: Medium

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:
nums = [1, 3]n = 6
Return 1.

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:
nums = [1, 5, 10]n = 20
Return 2.
The two patches can be [2, 4].

Example 3:
nums = [1, 2, 2]n = 5

Return 0.

题意

        告诉你几个元素组成的数组nums[ ]和表示范围n,问最少向数组中添加多少元素使得从nums[ ]中若干元素之和能够覆盖[1..n]

思路

假设数组nums的“部分元素和”可以表示范围[1, total)内的所有数字,

那么向nums中添加元素add可以将表示范围扩充至[1, total + add),其中add ≤ total,当且仅当add = total时取到范围上界[1, 2 * total)

若nums数组为空,则构造[1, n]的nums为[1, 2, 4, 8, ..., k],k为小于等于n的2的幂的最大值。


      cur表示当前能表示的范围为[1..cur],扫描nums[ ],贪心原则如下:

     若nums[i]<=2*cur + 1,把nums[i]添加进来,表示范围更新[1..cur+nums[i]];

     若nums[i]>2*cur + 1,添加新的元素cur+1,表示范围更新[1..2*cur+1].

     cur0开始

注意

      cur可能溢出

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. class Solution {  
  2. public:  
  3.     int minPatches(vector<int>& nums, int n) {  
  4.        long long cur = 0;  
  5.        int i = 0,add = 0;  
  6.        while(cur<n){  
  7.        if(i < nums.size()&&nums[i] <= cur + 1) {  
  8.         cur +=nums[i];  
  9.         i ++ ;  
  10.        }  
  11.     else {  
  12.         add++;  
  13.         cur = 2*cur + 1;  
  14.       }  
  15.     }  
  16.     return add;    
  17.     }  
  18. };  
这是到处搜罗的,感觉好难,看不懂。但是应该坚持吧,虽然看了解答还是不知道怎么做,不过多看一点算法,应该就会坚定自己不适合这方面了。 大哭
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值