Added sliding window
This commit is contained in:
		
							parent
							
								
									65996765d1
								
							
						
					
					
						commit
						c2bdd92ec4
					
				| @ -44,13 +44,13 @@ To run the tests: | |||||||
| #+end_src | #+end_src | ||||||
| 
 | 
 | ||||||
| * Plan | * Plan | ||||||
| ** Arrays [2/6] | ** Arrays [3/6] | ||||||
| - [X] Is Unique (CTCI: 1.1) | - [X] Is Unique (CTCI: 1.1) | ||||||
| - [X] Check permutation (CTCI: 1.2) | - [X] Check permutation (CTCI: 1.2) | ||||||
| - [ ] String Compression (CTCI: 1.6) | - [ ] String Compression (CTCI: 1.6) | ||||||
| - [ ] Rotate Matrix (CTCI: 1.7) | - [ ] Rotate Matrix (CTCI: 1.7) | ||||||
| - [ ] Zero Matrix (CTCI: 1.8) | - [ ] 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] | ** Linked Lists [1/6] | ||||||
| - [X] Remove Duplicates (CTCI: 2.1) | - [X] Remove Duplicates (CTCI: 2.1) | ||||||
| - [ ] Return Kth to Last (CTCI: 2.2) | - [ ] Return Kth to Last (CTCI: 2.2) | ||||||
|  | |||||||
| @ -4,3 +4,5 @@ | |||||||
| 
 | 
 | ||||||
| bool IsUnique(const char* s, int len); | bool IsUnique(const char* s, int len); | ||||||
| bool CheckPermutation(const char* s1, const char* s2, size_t s1_len, size_t s2_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); | ||||||
|  | |||||||
| @ -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_lib = static_library('chapter1', | ||||||
|                               chapter1_sources) |                               chapter1_sources) | ||||||
|  | |||||||
							
								
								
									
										82
									
								
								libs/chapter1/sliding_window.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								libs/chapter1/sliding_window.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -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; | ||||||
|  | } | ||||||
| @ -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 ___________________________________________
 | // Chapter 2: Linked Lists ___________________________________________
 | ||||||
| 
 | 
 | ||||||
| TEST_GROUP(ChapterTwo){}; | TEST_GROUP(ChapterTwo){}; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user