From 0845c513867d1f6aeabeadd246409d09f18a058a Mon Sep 17 00:00:00 2001 From: Himadri Bhattacharjee <107522312+lavafroth@users.noreply.github.com> Date: Fri, 31 Oct 2025 20:29:38 +0530 Subject: [PATCH] feat: addgrpa --- content/skTa4V7n8H8.md | 81 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 content/skTa4V7n8H8.md diff --git a/content/skTa4V7n8H8.md b/content/skTa4V7n8H8.md new file mode 100644 index 00000000..636fb613 --- /dev/null +++ b/content/skTa4V7n8H8.md @@ -0,0 +1,81 @@ +--- +title: "note skta4v7n8h8" +date: 2025-10-31T20:24:35+05:30 +draft: false +--- + +# GrPA 1 + +```python +def swap(arr, i, j): + arr[i], arr[j] = arr[j], arr[i] + + +def max_heapify(arr, end, current): + left = 2 * current + 1 + right = left + 1 + largest = current + + if left < end and arr[left] > arr[largest]: + largest = left + if right < end and arr[right] > arr[largest]: + largest = right + + if largest != current: + swap(arr, current, largest) + max_heapify(arr, end, largest) + + +def mergeKLists(arr): + arr = [value for subarray in arr for value in subarray] + n = len(arr) + for i in reversed(range(n // 2)): + max_heapify(arr, n, i) + + for i in range(n-1,0,-1): + swap(arr, 0, i) + max_heapify(arr, i, 0) + return arr +``` + +# GrPA 2 + + +```python +def maxLessThan(root, x): + floor = None + while not root.isempty(): + if root.value > x: + root = root.left + + elif root.value <= x: + floor = root.value + root = root.right + return floor +``` + +# GrPA 3 + +Note that this `max_heapify` is a workhorse function which you should learn well +because you're gonna use it very frequently to implement a priority queue. + +```python +def max_heapify(arr, end, current): + left = 2 * current + 1 + right = left + 1 + largest = current + + if left < end and arr[left] > arr[largest]: + largest = left + if right < end and arr[right] > arr[largest]: + largest = right + + if largest != current: + swap(arr, current, largest) + max_heapify(arr, end, largest) + +def min_max(arr): + n = len(arr) + for i in reversed(range(n // 2)): + max_heapify(arr, n, i) +```