File tree 2 files changed +32
-0
lines changed
2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 266
266
327|[ Count of Range Sum] ( ./0327-count-of-range-sum.js ) |Hard|
267
267
328|[ Odd Even Linked List] ( ./0328-odd-even-linked-list.js ) |Medium|
268
268
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|
269
270
331|[ Verify Preorder Serialization of a Binary Tree] ( ./0331-verify-preorder-serialization-of-a-binary-tree.js ) |Medium|
270
271
334|[ Increasing Triplet Subsequence] ( ./0334-increasing-triplet-subsequence.js ) |Medium|
271
272
337|[ House Robber III] ( ./0337-house-robber-iii.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments