You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.
5
+
6
+
// For example, given the array [2,3,1,2,4,3] and s = 7,
7
+
// the subarray [4,3] has the minimal length under the problem constraint.
8
+
9
+
// click to show more practice.
10
+
11
+
// Credits:
12
+
// Special thanks to @Freezen for adding this problem and creating all test cases.
13
+
14
+
// Hide Company Tags Facebook
15
+
// Hide Tags Array Two Pointers Binary Search
16
+
// Hide Similar Problems (H) Minimum Window Substring (M) Maximum Size Subarray Sum Equals k
17
+
2
18
3
19
/**
4
20
* @param {number} s
5
21
* @param {number[]} nums
6
22
* @return {number}
7
23
*/
24
+
25
+
// O(n) solution
8
26
varminSubArrayLen=function(s,nums){
9
27
varsum=0;
10
28
varleft=0;
11
-
varright=-1;
12
-
varlen=nums.length;
29
+
varright=0;
13
30
varminLen=Infinity;
14
31
15
-
while(right<len){
16
-
while(right<len&&sum<s){
17
-
sum+=nums[++right];
18
-
}
19
-
if(sum>=s){
20
-
minLen=Math.min(right-left+1,minLen);
21
-
sum-=nums[left];
22
-
left++;
32
+
while(right<nums.length){
33
+
while(right<nums.length&&sum<s){
34
+
sum+=nums[right++];
23
35
}
24
36
37
+
while(sum>=s){
38
+
minLen=Math.min(minLen,right-left);
39
+
sum-=nums[left++];
40
+
}
25
41
}
26
42
27
-
28
-
29
-
returnminLen>len ? 0 : minLen;
30
-
};
43
+
returnminLen>nums.length ? 0 : minLen;
44
+
};
45
+
46
+
// The O(NlogN) solution is to sum up the array
47
+
// [1,2,3,4,5] becomes [1,3,6,10,15]
48
+
// then iterate through array from index 0 to nums.length - 1
49
+
// for each value in the summed array
50
+
// binary search values after that index so the difference becomes greater than s
51
+
// example
52
+
// s = 8
53
+
// at index 0 with value 1 look between [3,6,10,15] using binary search.
54
+
// we can find that at value 10 the difference is 10 - 1 = 9 the minLen is index 3 - 1 + 1 = 3
55
+
// then we check index 1 with value 3 and binary search [6,10,15] we can find that at value 15 we have difference 15 - 3 = 12
// Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or * between the digits so they evaluate to the target value.
2
+
3
+
// Examples:
4
+
// "123", 6 -> ["1+2+3", "1*2*3"]
5
+
// "232", 8 -> ["2*3+2", "2+3*2"]
6
+
// "105", 5 -> ["1*0+5","10-5"]
7
+
// "00", 0 -> ["0+0", "0-0", "0*0"]
8
+
// "3456237490", 9191 -> []
9
+
// Credits:
10
+
// Special thanks to @davidtan1890 for adding this problem and creating all test cases.
11
+
12
+
// Hide Company Tags Google Facebook
13
+
// Hide Tags Divide and Conquer
14
+
// Hide Similar Problems (M) Evaluate Reverse Polish Notation (H) Basic Calculator (M) Basic Calculator II (M) Different Ways to Add Parentheses
0 commit comments