[LeetCode][JavaScript]Patching Array

Patching Array

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.

https://leetcode.com/problems/patching-array/



组合nums数组中的数字以填满[1, n]的范围,如果有空缺的组合数字,需要补patch到nums数组中,求最少需要几个patch。

https://leetcode.com/discuss/82822/solution-explanation

贪心,从小到大填满[1, n],能用原来的nums中的数的时候就用掉,不够就补patch。

举例来说,nums = [1, 5, 10], n = 40。

首先就缺少了2,patch补2,此时范围是[1, 3],即1 ∪ 2 ∪ 1 + 2。

这边还用不到5,patch补4,此时范围是[1, 7],即[1, 3] ∪ 4 ∪ [1 + 4, 3 + 4] 。

5在的[1, 7]的范围里,可以用上5,此时范围是[1, 12],即[1, 7] ∪ [1 + 5, 7 + 5] 。

10在的[1, 12]的范围里,用上10,此时范围是[1, 22],即[1, 12] ∪ [1 + 10, 12 + 10] 。

patch补23,此时范围是[1, 45],即[1, 22] ∪ 23 ∪ [1 + 23, 22 + 23] 。

最后需要3个patch[2, 4, 23]。

代码按照这个思路实现,先看nums里的数能不能用上,如果不行就加一个patch。

 1 /**
 2  * @param {number[]} nums
 3  * @param {number} n
 4  * @return {number}
 5  */
 6 var minPatches = function(nums, n) {
 7     var countPatch = 0, numsIndex = 0;
 8     for(var i = 1; i <= n;){
 9         if(nums[numsIndex] < i){
10             i += nums[numsIndex];
11             numsIndex++;
12             continue;
13         }
14         if(nums[numsIndex] === i)
15             numsIndex++;
16         else
17             countPatch++;
18         if(i - 1 > 0)
19             i += i - 1;
20         i++;
21     }
22     return countPatch;
23 };
时间: 2024-10-26 00:41:57

[LeetCode][JavaScript]Patching Array的相关文章

leetcode 330. Patching Array

传送门 330. Patching Array My Submissions QuestionEditorial Solution Total 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 any number in ra

【Leetcode】Patching Array

题目链接: 题目: 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 require

[LeetCode]Patching Array

题目:Patching Array 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

[LeetCode] Merge Sorted Array [22]

题目 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B ar

[LeetCode][JavaScript]Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] https://leetcode.com/problems/pascals-triangle/ 杨辉三角. 每行第一个和最后一个是1,其余是pre[i - 1] +

Javascript 中 Array的 sort()和 compare()方法

Javascript 中 Array的 sort()方法其实是把要排序的内容转化为string(调用 toString()), 然后按照字符串的第一位 ascii 码先后顺序进行比较,不是数字. 我们看看官方是怎么说的: arrayobj.sort(sortfunction) 参数 arrayObj 必选项.任意 Array 对象. sortFunction 可选项.是用来确定元素顺序的函数的名称.如果这个参数被省略,那么元素将按照 ASCII 字符顺序进行升序排列. 说明 sort 方法将 A

JavaScript 中Array数组的几个内置函数

本文章内容均参考<JavaScript高级程序设计第三版> 今天在看JavaScript书籍的时候,看到之前没有了解过的JavaScript中Array的几个内置函数对象,为了之后再开发工作中能方便查询,故编写此随笔.直接贴代码, function arrayEffect(){ var numbers = [1,2,3,4,5,6,7,8,9,10]; //------------------------------------ 支持浏览器版本 IE9+,Firfox 2+ ,Safair 3

330. Patching Array

/* * 330. Patching Array * 2016-7-9 by Mingyang * hehe */ public int minPatches(int[] nums, int n) { if (n <= 0) return 0; nums = nums == null ? new int[0] : nums; int current_ind = 0, ret = 0; long boundary_val = 1, sum = 0; while (boundary_val <=

[leetcode]Convert Sorted Array to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的. 解题思路:由于要求二叉查找树是平衡的.所以我们可以选在数组的中间那个数当树根root,然后这个数左边的数组为左子树,右边的数组为右子树,分别递归产生左右子树就可以了. 代码: # Definition for a binary tree node # class