File tree 2 files changed +30
-0
lines changed
2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 186
186
205|[ Isomorphic Strings] ( ./0205-isomorphic-strings.js ) |Easy|
187
187
206|[ Reverse Linked List] ( ./0206-reverse-linked-list.js ) |Easy|
188
188
207|[ Course Schedule] ( ./0207-course-schedule.js ) |Medium|
189
+ 209|[ Minimum Size Subarray Sum] ( ./0209-minimum-size-subarray-sum.js ) |Medium|
189
190
211|[ Design Add and Search Words Data Structure] ( ./0211-design-add-and-search-words-data-structure.js ) |Medium|
190
191
213|[ House Robber II] ( ./0213-house-robber-ii.js ) |Medium|
191
192
214|[ Shortest Palindrome] ( ./0214-shortest-palindrome.js ) |Hard|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 209. Minimum Size Subarray Sum
3
+ * https://leetcode.com/problems/minimum-size-subarray-sum/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an array of positive integers nums and a positive integer target, return the
7
+ * minimal length of a subarray whose sum is greater than or equal to target. If there
8
+ * is no such subarray, return 0 instead.
9
+ */
10
+
11
+ /**
12
+ * @param {number } target
13
+ * @param {number[] } nums
14
+ * @return {number }
15
+ */
16
+ var minSubArrayLen = function ( target , nums ) {
17
+ let result = Infinity ;
18
+
19
+ for ( let right = 0 , sum = 0 , left = 0 ; right < nums . length ; right ++ ) {
20
+ sum += nums [ right ] ;
21
+ while ( sum >= target ) {
22
+ result = Math . min ( result , right - left + 1 ) ;
23
+ sum -= nums [ left ] ;
24
+ left ++ ;
25
+ }
26
+ }
27
+
28
+ return result === Infinity ? 0 : result ;
29
+ } ;
You can’t perform that action at this time.
0 commit comments