-
Notifications
You must be signed in to change notification settings - Fork 2
feat(algorithms, heaps): top k and kth largest #152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
608e7b1
feat(algorithms, heaps): top k and kth largest
BrianLusina 0f36bfc
updating DIRECTORY.md
9a3ea30
feat(algorithms, heaps): top k closest point to origin
BrianLusina 2bb4e1f
updating DIRECTORY.md
5af34d3
Update algorithms/heap/topklargest/__init__.py
BrianLusina bb4ced1
Update algorithms/heap/topklargest/__init__.py
BrianLusina 8bbd95b
Update algorithms/heap/topkclosesttoorigin/__init__.py
BrianLusina 90c4681
Update algorithms/heap/topkclosesttoorigin/test_top_k_closest_to_orig…
BrianLusina 6ca89ad
refactor(algorithms, heaps): move from puzzles package
BrianLusina 2db74fa
feat(algorithms, matrix, dfs): is valid sudoku matrix puzzle and dfs …
BrianLusina 7256932
refactor(algorithms, heaps): move to heap package
BrianLusina 98018a8
updating DIRECTORY.md
39a67e0
Update algorithms/matrix/isvalidsudoku/__init__.py
BrianLusina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| # Top-K Largest Elements in an Array | ||
|
|
||
| Given an integer array nums, return the 3 largest elements in the array in any order. | ||
|
|
||
| ## Example | ||
|
|
||
| ```text | ||
| Input: nums = [9, 3, 7, 1, -2, 6, 8] | ||
| Output: [8, 7, 9] | ||
| # or [7, 9, 8] or [9, 7, 8] ... | ||
| ``` | ||
|
|
||
| ## Solution | ||
|
|
||
| Here's how we can solve this problem using a min-heap: | ||
|
|
||
| - Create a min-heap that stores the first 3 elements of the array. These represent the 3 largest elements we have seen so | ||
| far, with the smallest of the 3 at the root of the heap. | ||
|  | ||
|
|
||
| - Iterate through the remaining elements in the array. | ||
| - If the current element is larger than the root of the heap, pop the root and push the current element into the heap. | ||
| - Otherwise, continue to the next element. | ||
|
|
||
|  | ||
|  | ||
|  | ||
|  | ||
|
|
||
| After iterating through all the elements, the heap contains the 3 largest elements in the array. | ||
|
|
||
| ### Complexity Analysis | ||
|
|
||
| #### Time Complexity Breakdown | ||
|
|
||
| - The heapify function takes O(3) = O(1) time | ||
| - We iterate through all elements in the array once: O(n) time | ||
| - The heappop and heappush operations take O(log 3) = O(1) time each | ||
|
|
||
| #### Space Complexity | ||
|
|
||
| - We use a heap of size 3 to store the 3 largest elements: O(3) = O(1) space | ||
| - The algorithm uses constant space regardless of input size | ||
|
|
||
| Note: The time and space complexity become more interesting when 3 is a variable number k. | ||
|
|
||
| --- | ||
|
|
||
| # Kth Largest Element in an Array | ||
|
|
||
| Write a function that takes an array of unsorted integers nums and an integer k, and returns the kth largest element in | ||
| the array. This function should run in O(n log k) time, where n is the length of the array. | ||
|
|
||
| ## Examples | ||
|
|
||
| ```text | ||
| Input: | ||
| nums = [5, 3, 2, 1, 4] | ||
| k = 2 | ||
|
|
||
| Output: 4 | ||
| ``` | ||
|
|
||
| ## Solutions | ||
|
|
||
| - [Approach 1](#approach-1-sorting) | ||
| - [Approach 2](#approach-2-min-heap) | ||
|
|
||
| ### Approach 1: Sorting | ||
|
|
||
| The simplest approach is to sort the array in descending order and return the kth element. This approach has a time | ||
| complexity of O(n log n) where n is the number of elements in the array, and a space complexity of O(1). | ||
|
|
||
| ### Approach 2: Min Heap | ||
|
|
||
| By using a min-heap, we can reduce the time complexity to O(n log k), where n is the number of elements in the array and | ||
| k is the value of k. | ||
| The idea behind this solution is to iterate over the elements in the array while storing the k largest elements we've | ||
| seen so far in a min-heap. At each element, we check if it is greater than the smallest element (the root) of the heap. | ||
| If it is, we pop the smallest element from the heap and push the current element into the heap. This way, the heap will | ||
| always contain the k largest elements we've seen so far. | ||
| After iterating over all the elements, the root of the heap will be the kth largest element in the array. | ||
|
|
||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|
|
||
| #### Complexity Analysis | ||
|
|
||
| ##### Time Complexity: O(n log k) | ||
|
|
||
| Where n is the number of elements in the array and k is the input parameter. We iterate over | ||
| all elements, and in the worst case, we both push and pop each element from the heap, which takes O(log k) time per | ||
| element. | ||
|
|
||
| ##### Space Complexity: O(k) | ||
|
|
||
| Where k is the input parameter. The space is used by the heap to store the k largest elements. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| from typing import List | ||
| import heapq | ||
|
|
||
|
|
||
| def k_largest(nums: List[int], k: int = 3) -> List[int]: | ||
| """ | ||
| Finds the k largest elements in a given list. K is defaulted to three, but can be used to tweak the k largest | ||
| elements in the array | ||
| Args: | ||
| nums(list): list of elements to check for | ||
| k(int): number of elements to check, defaulted to 3 | ||
| Returns: | ||
| list: top k largest elements | ||
| """ | ||
| # create a minimum heap with the first k elements | ||
| min_heap = nums[:k] | ||
| heapq.heapify(min_heap) | ||
|
|
||
| # iterate through the remaining elements | ||
| for num in nums[k:]: | ||
| # if the current number is greater than the element at the top of the heap | ||
| if num > min_heap[0]: | ||
| # Remove it and add this element | ||
| heapq.heappushpop(min_heap, num) | ||
|
|
||
| # return the top k elements | ||
| return min_heap | ||
|
|
||
|
|
||
| def kth_largest(nums: List[int], k: int) -> int: | ||
| """ | ||
| Finds the kth largest element in a given list | ||
| Args: | ||
| nums(list): list of elements to check for | ||
| k(int): the kth largest element to return | ||
| Returns: | ||
| int: the kth largest element | ||
| """ | ||
| # input validation to ensure we don't get unexpected results | ||
| if not nums or k <= 0 or k > len(nums): | ||
| return -1 | ||
|
|
||
| # create a minimum heap with the first k elements | ||
| min_heap = [] | ||
|
|
||
| # iterate through the remaining elements | ||
| for num in nums: | ||
| if len(min_heap) < k: | ||
| heapq.heappush(min_heap, num) | ||
| # if the current number is greater than the element at the top of the heap | ||
| elif num > min_heap[0]: | ||
| # Remove it and add this element | ||
| heapq.heappushpop(min_heap, num) | ||
|
|
||
| # return the top kth element | ||
| return min_heap[0] | ||
|
|
||
|
|
||
| def kth_largest_sorting(nums: List[int], k: int) -> int: | ||
| """ | ||
| Finds the kth largest element in a given list using sorting | ||
| Args: | ||
| nums(list): list of elements to check for | ||
| k(int): the kth largest element to return | ||
| Returns: | ||
| int: the kth largest element | ||
| """ | ||
| # input validation to ensure we don't get unexpected results | ||
| if not nums or k <= 0 or k > len(nums): | ||
| return -1 | ||
|
|
||
| # Sort the list in place which incurs a time complexity cost of O(n log(n)). Space complexity is O(n) due to using | ||
| # an in-memory data structure to store the elements during sorting using timsort in Python | ||
| nums.sort(reverse=True) | ||
| # Return the kth largest element | ||
| return nums[k - 1] | ||
|
BrianLusina marked this conversation as resolved.
Outdated
|
||
Binary file added
BIN
+31.7 KB
...s/heap/topklargest/images/solutions/kth_largest_element_in_array_solution_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+48.7 KB
.../heap/topklargest/images/solutions/kth_largest_element_in_array_solution_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+30.8 KB
.../heap/topklargest/images/solutions/kth_largest_element_in_array_solution_11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+28.1 KB
...s/heap/topklargest/images/solutions/kth_largest_element_in_array_solution_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+26.8 KB
...s/heap/topklargest/images/solutions/kth_largest_element_in_array_solution_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+40.2 KB
...s/heap/topklargest/images/solutions/kth_largest_element_in_array_solution_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+38.1 KB
...s/heap/topklargest/images/solutions/kth_largest_element_in_array_solution_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+45.2 KB
...s/heap/topklargest/images/solutions/kth_largest_element_in_array_solution_6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+44.2 KB
...s/heap/topklargest/images/solutions/kth_largest_element_in_array_solution_7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+41.9 KB
...s/heap/topklargest/images/solutions/kth_largest_element_in_array_solution_8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+43.8 KB
...s/heap/topklargest/images/solutions/kth_largest_element_in_array_solution_9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+22.2 KB
...heap/topklargest/images/solutions/top_k_largest_element_in_array_solution_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+31.8 KB
...heap/topklargest/images/solutions/top_k_largest_element_in_array_solution_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+29.9 KB
...heap/topklargest/images/solutions/top_k_largest_element_in_array_solution_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+40.9 KB
...heap/topklargest/images/solutions/top_k_largest_element_in_array_solution_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+40.9 KB
...heap/topklargest/images/solutions/top_k_largest_element_in_array_solution_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions
45
algorithms/heap/topklargest/test_top_k_largest_elements.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import unittest | ||
| from typing import List | ||
| from parameterized import parameterized | ||
| from algorithms.heap.topklargest import k_largest, kth_largest, kth_largest_sorting | ||
|
|
||
| K_LARGEST_ELEMENTS_TEST_CASES = [ | ||
| ([9, 3, 7, 1, -2, 6, 8], 3, [8, 7, 9]), | ||
| ([5, 3, 2, 1, 4], 2, [5, 4]), | ||
| ] | ||
|
|
||
|
|
||
| class TopKLargestElementsTestCase(unittest.TestCase): | ||
| @parameterized.expand(K_LARGEST_ELEMENTS_TEST_CASES) | ||
| def test_top_k_largest_elements(self, nums: List[int], k: int, expected: List[int]): | ||
| actual = k_largest(nums, k) | ||
| self.assertListEqual(sorted(expected), sorted(actual)) | ||
|
|
||
|
|
||
| KTH_LARGEST_ELEMENTS_TEST_CASES = [ | ||
| ([3, 2, 1, 5, 6, 4], 2, 5), | ||
| ([3, 2, 3, 1, 2, 4, 5, 5, 6], 4, 4), | ||
| ([1], 1, 1), | ||
| ([7, 10, 4, 3, 20, 15], 3, 10), | ||
| ([7, 10, 4, 3, 20, 15], 4, 7), | ||
| ([7, 10, 4, 3, 20, 15], 5, 4), | ||
| ([5, 3, 2, 1, 4], 2, 4), | ||
| ] | ||
|
|
||
|
|
||
| class TopKthLargestElementsTestCase(unittest.TestCase): | ||
| @parameterized.expand(KTH_LARGEST_ELEMENTS_TEST_CASES) | ||
| def test_top_kth_largest_element(self, nums: List[int], k: int, expected: int): | ||
| actual = kth_largest(nums, k) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| @parameterized.expand(KTH_LARGEST_ELEMENTS_TEST_CASES) | ||
| def test_top_kth_largest_element_sorting( | ||
| self, nums: List[int], k: int, expected: int | ||
| ): | ||
| actual = kth_largest_sorting(nums, k) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.