Skip to content

Commit 85b2553

Browse files
committed
Add solution #209
1 parent f309a81 commit 85b2553

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
205|[Isomorphic Strings](./0205-isomorphic-strings.js)|Easy|
187187
206|[Reverse Linked List](./0206-reverse-linked-list.js)|Easy|
188188
207|[Course Schedule](./0207-course-schedule.js)|Medium|
189+
209|[Minimum Size Subarray Sum](./0209-minimum-size-subarray-sum.js)|Medium|
189190
211|[Design Add and Search Words Data Structure](./0211-design-add-and-search-words-data-structure.js)|Medium|
190191
213|[House Robber II](./0213-house-robber-ii.js)|Medium|
191192
214|[Shortest Palindrome](./0214-shortest-palindrome.js)|Hard|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
};

0 commit comments

Comments
 (0)