From c2bdd92ec4b0afb6be43bf1610d19877ba397855 Mon Sep 17 00:00:00 2001 From: Jesse Millwood Date: Wed, 30 Oct 2024 17:49:58 -0400 Subject: [PATCH] Added sliding window --- Readme.org | 4 +- libs/chapter1/chapter1.hpp | 2 + libs/chapter1/meson.build | 3 +- libs/chapter1/sliding_window.cpp | 82 ++++++++++++++++++++++++++++++++ tests/test.cpp | 14 ++++++ 5 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 libs/chapter1/sliding_window.cpp diff --git a/Readme.org b/Readme.org index ce7a0c4..75857b1 100644 --- a/Readme.org +++ b/Readme.org @@ -44,13 +44,13 @@ To run the tests: #+end_src * Plan -** Arrays [2/6] +** Arrays [3/6] - [X] Is Unique (CTCI: 1.1) - [X] Check permutation (CTCI: 1.2) - [ ] String Compression (CTCI: 1.6) - [ ] Rotate Matrix (CTCI: 1.7) - [ ] Zero Matrix (CTCI: 1.8) -- [ ] Sliding Window ([[https://www.geeksforgeeks.org/window-sliding-technique/][GfG: Sliding Window Technique]]) +- [X] Sliding Window ([[https://www.geeksforgeeks.org/window-sliding-technique/][GfG: Sliding Window Technique]]) ** Linked Lists [1/6] - [X] Remove Duplicates (CTCI: 2.1) - [ ] Return Kth to Last (CTCI: 2.2) diff --git a/libs/chapter1/chapter1.hpp b/libs/chapter1/chapter1.hpp index ccc57c4..18fe0bb 100644 --- a/libs/chapter1/chapter1.hpp +++ b/libs/chapter1/chapter1.hpp @@ -4,3 +4,5 @@ bool IsUnique(const char* s, int len); bool CheckPermutation(const char* s1, const char* s2, size_t s1_len, size_t s2_len); +int sliding_window_max_sum(int* array, int len, int k_len); +int sliding_window_shortest_subarray(int* array,int len, int x); diff --git a/libs/chapter1/meson.build b/libs/chapter1/meson.build index 490a549..a0fe21c 100644 --- a/libs/chapter1/meson.build +++ b/libs/chapter1/meson.build @@ -1,5 +1,6 @@ -chapter1_sources = ['is_unique.cpp', 'check_permutation.cpp'] +chapter1_sources = ['is_unique.cpp', 'check_permutation.cpp', + 'sliding_window.cpp'] chapter1_lib = static_library('chapter1', chapter1_sources) diff --git a/libs/chapter1/sliding_window.cpp b/libs/chapter1/sliding_window.cpp new file mode 100644 index 0000000..84dae79 --- /dev/null +++ b/libs/chapter1/sliding_window.cpp @@ -0,0 +1,82 @@ +#include "chapter1.hpp" + +/** + * \brief sliding_window_max_sum + * + * Detailed description + * + * \param array The input array + * \param len The length of the input array + * \param k_len The length of the subarray + * \return return int The maximum found in the subarray + */ +int sliding_window_max_sum(int* array, int len, int k_len){ + int current_max = 0; + int* window_beg = array; + int* window_end = (array + k_len); + int previous_beg = 0; + // initial sum + for (int i = 0; i < k_len; i++){ + current_max += array[i]; + } + previous_beg = *window_beg; + window_beg += 1; + window_end += 1; + + // iterate window + int current_sum = current_max; + while(window_end != (array + len - 1)){ + current_sum = current_sum - previous_beg + *window_end; + if (current_sum > current_max){ + current_max = current_sum; + } + previous_beg = *window_beg; + window_beg += 1; + window_end += 1; + } + + return current_max; +} + + +/** + * \brief sliding_window_shortest_subarray + * + * Find the shortest subarray that is greater than or equal to x. + * + * \param array The intput array + * \param len The length of the input array + * \param x The threshold to compare to + * \return return the length of the shortest subarray + */ +int sliding_window_shortest_subarray(int* array,int len, int x){ + + int* window_beg = &array[0]; + int* window_end = &array[1]; + int current_sum = array[0] + array[1]; + int min_len = len; + int sub_array_len = len; + + + while(window_beg != (array + len - 1)){ + if(current_sum < x){ + window_end += 1; + current_sum += *window_end; + } + if (current_sum >= x){ + sub_array_len = window_end - window_beg + 1; + if(sub_array_len < min_len){ + min_len = sub_array_len; + } + current_sum -= *window_beg; + window_beg += 1; + if(window_beg > window_end){ + window_end += 1; + current_sum += *window_end; + } + } + + } + + return min_len; +} diff --git a/tests/test.cpp b/tests/test.cpp index 1e47da6..092d6cb 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -32,6 +32,20 @@ TEST(ChapterOne, CheckPermutations){ } +TEST(ChapterOne, SubArraySum){ + // Not actually in CTCI but is related to arrays + + // Prompt: Given an array, find the sum of all subarrays of length K + // (fixed sliding window) + int a1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + CHECK_EQUAL(27, sliding_window_max_sum(a1, 11, 3)); + + // Prompt: Find the shortest subarray with a sum greater than or + // equal to X (dynamic sliding window) + int a2[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; + CHECK_EQUAL(1, sliding_window_shortest_subarray(a2, 9, 7)); +} + // Chapter 2: Linked Lists ___________________________________________ TEST_GROUP(ChapterTwo){};