chore: clean up content phase organization

This commit is contained in:
Prad Nukala committed 2026-09-24 15:57:38 -04:00
1 parent 403a2d7f23
commit bf0ac34a43
138 files changed
+1436 -368

No files matched your search

@@ -0,0 +1,59 @@
---
title: '215. Kth Largest Element in an Array'
description: Given an integer array nums and an integer k, return the k^th largest element in the array
sidebar:
label: 'Kth Largest Element in an Array'
badge: 'Medium'
---
<Badge variant="accent">Array</Badge>
### Example 1:
- Input: `nums = [3,2,1,5,6,4], k = 2`
- Output: `5`
### Example 2:
- Input: `nums = [3,2,3,1,2,4,5,5,6], k = 4`
- Output: `4`
### Constraints:
- `1 <= k <= nums.length <= 10^5`
- `-10^4 <= nums[i] <= 10^4`
## Approach
```mermaid
flowchart TD
S(["findKthLargest(nums, k)"]) --> F{"more i in range(len(nums))?"}
F -- yes --> N["nums[i] = -nums[i] — negate so the min-heap acts as a max-heap"]
N --> F
F -- no --> H["heapq.heapify(nums)"]
H --> W{"more _ in range(k - 1)?"}
W -- yes --> P["heapq.heappop(nums) — discard the k-1 largest"]
P --> W
W -- no --> E(["return -heapq.heappop(nums)"])
```
## Solution
```py
import heapq
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
for i in range(len(nums)):
nums[i] = -nums[i]
heapq.heapify(nums)
for _ in range(k - 1):
heapq.heappop(nums)
return -heapq.heappop(nums)
```
## Explanation
<YouTube id="XEmy13g1Qxc" title="Kth Largest Element in an Array - Quick Select - Leetcode 215 - Python" />
@@ -0,0 +1,94 @@
---
title: '23. Merge k Sorted Lists'
description: You are given an array of k linked-lists lists, each linked-list is sorted in ascending order
sidebar:
label: 'Merge k Sorted Lists'
badge: 'Hard'
---
<Badge variant="accent">Linked List</Badge>
### Example 1:
- Input: `lists = [[1,4,5],[1,3,4],[2,6]]`
- Output: `[1,1,2,3,4,4,5,6]`
- Explanation: The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted linked list: 1->1->2->3->4->4->5->6
### Example 2:
- Input: `lists = []`
- Output: `[]`
### Example 3:
- Input: `lists = [[]]`
- Output: `[]`
### Constraints:
- `k == lists.length`
- `0 <= k <= 10^4`
- `0 <= lists[i].length <= 500`
- `-10^4 <= lists[i][j] <= 10^4`
- `lists[i]` is sorted in ascending order.
- The sum of lists[i].length will not exceed 10^4.
## Approach
```mermaid
flowchart TD
S(["mergeKLists(lists)"]) --> I["heap = []"]
I --> F{"more i, node in enumerate(lists)?"}
F -- yes --> C{"node?"}
C -- yes --> P["heapq.heappush(heap, (node.val, i, node)) — i breaks ties so nodes are never compared"]
P --> F
C -- no --> F
F -- no --> D["D = ListNode(), cur = D"]
D --> W{"heap?"}
W -- no --> E(["return D.next"])
W -- yes --> O["val, i, node = heapq.heappop(heap) — the smallest head across all lists"]
O --> L["cur.next = node, cur = node, node = node.next"]
L --> N{"node?"}
N -- yes --> Q["heapq.heappush(heap, (node.val, i, node))"]
Q --> W
N -- no --> W
```
## Solution
```py
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
import heapq
class Solution:
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
heap = []
# K log K
for i, node in enumerate(lists):
if node:
heapq.heappush(heap, (node.val, i, node))
D = ListNode()
cur = D
# n log k
while heap:
val, i, node = heapq.heappop(heap)
cur.next = node
cur = node
node = node.next
if node:
heapq.heappush(heap, (node.val, i, node))
# Time: O(N log k)
# Space: O(n)
return D.next
```
## Explanation
<YouTube id="q5a5OiGbT6Q" title="Merge K Sorted Lists - Leetcode 23 - Python" />
@@ -0,0 +1,69 @@
---
title: '973. K Closest Points to Origin'
description: Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0)
sidebar:
label: 'K Closest Points to Origin'
badge: 'Medium'
---
<Badge variant="accent">Array</Badge>
### Example 1:
- Input: `points = [[1,3],[-2,2]], k = 1`
- Output: `[[-2,2]]`
- Explanation: The distance between `(1`, `3`) and the origin is sqrt(10). The distance between `(-2`, `2`) and the origin is sqrt(8). Since sqrt(8) < sqrt(10), `(-2`, `2`) is closer to the origin. We only want the closest `k = 1 points` from the origin, so the answer is just [[-2,2]].
### Example 2:
- Input: `points = [[3,3],[5,-1],[-2,4]], k = 2`
- Output: `[[3,3],[-2,4]]`
- Explanation: The answer [[-2,4],[3,3]] would also be accepted.
### Constraints:
- `1 <= k <= points.length <= 10^4`
- `-10^4 <= xi, yi <= 10^4`
## Approach
```mermaid
flowchart TD
S(["kClosest(points, k)"]) --> I["minHeap = []"]
I --> F{"more x, y in points?"}
F -- yes --> D["dist = x**2 + y**2 — squared distance orders the same as sqrt"]
D --> A["minHeap.append([dist, x, y])"]
A --> F
F -- no --> H["heapq.heapify(minHeap), res = [] — O(n) build, smallest dist on top"]
H --> W{"k > 0?"}
W -- yes --> P["dist, x, y = heapq.heappop(minHeap)"]
P --> R["res.append([x, y]), k -= 1"]
R --> W
W -- no --> E(["return res"])
```
## Solution
```py
import heapq
class Solution:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
minHeap = []
for x, y in points:
dist = (x**2) + (y**2)
minHeap.append([dist, x, y])
heapq.heapify(minHeap)
res = []
while k > 0:
dist, x, y = heapq.heappop(minHeap)
res.append([x, y])
k -= 1
return res
```
## Explanation
<YouTube id="rI2EBUEMfTk" title="K Closest Points to Origin - Heap / Priority Queue - Leetcode 973 - Python" />
@@ -0,0 +1,5 @@
---
title: Heap / Priority Queue
description: 'The largest or smallest item is always at the top, and it is removed in logarithmic time.'
hidden: true
---
@@ -0,0 +1,3 @@
import { defineMeta } from "blume";
export default defineMeta({ title: "Heap / Priority Queue", order: 14, icon: "triangle", display: "group" });