|
| 1 | +from queue import PriorityQueue |
| 2 | + |
| 3 | +# O(NlogN) |
| 4 | + |
| 5 | + |
| 6 | +def simple_method(arr, k): |
| 7 | + arr.sort() |
| 8 | + return arr[len(arr) - k] |
| 9 | + |
| 10 | + |
| 11 | +""" |
| 12 | +O(N*logK) Approach with heap |
| 13 | +
|
| 14 | +We throw away items that items smaller then |
| 15 | +the kth largest item. So that we grab the smallest item |
| 16 | +left in the heap is the second largest item. |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +def heap_approach(arr, k): |
| 21 | + q = PriorityQueue() |
| 22 | + for num in arr: |
| 23 | + q.put(num) |
| 24 | + if q.qsize() > k: |
| 25 | + q.get() |
| 26 | + |
| 27 | + return q.get() |
| 28 | + |
| 29 | + |
| 30 | +""" |
| 31 | +O(N) Approach using partitions |
| 32 | +Think of BUD (Bottlenecks, Unnecessary work, Duplicate work) |
| 33 | +
|
| 34 | +We can do a partition and eliminate half of the search space (on average)self. |
| 35 | +It works on the fact that in a sorted array: |
| 36 | +
|
| 37 | +- The kth largest item must be at index n-k |
| 38 | +- Generally speaking if we do a partition then if we know the |
| 39 | +kth largest item should be at an index greater then the index of pivot |
| 40 | +then we should focus our search on the items with greater indices (or to the right) |
| 41 | +""" |
| 42 | + |
| 43 | + |
| 44 | +import math |
| 45 | +import random |
| 46 | + |
| 47 | + |
| 48 | +def kthLargest(arr, k): |
| 49 | + ''' |
| 50 | + :type arr: list of int |
| 51 | + :type k: int |
| 52 | + :rtype: int |
| 53 | + ''' |
| 54 | + n = len(arr) |
| 55 | + left = 0 |
| 56 | + right = n - 1 |
| 57 | + |
| 58 | + while left <= right: |
| 59 | + # Random pivot index will ensure on average we avoid |
| 60 | + # O(N^2) runtime |
| 61 | + choosen_pivot_index = random.randint(left, right) |
| 62 | + |
| 63 | + final_index_of_choosen_pivot = partition( |
| 64 | + arr, left, right, choosen_pivot_index) |
| 65 | + |
| 66 | + # What does the 'finalIndexOfChoosenPivot' tell us? |
| 67 | + if (final_index_of_choosen_pivot == n - k): |
| 68 | + |
| 69 | + # If the pivot index in the (n-k)th index then we are done and can stop here |
| 70 | + return arr[final_index_of_choosen_pivot] |
| 71 | + elif (final_index_of_choosen_pivot > n - k): |
| 72 | + # k'th largest must be in the left partition. |
| 73 | + right = final_index_of_choosen_pivot - 1 |
| 74 | + else: |
| 75 | + # finalIndexOfChoosenPivot < n - k |
| 76 | + |
| 77 | + # k'th largest must be in the right partition. |
| 78 | + |
| 79 | + left = final_index_of_choosen_pivot + 1 |
| 80 | + |
| 81 | + return -1 |
| 82 | + |
| 83 | + |
| 84 | +def partition(self, arr, left, right, pivot_index): |
| 85 | + pivot_value = arr[pivot_index] |
| 86 | + lesser_items_tail_index = left |
| 87 | + |
| 88 | + # Move the item at the 'pivotIndex' OUT OF THE WAY. We are about to bulldoze |
| 89 | + # through the partitioning space and we don't want it in the way |
| 90 | + swap(arr, pivot_index, right) |
| 91 | + |
| 92 | + for i in range(left, right): |
| 93 | + if arr[i] < pivot_value: |
| 94 | + swap(arr, i, lesser_items_tail_index) |
| 95 | + lesser_items_tail_index += 1 |
| 96 | + |
| 97 | + # Ok...partitioning is done. Swap the pivot item BACK into the space we just |
| 98 | + # partitioned at the 'lesserItemsTailIndex'...that's where the pivot item |
| 99 | + # belongs |
| 100 | + |
| 101 | + # In the middle of the "sandwich". |
| 102 | + |
| 103 | + swap(arr, right, lesser_items_tail_index) |
| 104 | + |
| 105 | + return lesser_items_tail_index |
| 106 | + |
| 107 | + |
| 108 | +def swap(self, arr, first, second): |
| 109 | + temp = arr[first] |
| 110 | + arr[first] = arr[second] |
| 111 | + arr[second] = temp |
| 112 | + |
| 113 | + |
| 114 | +arr = [8, 1, 3, 2, 6, 7] |
| 115 | + |
| 116 | +print(heap_approach(arr, 2)) |
0 commit comments