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
Copy file name to clipboardExpand all lines: src/javascript/algorithms/array/6.minimumSizeSubarraySum/minSizeSubarraySum.md
+20-16Lines changed: 20 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -12,26 +12,30 @@ Output: 2
12
12
Input: target = 15, nums = [2, 2, 2, 2, 2]
13
13
Output: 0
14
14
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:
17
15
18
-
1. Initialize left and right pointers to 0, to keep track of the substring window boundaries.
16
+
---
19
17
20
-
2. Initialize total variable to 0, to store the sum of the subarray values to meet the target criteria.
18
+
### Algorithmic Steps
21
19
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:
23
21
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`.
25
32
26
-
5. Calculate the total value by adding array value at respective right pointer.
33
+
---
27
34
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
29
36
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.
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)
3
2
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
+
---
5
12
6
13
### Examples
7
14
@@ -15,26 +22,40 @@ Example 2:
15
22
Input: nums = [2,1,0]
16
23
Output: [0,1,2]
17
24
18
-
**Algorithmic Steps:**
25
+
---
26
+
27
+
## Algorithmic Steps (Two-Pointer / Dutch National Flag)
19
28
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:
21
30
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.
22
34
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:
24
36
25
-
2. Initialize current index pointer(i.e, `i`) to 0, to keep track of the current character while iterating the array.
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`
28
48
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.
30
50
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
+
---
32
52
33
-
6. If the character is neither `0` or `2`, then just increment the index pointer.
53
+
## Time and Space Complexity
34
54
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.
36
57
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.
38
60
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.
0 commit comments