没有输出的输入是不完整的

0%

leetcode-330-PatchingArray

今天周六,好好做个题,明天就可以心安理得的出去玩啦!
然后就碰见了这个解题方法特别让人觉得奇妙的题目,算法这个东西实在是太神奇了。

题目描述

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

解题思路

贪心

巧妙之处

构造一个miss,表示当前不能覆盖的最小值,也就是说[0,miss)内的数都可以表示。最开始miss=1,然后依次看数组中的值(要注意数组已经排好序,所以才可以这么用!)。如果当前元素小于等于miss,就把它加上来,扩大能表示的范围;如果当前元素大于miss,或者数组已经用完,就补入miss本身。一直到miss大于n,就完成要求啦。

举个例子

以sample2也就是nums = [1,5,10], n=20举例来讲

  1. 初始化的时候,数组索引index = 0,最小缺失值miss为1,res为0;
  2. 此时miss<=20成立,没有覆盖到[1,n]的范围,所以进入循环判断,此时nums[0] = 1<=miss 而且index没有超出数组范围,所以miss会加上当前元素的值,也就是说miss = 2,表示此时可以表示范围为[0,2)啦。
  3. 然后miss此时仍然不能表示n,继续往后走,此时nums[1] = 5, 是大于miss的,也就是说此时我们最多表示到1,但是你突然来了个5,那我[2,4]的数据怎么办,就只能自己往里添加啦,所以此时为了尽可能表示的数字变得更大,所以此时加上miss本身这个数,从而就可以表示 [0,miss+miss)的数字了,所以此时添加一个2,res在原来的基础上+1变为1,miss也就变成了4;
  4. [0,4)依然不能表示[0,n],所以还是要继续判断,此时miss仍然小于nums[1],所以我仍然不能用nums中的数字来表示4这个数,所以继续向数组中添加4,然后此时可表示范围变成了[0,8),res+1变成2,添加了4这个数字。
  5. [0,8)仍然表示不了[0,n],所以仍然没有结束,此时的miss大于nums[1],也就是miss>5成立,所以此时nums到索引1然后再加上添加的元素可以表示数的范围就变成了[0,13).
  6. [0,13)仍然表示不了[0,20],所以继续,此时miss大于nums[2],所以如果nums中从0到索引2的所有数字加上之前添加的数字可以表示的数字范围就变成了[0,23).
  7. [0,23)是可以表示[0,20]的,所以结束,最终结果需要添加两个数,一个2,一个4即可。
    太神奇了。

解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int minPatches(vector<int>& nums, int n) {
int res = 0;
long long miss = 1; // miss可能超过int范围,long在部分平台仍是32位
size_t i = 0;
while(miss <= n){
if(i<nums.size() && nums[i] <= miss){
miss += nums[i];
i++;
}else{
miss += miss;
res++;
}
}
return res;
}
};