Skip to content

Commit fdd362b

Browse files
committed
Update minsize Subarray sum & sort colors algorithms
1 parent f12cc38 commit fdd362b

File tree

3 files changed

+70
-41
lines changed

3 files changed

+70
-41
lines changed

src/javascript/algorithms/array/6.minimumSizeSubarraySum/minSizeSubarraySum.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,29 @@
33
function minSizeSubarraySum(nums, target) {
44

55
let left = right = total = 0;
6-
let result = Infinity;
6+
let minLength = Infinity;
77

88
while(right < nums.length) {
99
total += nums[right];
1010
while(total >= target) {
11-
result = Math.min(right - left + 1, result);
11+
minLength = Math.min(right - left + 1, minLength);
1212
total -= nums[left++];
1313
}
1414
right++;
1515
}
1616

17-
return result === Infinity ? 0 : result;
17+
return minLength === Infinity ? 0 : minLength;
1818
}
1919

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));
20+
// Test Cases
21+
const testCases = [
22+
{ target: 7, nums: [2, 4, 1, 2, 4, 3] },
23+
{ target: 5, nums: [1, 5, 5, 5] },
24+
{ target: 15, nums: [2, 2, 2, 2, 2] }
25+
];
26+
27+
for (const { target, nums } of testCases) {
28+
console.log(`Input: target = ${target}, nums = [${nums}]`);
29+
console.log(`Output: ${minSizeSubarraySum(nums, target)}\n`);
30+
}
2731

src/javascript/algorithms/array/6.minimumSizeSubarraySum/minSizeSubarraySum.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,30 @@ Output: 2
1212
Input: target = 15, nums = [2, 2, 2, 2, 2]
1313
Output: 0
1414

15-
**Algorithmic Steps:**
16-
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:
1715

18-
1. Initialize left and right pointers to 0, to keep track of the substring window boundaries.
16+
---
1917

20-
2. Initialize total variable to 0, to store the sum of the subarray values to meet the target criteria.
18+
### Algorithmic Steps
2119

22-
3. Initialize result variable to max integer value to indicate that there is no subarray to meet target value.
20+
This problem is efficiently solved using the **sliding window** technique without requiring any special data structures. The approach can be summarized as follows:
2321

24-
4. Iterate over the input array using right pointer until the end of the string.
22+
1. Initialize two pointers, `left` and `right`, both at `0`. These represent the current window boundaries.
23+
2. Initialize a variable `total` to `0` to keep track of the sum of the current window.
24+
3. Initialize `minLength` to a very large number (e.g., infinity) to represent the length of the smallest valid subarray found so far.
25+
4. Iterate through the array with the `right` pointer:
26+
- Add `nums[right]` to `total`.
27+
- While `total` is greater than or equal to `target`:
28+
- Update `minLength` to the smaller value between the current `minLength` and the window size (`right - left + 1`).
29+
- Subtract `nums[left]` from `total` and move the `left` pointer forward to try and find a smaller valid window.
30+
5. Continue until the `right` pointer reaches the end of the array.
31+
6. If `minLength` is still infinity (meaning no valid subarray was found), return `0`. Otherwise, return `minLength`.
2532

26-
5. Calculate the total value by adding array value at respective right pointer.
33+
---
2734

28-
6. If the total value is greater 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.
35+
### Time and Space Complexity
2936

30-
7. Increment the right pointer to find the next subarray.
31-
32-
8. Repeat steps 4–7 until the right pointer reaches the end of the array.
33-
34-
9. Return 0 if the result is still a maximum integer which indicates no valid subarray otherwise return the calculated minimum subarray value.
35-
36-
**Time and Space complexity:**
37-
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).
37+
- **Time Complexity:** `O(n)`
38+
The array is traversed at most twice (once by each pointer), resulting in linear time complexity.
39+
40+
- **Space Complexity:** `O(1)`
41+
No additional data structures are used; only a fixed number of variables are maintained.
Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
**Description:**
2-
Given an array `nums` with `n` objects colored red, white, where each object is color coded as integer: blue, 0 for red, 1 for white, and 2 for blue. We need to sort them in-place so that objects of the same color are grouped together, with the colors in the order red, white, and blue.
1+
# Sort Colors (Dutch National Flag Problem)
32

4-
This problem needs to be solved without using the library's sort function.
3+
## Description
4+
5+
Given an array `nums` containing `n` integers where each integer is `0` (red), `1` (white), or `2` (blue), sort the array **in-place** so that elements of the same color are adjacent and in the order: red (0), white (1), and blue (2).
6+
7+
**Constraints:**
8+
- Must not use any built-in sorting functions.
9+
- Must use constant space (`O(1)`).
10+
11+
---
512

613
### Examples
714

@@ -15,26 +22,40 @@ Example 2:
1522
Input: nums = [2,1,0]
1623
Output: [0,1,2]
1724

18-
**Algorithmic Steps:**
25+
---
26+
27+
## Algorithmic Steps (Two-Pointer / Dutch National Flag)
1928

20-
This problem is solved with the help of **two pointers** technique. It is also know as **"Dutch National Flag problem"**. The algorithmic approach can be summarized as follows:
29+
We use a **two-pointer** approach with three indices:
2130

31+
- `left`: the position where the next `0` should be placed.
32+
- `right`: the position where the next `2` should be placed.
33+
- `i`: the current index we are evaluating.
2234

23-
1. Initialize both left and right pointers to first index(i.e, `0`) and end index(i.e, `nums.length-1`) of the array respectively, to keep track of the current window boundaries.
35+
### Steps:
2436

25-
2. Initialize current index pointer(i.e, `i`) to 0, to keep track of the current character while iterating the array.
37+
1. Initialize `left = 0`, `i = 0`, and `right = nums.length - 1`.
2638

27-
3. Iterate over the input array using index pointer until the end of the array.
39+
2. Traverse the array while `i <= right`:
40+
- If `nums[i] == 0`:
41+
- Swap `nums[i]` and `nums[left]`
42+
- Increment both `i` and `left`
43+
- If `nums[i] == 2`:
44+
- Swap `nums[i]` and `nums[right]`
45+
- Decrement `right` (do **not** increment `i` here)
46+
- If `nums[i] == 1`:
47+
- Just increment `i`
2848

29-
4. If the current character is equals to 0, swap the character values at left pointer and index pointer. Also, increment the left pointer and index pointer.
49+
3. The array will be sorted in one pass.
3050

31-
5. If the current character is equals to 2, swap the character values at right pointer and index pointer. Also, decrement the right pointer.
51+
---
3252

33-
6. If the character is neither `0` or `2`, then just increment the index pointer.
53+
## Time and Space Complexity
3454

35-
7. Repeat steps 4–6 until the index pointer reaches the end of the array.
55+
- **Time Complexity:** `O(n)`
56+
Each element is processed at most once.
3657

37-
8. Return the updated in-place array where characters are sorted.
58+
- **Space Complexity:** `O(1)`
59+
Sorting is done in-place without any additional data structures.
3860

39-
**Time and Space complexity:**
40-
This algorithm takes a time complexity of `O(n)` because we are traversing the array only once. Also, it requires space complexity of `O(1)` because we are updating the array in-place without using an additional data structure.
61+
---

0 commit comments

Comments
 (0)