From d63202bf2089806e111a80f542f2be5719cb5af8 Mon Sep 17 00:00:00 2001 From: tactonbishop Date: Sat, 2 Jul 2022 19:30:04 +0100 Subject: [PATCH] Last Sync: 2022-07-02 19:30:04 --- Data_Structures/Patterns/Module_pattern.md | 2 -- Data_Structures/Patterns/Sliding_window.md | 41 +++++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Data_Structures/Patterns/Module_pattern.md b/Data_Structures/Patterns/Module_pattern.md index 3d92a03..d4c3cc9 100644 --- a/Data_Structures/Patterns/Module_pattern.md +++ b/Data_Structures/Patterns/Module_pattern.md @@ -43,8 +43,6 @@ In the example above, `aValue` could be edited in instantiations of the class. G ## Object modules ---- - If you want to use an object instead of a class, you have to take greater care to ensure that the objects are not overwritable. **Also you cannot use the `#` modifier to make properties private.** - Use getters and setters for updating and retrieving values diff --git a/Data_Structures/Patterns/Sliding_window.md b/Data_Structures/Patterns/Sliding_window.md index acaf2f8..88de969 100644 --- a/Data_Structures/Patterns/Sliding_window.md +++ b/Data_Structures/Patterns/Sliding_window.md @@ -6,4 +6,43 @@ tags: # Sliding window -Whereas the [multiple pointer](Multiple_pointers.md) pattern works by keeping two array indices running at once and comparing their values, the sliding +Whereas the [multiple pointer](Multiple_pointers.md) pattern works by keeping two array indices running at once and comparing their values on each iteration, the sliding window +has a running value (the 'window') that is updated at each iteration and which compares itself against its most recent previous value. + +This is what makes it 'sliding': the value isn't constant, it changes (or doesn't) based on what it was previously. + +## Example: maximum sum of sub-array + +We create a function that takes an array and a sub-array length _n_. The objective is to find the maximum value that can be created by summing _n_ elements of the array. It moves through the array, summing by _n_ keeping track of the highest sum value so far and the current sum value. At the end it should return the highest possible sum value. + +Here we do this for a sub-array of length 2: + +```ts +function maxSubarraySum(arr, subArrLength) { + let maxSum = 0; // Largest sum value so far + let tempSum = 0; // Current sum value + + // Establish the first 'maxSum' + // At the beginning this will just be the sum of the first two array elements + for (let i = 0; i < subArrLength; i++) { + maxSum += arr[i]; + } + + // Map temporary sum to maxSum + // Accordingly, as we have only mapped the first sub-array, the max sum will be the same + // as the current sum + tempSum = maxSum; + + // Move through the array one element at a time (`i++`) via a window starting from the element that is equal to `subArrLength` + // The first sum calculation is already taken care of in the earlier loop and stored in `maxSum`, so we don't have to worry about missing the elements in indices less than subArrLength + for (let i = subArrLength; i < arr.length; i++) { + // Temp sum becomes a moveable window value equal to the subtraction of the previous element and the addition of next element in line + tempSum = tempSum - arr[i - subArrLength] + arr[i]; + // Max sum is redefined as the largest subArrLengthber between the previous highest and the current value of tempSum + maxSum = Math.max(maxSum, tempSum); + } + return maxSum; +} + +console.log(maxSubarraySum([100, 200, 300, 400], 2)); // 700 +``` \ No newline at end of file