Skip to content

Commit 483c1df

Browse files
committed
Add min subarray sum
1 parent de19143 commit 483c1df

File tree

7 files changed

+144
-9
lines changed

7 files changed

+144
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Sliding window: TC: O(n), SC: O(1)
2+
package minimumSizeSubarraySum;
3+
4+
public class MinimumSizeSubarraySum {
5+
6+
private static int minSizeSubarraySum(int[] nums, int target) {
7+
int left = 0, right = 0, total = 0, result = Integer.MAX_VALUE;
8+
9+
while(right < nums.length) {
10+
total += nums[right];
11+
while(total >= target) {
12+
result = Math.min(right-left+1, result);
13+
total -= nums[left++];
14+
}
15+
right++;
16+
}
17+
18+
return result == Integer.MAX_VALUE ? 0 : result;
19+
20+
}
21+
22+
public static void main(String[] args) {
23+
24+
int target1 = 7;
25+
int[] nums1 = {2,4,1,2,4,3};
26+
int target2 = 5;
27+
int[] nums2 = {1, 5, 5, 5};
28+
int target3 = 15;
29+
int[] nums3 = {2, 2, 2, 2, 2};
30+
31+
System.out.println(minSizeSubarraySum(nums1, target1));
32+
System.out.println(minSizeSubarraySum(nums2, target2));
33+
System.out.println(minSizeSubarraySum(nums3, target3));
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Algorithmic Steps**
2+
This problem is solved with the help of sliding window approach without using any special data structure. The algorithmic approach can be summarized as follows:
3+
4+
1. Initialize left and right pointers to 0, to keep track of the substring window boundaries.
5+
6+
2. Initialize total variable to 0, to store the sum of the subarray values to meet the target criteria.
7+
8+
3. Initialize result variable to max integer value to indicate that there is no subarray to meet target value.
9+
10+
4. Iterate over the input array using right pointer until the end of the string.
11+
12+
5. Calculate the total value by adding array value at respective right pointer.
13+
14+
6. If the total value is greather or equal to target, find the minimum of subarray sum and shrink the current window total and left pointer value. This step need to be repeated until there are no subarray exists to meet the target criteria.
15+
16+
7. Increment the right pointer to find the next subarray.
17+
18+
8. Repeat steps 4–7 until the right pointer reaches the end of the array.
19+
20+
9. Return 0 if the result is still a maximum integer which indicates no valid subarray otherwise return the calculated minimum subarray value.
21+
22+
**Time and Space complexity:**
23+
This algorithm has a time complexity of O(n) because we are traversing the array only twice at max. Since it doens't require any datastructure, the space complexity will be O(1).

src/java1/algorithms/strings/longestSubstringWithoutRepeatingChar.java src/java1/algorithms/strings/longestSubstringWithoutRepeatingChars/LongestSubstringWithoutRepeatingChars.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
// Sliding window: TC: O(n), SC: O(n)
2-
package java1.algorithms.strings;
2+
package java1.algorithms.strings.longestSubstringWithoutRepeatingChars;
33

44
import java.util.HashSet;
55

6-
public class LongestSubstringWithoutRepeatingChar {
6+
public class LongestSubstringWithoutRepeatingChars {
7+
78
private static int longestSubstringLengthWithoutRepeatingChar(String str) {
9+
810
HashSet<Character> hashset = new HashSet<>();
9-
int left = 0, max = 0;
11+
int left = 0, right = 0, max = 0;
1012

11-
for(int right = 0; right < str.length(); right++) {
13+
while(right < str.length()) {
1214
while(hashset.contains(str.charAt(right))){
1315
hashset.remove(str.charAt(left));
1416
left++;
1517
}
1618
hashset.add(str.charAt(right));
19+
right++;
1720
max = Math.max(max, hashset.size());
1821
}
1922
return max;
@@ -22,6 +25,7 @@ private static int longestSubstringLengthWithoutRepeatingChar(String str) {
2225
public static void main(String[] args) {
2326
String string1 = "abcabcbbaa";
2427
System.out.println(longestSubstringLengthWithoutRepeatingChar(string1));
28+
2529
String string2 = "aaaaaaa";
2630
System.out.println(longestSubstringLengthWithoutRepeatingChar(string2));
2731
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Algorithmic Steps**
2+
This problem is solved with the help of sliding window approach using a HashSet. The algorithmic approach can be summarized as follows:
3+
4+
1. Initialize an empty HashSet to store unique characters within the current window.
5+
6+
2. Initialize left and right pointers to 0, to keep track of the substring window boundaries.
7+
8+
3. Initialize maxLength variable to 0, to store the length of the longest substring without repeating characters.
9+
10+
4. Iterate over the input string using right pointer until the end of the string.
11+
12+
5. If a repeating character is found, remove the repeated character from the char set and move the left pointer to the right(i.e, increment left pointer) to shrink the window. This step need to be repeated until there is no repeated character in the set.
13+
14+
6. If the new character doesn't exist in the char set, add the character to the set and move the right pointer(i.e, increment right pointer) to expand the window.
15+
16+
7. Update the maxLength variable if the current window length is greater than the previous maxLength.
17+
18+
8. Repeat steps 4–7 until the right pointer reaches the end of the string.
19+
20+
9. Return the maxLength variable which indicates the longest substring length.
21+
22+
**Time and Space complexity:**
23+
This algorithm has a time complexity of O(n) because we are traversing the string only once. Also, it requires space complexity of O(n) due to HashSet data structure.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Sliding window: TC: O(n), SC: O(1)
2+
3+
function minSizeSubarraySum(nums, target) {
4+
5+
let left = right = total = 0;
6+
let result = Infinity;
7+
8+
while(right < nums.length) {
9+
total += nums[right];
10+
while(total >= target) {
11+
result = Math.min(right - left + 1, result);
12+
total -= nums[left++];
13+
}
14+
right++;
15+
}
16+
17+
return result === Infinity ? 0 : result;
18+
}
19+
20+
let target1 = 7, nums1 = [2,4,1,2,4,3];
21+
let target2 = 5, nums2 = [1, 5, 5, 5];
22+
let target3 = 15, nums3 = [2, 2, 2, 2, 2];
23+
24+
console.log(minSizeSubarraySum(nums1, target1));
25+
console.log(minSizeSubarraySum(nums2, target2));
26+
console.log(minSizeSubarraySum(nums3, target3));
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Algorithmic Steps**
2+
This problem is solved with the help of sliding window approach without using any special data structure. The algorithmic approach can be summarized as follows:
3+
4+
1. Initialize left and right pointers to 0, to keep track of the substring window boundaries.
5+
6+
2. Initialize total variable to 0, to store the sum of the subarray values to meet the target criteria.
7+
8+
3. Initialize result variable to infinity to indicate that there is no subarray to meet target value.
9+
10+
4. Iterate over the input array using right pointer until the end of the string.
11+
12+
5. Calculate the total value by adding array value at respective right pointer.
13+
14+
6. If the total value is greather or equal to target, find the minimum of subarray sum and shrink the current window total and left pointer value. This step need to be repeated until there are no subarray exists to meet the target criteria.
15+
16+
7. Increment the right pointer to find the next subarray.
17+
18+
8. Repeat steps 4–7 until the right pointer reaches the end of the array.
19+
20+
9. Return 0 if the result is still an infinity which indicates no valid subarray otherwise return the calculated minimum subarray value.
21+
22+
**Time and Space complexity:**
23+
This algorithm has a time complexity of O(n) because we are traversing the array only twice at max. Since it doens't require any datastructure, the space complexity will be O(1).

src/javascript/algorithms/strings/LongestSubstringWithoutRepeatingChars/longestSubstringWithoutRepeatingChars.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// Sliding window: TC: O(n), SC: O(n)
22
function longestSubstringLengthWithoutRepeatingChar(string) {
33

4-
let set = new Set();
4+
let charSet = new Set();
55
let left = right = maxLength = 0;
66

77
while(right < string.length) {
8-
while(set.has(string[right])) {
9-
set.delete(string[left]);
8+
while(charSet.has(string[right])) {
9+
charSet.delete(string[left]);
1010
left++;
1111
}
12-
set.add(string[right]);
12+
charSet.add(string[right]);
1313
right++;
14-
maxLength = Math.max(maxLength, set.size);
14+
maxLength = Math.max(maxLength, charSet.size);
1515
}
1616
return maxLength;
1717
}

0 commit comments

Comments
 (0)