https://leetcode.com/problems/patching-array/
https://discuss.leetcode.com/topic/35709/share-my-thinking-process
https://leetcode.com/discuss/82827/o-log-n-time-o-1-space-java-with-explanation-trying-my-best
https://leetcode.com/discuss/82832/java-here-is-my-greedy-version-with-brief-explanations-1ms
http://bookshadow.com/weblog/2016/01/27/leetcode-patching-array/
https://www.hrwhisper.me/leetcode-patching-array/
Google 2016 面试题 | 数组补丁
给出一个从小到大排好序的整数数组nums和一个整数n,在数组中添加若干个补丁(元素)使得[1,n]的区间内的所有数都可以表示成nums中若干个数的和。返回最少需要添加的补丁个数。
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 =
Return
nums =
[1, 3]
, n = 6
Return
1
.
Combinations of nums are
Now if we add/patch
Possible sums are
So we only need
[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 =
Return
The two patches can be
nums =
[1, 5, 10]
, n = 20
Return
2
.The two patches can be
[2, 4]
.
Example 3:
nums =
Return
nums =
[1, 2, 2]
, n = 5
Return
0
.
think about
nums = [1,1,1,1,...]
. Then you'll go through all those before you ever get to doubling. It's O(k+log(n)), where k is the size of the array.int minPatches(vector<int>& nums, int n) {
long miss = 1, added = 0, i = 0;
while (miss <= n) {
if (i < nums.size() && nums[i] <= miss) {
miss += nums[i++];
} else {
miss += miss;
added++;
}
}
return added;
}
Let
miss
be the smallest sum in [1,n]
that we might be missing. Meaning we already know we can build all sums in [1,miss)
. Then if we have a number num <= miss
in the given array, we can add it to those smaller sums to build all sums in [1,miss+num)
. If we don't, then we must add such a number to the array, and it's best to add miss
itself, to maximize the reach.
Another implementation, though I prefer the above one.
int minPatches(vector<int>& nums, int n) {
int count = 0, i = 0;
for (long miss=1; miss <= n; count++)
miss += (i < nums.size() && nums[i] <= miss) ? nums[i++] : miss;
return count - i;
}
https://discuss.leetcode.com/topic/35709/share-my-thinking-process
https://leetcode.com/discuss/82827/o-log-n-time-o-1-space-java-with-explanation-trying-my-best
https://leetcode.com/discuss/82832/java-here-is-my-greedy-version-with-brief-explanations-1ms
Greedy idea: add the maximum possible element whenever there is a gap
Iterating the nums[], and keeps adding them up, and we are getting a running sum. At any position, if nums[i] > sum+1, them we are sure we have to patch a sum+1 because all nums before index i can't make sum+1 even add all of them up, and all nums after index i are all simply too large.
public class Solution {
public int minPatches(int[] nums, int n) {
long sum = 0;
int count = 0;
for (int x : nums) {
if (sum >= n) break;
while (sum+1 < x && sum < n) {
++count;
sum += sum+1;
}
sum += x;
}
while (sum < n) {
sum += sum+1;
++count;
}
return count;
}
}
假设数组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的幂的最大值。
若nums数组非空,则扩展时利用nums中的已有元素,详见代码。
public int minPatches(int[] nums, int n) {
int cnt = 0,i=0;
for(long known_sum = 1;known_sum <= n;){
if(i < nums.length && known_sum >= nums[i]){
known_sum += nums[i++];
}else{
known_sum <<= 1;
cnt++;
}
}
return cnt;
}
http://www.zrzahid.com/patching-array/
Basically, as long as cumulative sum so far is greater than the number we need to create , we are good. But id cumsum falls below the current number (between 1 to n) we need to create , then we have to add the current cumsum to itself (why?). This is because we assumed at any particular time cumsum is equal or greater than the current element we need to create. That is we doubling the cumsum when we add a missing element.
public static int minPatches(int[] nums, int n) { int sum = 1; int i = 0; int count = 0; while(sum <= n){ if(i < nums.length && nums[i] <= sum){ sum+=nums[i++]; } else{ sum<<=1; count++; } } return count; }
Google 2016 面试题 | 数组补丁
给出一个从小到大排好序的整数数组nums和一个整数n,在数组中添加若干个补丁(元素)使得[1,n]的区间内的所有数都可以表示成nums中若干个数的和。返回最少需要添加的补丁个数。
Example 1:
nums =[1, 3]
, n =6
返回1,表示至少需要添加1个数{2},才可以表示1到6之间所有数。
Example 2:
nums =[1, 5, 10]
, n =20
返回2,表示至少需要添加两个数{2,4},才可以表示1到20之间所有数。
读者不难想到暴力搜索的做法:先穷举每一个不在数组里的数p,再穷举判断p是否可以表示为数组中若干个数的和;如果不能,则把p加入数组中,把答案加一。
然而,这种做法时间复杂度高且实际操作难度大(需要考虑穷举的顺序)。我们不妨先思考一个简单的问题,如果nums数组为空,那么最少需要多少个数字才能表示1到n之间所有数?相信大家都可以想到一个贪心算法,即按照1、2、4、8...都顺序添加,每次加入都数都比之前所有数的总和大1,直到总和大于n。本题的难点是预先给出了一些数,但这不影响我们的贪心策略:假设nums当前至多可以表示1到m之间的所有数,加入m+1;直到m大于等于n。
此题的最优算法是贪心。在实际面试过程中,笔者认为只需要想到贪心算法,并给出算法框架,就可以达到hire的程度。能在短时间内完成程序,可以达到strong hire。
public int minPatches(int[] nums, int n) { long sum = 0; int ans = 0; int index = 0; if (nums.length > 0 && nums[0] == 1) { sum = 1; index = 1; } while (sum < n) { while (index < nums.length && nums[index] <= sum) { sum += nums[index]; index++; } if (sum < n) { if (index < nums.length && nums[index] == sum + 1) index++; else { ans++; } sum = 2 * sum + 1; } } return ans; }