Leetcode 330. Patching Array

Source: Internet
Author: User

Transmission Door

Patching ArrayMy SubmissionsQuestionEditorial SolutionTotal accepted:8597 Total submissions:29516 difficulty:medium

Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that a NY number in range [1, n] inclusive can is 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 [1], [3], [1,3] is, which form possible sums of: 1, 3, 4 .
Now if we add/patch 2 to nums, the combinations is: [1], [2], [3], [1,3], [2,3], [1,2,3] .
Possible sums 1, 2, 3, 4, 5, 6 is, which now covers the range [1, 6] .
So we are only need 1 patch.

Example 2:
nums = [1, 5, 10] , n =20
Return 2 .
The both patches can be [2, 4] .

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

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Hide TagsGreedyTurn the puzzle: http://www.bubuko.com/infodetail-1345277.html

This problem gives us an ordered positive array nums, and gives us a positive integer n, ask us at least to add a few numbers to nums, so that it can make up all the numbers between [1,n], note that the elements in the array can not be reused, otherwise only 1, you can form all the numbers. This problem I will not again, on the Internet to see the solution of Stephen Great God, worship Ah, here all according to his solution to it. We define a variable miss to represent the smallest non-represented value between [0,n], then initialize to 1, why not 0, because n=0 is meaningless and returns 0 directly. So at this point we can represent a range of [0, Miss], indicating that at this point we can represent 0 to miss-1 number, if Num <= miss at this time, then we can extend the range we can represent the number to [0, Miss+num], if Num>miss, So at this point we need to add a number, in order to maximize the representation of the range, we add miss it itself, and so on until the complete array of arrays, we can get the results. Let's take an example to illustrate the following:

Given nums = [1, 2, 4, one, one], n = 50, we need to let all the numbers between [0, 50] be represented by the sum of the numbers in the nums.

First Use 1, 2, 4 may represent all numbers from 0 to 7, the range is [0, 8), but we can not represent 8, because the next number 11 is too big, so we want to add a 8 in the array, this time can be expressed in the range is [0, 16), then we need to insert 16, the answer is no, Because our array has 1 and 4, can be composed of 5, and the next number 11, plus can be composed of 16, so with the array of 11, we can now represent the scope to expand to [0, 27), but we cannot represent 27, because 30 is too big, so at this point we add a 27 to the array, So now the range that can be expressed is [0, 54], should be satisfied, we added two numbers 8 and 27, so return 2.

1 classSolution {2  Public:3     intMinpatches (vector<int>& Nums,intN) {4         intRET =0;5         intLen =nums.size ();6         inti =0;7         Long LongMiss =1;8          while(Miss <=N) {9             //printf ("ret =%d miss =%d\n", Ret,miss);Ten             if(I < len && Nums[i] <=Miss) { OneMiss + =Nums[i]; Ai++; -             } -             Else{ theRET + +; -Miss *=2; -             } -         } +         //printf ("ret =%d miss =%d\n", Ret,miss); -         returnret; +     } A};

Leetcode 330. Patching Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.