Skip to content

Commit 312a215

Browse files
committed
Add solution #330
1 parent 7dcb520 commit 312a215

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@
266266
327|[Count of Range Sum](./0327-count-of-range-sum.js)|Hard|
267267
328|[Odd Even Linked List](./0328-odd-even-linked-list.js)|Medium|
268268
329|[Longest Increasing Path in a Matrix](./0329-longest-increasing-path-in-a-matrix.js)|Hard|
269+
330|[Patching Array](./0330-patching-array.js)|Hard|
269270
331|[Verify Preorder Serialization of a Binary Tree](./0331-verify-preorder-serialization-of-a-binary-tree.js)|Medium|
270271
334|[Increasing Triplet Subsequence](./0334-increasing-triplet-subsequence.js)|Medium|
271272
337|[House Robber III](./0337-house-robber-iii.js)|Medium|

solutions/0330-patching-array.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 330. Patching Array
3+
* https://leetcode.com/problems/patching-array/
4+
* Difficulty: Hard
5+
*
6+
* Given a sorted integer array nums and an integer n, add/patch elements to the array such
7+
* that any number in the range [1, n] inclusive can be formed by the sum of some elements
8+
* in the array.
9+
*
10+
* Return the minimum number of patches required.
11+
*/
12+
13+
/**
14+
* @param {number[]} nums
15+
* @param {number} n
16+
* @return {number}
17+
*/
18+
var minPatches = function(nums, n) {
19+
let result = 0;
20+
21+
for (let i = 0, k = 1; k <= n;) {
22+
if (i < nums.length && nums[i] <= k) {
23+
k += nums[i++];
24+
} else {
25+
k += k;
26+
result++;
27+
}
28+
}
29+
30+
return result;
31+
};

0 commit comments

Comments
 (0)