feat: refactored docs structure

This commit is contained in:
Prad Nukala committed 2026-10-05 13:53:14 -04:00
1 parent d388b3000e
commit 9de25fecff
212 files changed
+641 -4169

No files matched your search

@@ -1,117 +0,0 @@
---
title: '146. LRU Cache'
description: Design a data structure that follows the constraints of a Least Recently Used (LRU) cache
icon: dot
topics:
- { name: "Hash Table", slug: "hash-table" }
- { name: "Linked List", slug: "linked-list" }
- { name: "Design", slug: "design" }
- { name: "Doubly-Linked List", slug: "doubly-linked-list" }
video: "7ABFKPK2hD4"
issue: "https://github.com/prdlk/leetcode/issues/87"
sidebar:
label: 'LRU Cache'
badge: 'Medium'
---
### Example 1:
- Input: ``
- Output: ``
- Explanation: Input ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] `[[2]`, [1, 1], [2, 2], `[1]`, [3, 3], `[2]`, [4, 4], `[1]`, `[3]`, [4]] Output [null, null, null, `1`, null, `-1`, null, `-1`, `3`, 4] Explanation LRUCache lRUCache = new LRUCache(2); lRUCache.put(1, `1`); // cache is {1=1} lRUCache.put(2, `2`); // cache is {1=1, 2=2} lRUCache.get(1); // return `1` lRUCache.put(3, `3`); // LRU `key` was `2`, evicts `key 2`, cache is {1=1, 3=3} lRUCache.get(2); // returns `-1` (not found) lRUCache.put(4, `4`); // LRU `key` was `1`, evicts `key 1`, cache is {4=4, 3=3} lRUCache.get(1); // return `-1` (not found) lRUCache.get(3); // return `3` lRUCache.get(4); // return `4`
### Constraints:
- `1 <= capacity <= 3000`
- `0 <= key <= 10^4`
- `0 <= value <= 10^5`
- At most 2 * 10^5 calls will be made to get and put.
## Approach
```mermaid
flowchart TD
subgraph state["state — dict for O(1) lookup, doubly linked list for O(1) reorder"]
H["head — least recent"] --- N["... nodes ..."] --- T["tail — most recent"]
end
subgraph get["get(key)"]
G1{"key in cache?"} -- no --> G2(["return -1"])
G1 -- yes --> G3["_remove_node then _add_node — move it beside the tail"]
G3 --> G4(["return node.value"])
end
subgraph put["put(key, value)"]
P1{"key in cache?"} -- yes --> P2["update value, _remove_node then _add_node"]
P1 -- no --> P3["new Node, cache[key] = node, _add_node"]
P3 --> P4{"len(cache) > capacity?"}
P4 -- yes --> P5["evict head.next — the least recent — and del cache[lru.key]"]
P4 -- no --> P6(["done"])
P2 --> P6
P5 --> P6
end
state --> get
state --> put
```
## Solution
```py
class Node:
def __init__(self, key=0, value=0):
self.key = key
self.value = value
self.prev = None
self.next = None
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.cache = {}
self.head = Node()
self.tail = Node()
self.head.next = self.tail # pyright: ignore[reportAttributeAccessIssue]
self.tail.prev = self.head # pyright: ignore[reportAttributeAccessIssue]
def _remove_node(self, node):
prev_node = node.prev
next_node = node.next
prev_node.next = next_node
next_node.prev = prev_node
def _add_node(self, node):
# Insert just before the tail (most recent at end)
node.next = self.tail
node.prev = self.tail.prev
self.tail.prev.next = node # pyright: ignore[reportAttributeAccessIssue]
self.tail.prev = node
def get(self, key: int) -> int:
if key not in self.cache:
return -1
node = self.cache[key]
self._remove_node(node)
self._add_node(node)
return node.value
def put(self, key: int, value: int) -> None:
if key in self.cache:
node = self.cache[key]
node.value = value
self._remove_node(node)
self._add_node(node)
else:
new_node = Node(key, value)
self.cache[key] = new_node
self._add_node(new_node)
if len(self.cache) > self.capacity:
lru = self.head.next # Oldest node is next to the head
self._remove_node(lru)
del self.cache[lru.key] # pyright: ignore[reportAttributeAccessIssue]
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
```
@@ -1,76 +0,0 @@
---
title: '2. Add Two Numbers'
description: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list
icon: dot
topics:
- { name: "Linked List", slug: "linked-list" }
- { name: "Math", slug: "math" }
- { name: "Recursion", slug: "recursion" }
video: "wgFPrzTjm7s"
issue: "https://github.com/prdlk/leetcode/issues/88"
sidebar:
label: 'Add Two Numbers'
badge: 'Medium'
---
### Example 1:
- Input: `l1 = [2,4,3], l2 = [5,6,4]`
- Output: `[7,0,8]`
- Explanation: `342 + 465 = 807`.
### Example 2:
- Input: `l1 = [0], l2 = [0]`
- Output: `[0]`
### Example 3:
- Input: `l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]`
- Output: `[8,9,9,9,0,0,0,1]`
### Constraints:
- The number of nodes in each linked list is in the range [1, 100].
- `0 <= Node.val <= 9`
- It is guaranteed that the list represents a number that does not have leading zeros.
## Approach
```mermaid
flowchart TD
S(["addTwoNumbers(l1, l2)"]) --> I["dummy = ListNode(), carry = 0, curr = dummy — dummy.next will be the head"]
I --> W{"l1 or l2 or carry?"}
W -- no --> E(["return dummy.next"])
W -- yes --> A["s = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry — a missing digit counts as 0"]
A --> D["carry, val = divmod(s, 10)"]
D --> N["curr.next = ListNode(val), curr = curr.next"]
N --> V["l1 = l1.next if l1 else None, l2 = l2.next if l2 else None"]
V --> W
```
## Solution
```py
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(
self, l1: Optional[ListNode], l2: Optional[ListNode]
) -> Optional[ListNode]:
dummy = ListNode()
carry = 0
curr = dummy
while l1 or l2 or carry:
s = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry
carry, val = divmod(s, 10)
curr.next = ListNode(val)
curr = curr.next
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
return dummy.next
```
@@ -1,5 +0,0 @@
---
title: Hybrid Structures
description: 'Two structures are combined, such as a hash map and a linked list, to get the best properties of each.'
hidden: true
---
@@ -1,3 +0,0 @@
import { defineMeta } from "blume";
export default defineMeta({ title: "Hybrid Structures", order: 9, icon: "combine", display: "page" });
@@ -1,76 +0,0 @@
---
title: '141. Linked List Cycle'
description: Given head, the head of a linked list, determine if the linked list has a cycle in it
icon: dot
topics:
- { name: "Hash Table", slug: "hash-table" }
- { name: "Linked List", slug: "linked-list" }
- { name: "Two Pointers", slug: "two-pointers" }
- { name: "Floyd's Cycle Finding Algorithm", slug: "floyds-cycle-finding-algorithm" }
video: "gBTe7lFR3vc"
issue: "https://github.com/prdlk/leetcode/issues/79"
sidebar:
label: 'Linked List Cycle'
badge: 'Easy'
---
::::warning
Can you solve it using O(1) (i.e. constant) memory?
::::
### Example 1:
- Input: `head = [3,2,0,-4], pos = 1`
- Output: `true`
- Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
### Example 2:
- Input: `head = [1,2], pos = 0`
- Output: `true`
- Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
### Example 3:
- Input: `head = [1], pos = -1`
- Output: `false`
- Explanation: There is no cycle in the linked list.
### Constraints:
- The number of the nodes in the list is in the range [0, 10^4].
- `-10^5 <= Node.val <= 10^5`
- pos is -1 or a valid index in the linked-list.
## Approach
```mermaid
flowchart TD
S(["hasCycle(head)"]) --> I["fast = slow = head"]
I --> W{"fast and fast.next?"}
W -- no --> E(["return False — ran off the end, no cycle"])
W -- yes --> A["fast = fast.next.next — two steps"]
A --> B["slow = slow.next — one step"]
B --> Q{"fast is slow?"}
Q -- yes --> R(["return True — the gap closed, so it loops"])
Q -- no --> W
```
## Solution
```py
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
fast = slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast is slow:
return True
return False
```
@@ -1,67 +0,0 @@
---
title: '206. Reverse Linked List'
description: Given the head of a singly linked list, reverse the list, and return the reversed list
icon: dot
topics:
- { name: "Linked List", slug: "linked-list" }
- { name: "Recursion", slug: "recursion" }
video: "G0_I-ZF0S38"
issue: "https://github.com/prdlk/leetcode/issues/77"
sidebar:
label: 'Reverse Linked List'
badge: 'Easy'
---
::::warning
A linked list can be reversed either iteratively or recursively. Could you implement both?
::::
### Example 1:
- Input: `head = [1,2,3,4,5]`
- Output: `[5,4,3,2,1]`
### Example 2:
- Input: `head = [1,2]`
- Output: `[2,1]`
### Example 3:
- Input: `head = []`
- Output: `[]`
### Constraints:
- The number of nodes in the list is the range [0, 5000].
- `-5000 <= Node.val <= 5000`
## Approach
```mermaid
flowchart TD
S(["reverseList(head)"]) --> I["prev = None, curr = head"]
I --> W{"curr?"}
W -- no --> E(["return prev — the old tail is the new head"])
W -- yes --> A["next_ = curr.next — save it before overwriting"]
A --> B["curr.next = prev — flip the link"]
B --> C["prev = curr; curr = next_ — step forward"]
C --> W
```
## Solution
```py
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = None
curr = head
while curr:
next_ = curr.next
curr.next = prev
prev = curr
curr = next_
return prev
```
@@ -1,87 +0,0 @@
---
title: '21. Merge Two Sorted Lists'
description: You are given the heads of two sorted linked lists list1 and list2
icon: dot
topics:
- { name: "Linked List", slug: "linked-list" }
- { name: "Recursion", slug: "recursion" }
video: "XIdigk956u0"
issue: "https://github.com/prdlk/leetcode/issues/78"
sidebar:
label: 'Merge Two Sorted Lists'
badge: 'Easy'
---
### Example 1:
- Input: `list1 = [1,2,4], list2 = [1,3,4]`
- Output: `[1,1,2,3,4,4]`
### Example 2:
- Input: `list1 = [], list2 = []`
- Output: `[]`
### Example 3:
- Input: `list1 = [], list2 = [0]`
- Output: `[0]`
### Constraints:
- The number of nodes in both lists is in the range [0, 50].
- `-100 <= Node.val <= 100`
- Both list1 and list2 are sorted in non-decreasing order.
## Approach
```mermaid
flowchart TD
S(["mergeTwoLists(list1, list2)"]) --> G1{"list1 empty?"}
G1 -- yes --> X1(["return list2"])
G1 -- no --> G2{"list2 empty?"}
G2 -- yes --> X2(["return list1"])
G2 -- no --> H["head = the smaller first node; advance that list"]
H --> C["current = head"]
C --> W{"list1 and list2 both non-empty?"}
W -- yes --> P["current.next = the smaller node; advance that list; current = current.next"]
P --> W
W -- no --> T["current.next = list1 or list2 — append the leftover tail"]
T --> E(["return head"])
```
## Solution
```py
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(
self, list1: Optional[ListNode], list2: Optional[ListNode]
) -> Optional[ListNode]:
if not list1:
return list2
if not list2:
return list1
if list1.val < list2.val:
head = list1
list1 = list1.next
else:
head = list2
list2 = list2.next
current = head
while list1 and list2:
if list1.val < list2.val:
current.next = list1
list1 = list1.next
else:
current.next = list2
list2 = list2.next
current = current.next
current.next = list1 or list2
return head
```
@@ -1,5 +0,0 @@
---
title: Linked List
description: 'Each node points to the next node, and the pointers give the path through the data.'
hidden: true
---
@@ -1,3 +0,0 @@
import { defineMeta } from "blume";
export default defineMeta({ title: "Linked List", order: 8, icon: "link", display: "page" });
@@ -1,58 +0,0 @@
---
title: '48. Rotate Image'
description: You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise)
icon: dot
topics:
- { name: "Array", slug: "array" }
- { name: "Math", slug: "math" }
- { name: "Matrix", slug: "matrix" }
video: "fMSJSS7eO1w"
issue: "https://github.com/prdlk/leetcode/issues/91"
sidebar:
label: 'Rotate Image'
badge: 'Medium'
---
### Example 1:
- Input: `matrix = [[1,2,3],[4,5,6],[7,8,9]]`
- Output: `[[7,4,1],[8,5,2],[9,6,3]]`
### Example 2:
- Input: `matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]`
- Output: `[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]`
### Constraints:
- `n == matrix.length == matrix[i].length`
- `1 <= n <= 20`
- `-1000 <= matrix[i][j] <= 1000`
## Approach
```mermaid
flowchart TD
S(["rotate(matrix)"]) --> N["n = len(matrix)"]
N --> F{"more r in range(n)?"}
F -- yes --> G{"more c in range(r + 1, n)?"}
G -- yes --> T["swap matrix[r][c] and matrix[c][r] — transpose across the diagonal"]
T --> G
G -- no --> F
F -- no --> H{"more r in range(n)?"}
H -- yes --> R["matrix[r].reverse() — transpose then mirror is a clockwise turn"]
R --> H
H -- no --> E(["return None — rotated in place"])
```
## Solution
```py
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
n = len(matrix)
for r in range(n):
for c in range(r + 1, n):
matrix[r][c], matrix[c][r] = matrix[c][r], matrix[r][c]
for r in range(n):
matrix[r].reverse()
```
@@ -1,82 +0,0 @@
---
title: '54. Spiral Matrix'
description: Given an m x n matrix, return all elements of the matrix in spiral order
icon: dot
topics:
- { name: "Array", slug: "array" }
- { name: "Matrix", slug: "matrix" }
- { name: "Simulation", slug: "simulation" }
video: "BJnMZNwUk1M"
issue: "https://github.com/prdlk/leetcode/issues/92"
sidebar:
label: 'Spiral Matrix'
badge: 'Medium'
---
### Example 1:
- Input: `matrix = [[1,2,3],[4,5,6],[7,8,9]]`
- Output: `[1,2,3,6,9,8,7,4,5]`
### Example 2:
- Input: `matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]`
- Output: `[1,2,3,4,8,12,11,10,9,5,6,7]`
### Constraints:
- `m == matrix.length`
- `n == matrix[i].length`
- `1 <= m, n <= 10`
- `-100 <= matrix[i][j] <= 100`
## Approach
```mermaid
flowchart TD
S(["spiralOrder(matrix)"]) --> I["res = [], top = 0, bottom = len(matrix) - 1, left = 0, right = len(matrix[0]) - 1"]
I --> W{"top <= bottom and left <= right?"}
W -- no --> E(["return res"])
W -- yes --> T["append matrix[top][c] for c in left..right, then top += 1 — top row, shrink from above"]
T --> R["append matrix[r][right] for r in top..bottom, then right -= 1 — right column"]
R --> B{"top <= bottom?"}
B -- yes --> C["append matrix[bottom][c] for c in right down to left, then bottom -= 1 — bottom row"]
C --> L{"left <= right?"}
B -- no --> L
L -- yes --> D["append matrix[r][left] for r in bottom down to top, then left += 1 — left column"]
D --> W
L -- no --> W
```
## Solution
```py
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
res = []
top, bottom = 0, len(matrix) - 1
left, right = 0, len(matrix[0]) - 1
while top <= bottom and left <= right:
# 1. Top Row
for c in range(left, right + 1):
res.append(matrix[top][c])
top += 1
# 2. Right Column
for r in range(top, bottom + 1):
res.append(matrix[r][right])
right -= 1
# 3. Bottom Row
if top <= bottom:
for c in range(right, left - 1, -1):
res.append(matrix[bottom][c])
bottom -= 1
# 4. Left Column
if left <= right:
for r in range(bottom, top - 1, -1):
res.append(matrix[r][left])
left += 1
return res
```
@@ -1,101 +0,0 @@
---
title: '73. Set Matrix Zeroes'
description: Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's
icon: dot
topics:
- { name: "Array", slug: "array" }
- { name: "Hash Table", slug: "hash-table" }
- { name: "Matrix", slug: "matrix" }
video: "T41rL0L3Pnw"
issue: "https://github.com/prdlk/leetcode/issues/93"
sidebar:
label: 'Set Matrix Zeroes'
badge: 'Medium'
---
::::warning
You must do it in place.
::::
### Example 1:
- Input: `matrix = [[1,1,1],[1,0,1],[1,1,1]]`
- Output: `[[1,0,1],[0,0,0],[1,0,1]]`
### Example 2:
- Input: `matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]`
- Output: `[[0,0,0,0],[0,4,5,0],[0,3,1,0]]`
### Constraints:
- `m == matrix.length`
- `n == matrix[0].length`
- `1 <= m, n <= 200`
- `-2^31 <= matrix[i][j] <= 2^31 - 1`
- A straightforward solution using O(mn) space is probably a bad idea.
- A simple improvement uses O(m + n) space, but still not the best solution.
- Could you devise a constant space solution?
## Approach
```mermaid
flowchart TD
S(["setZeroes(matrix)"]) --> I["m = len(matrix), n = len(matrix[0])"]
I --> Z["row0_zero = any(matrix[0][c] == 0), col0_zero = any(matrix[r][0] == 0) — remember before they become markers"]
Z --> F{"more r in range(1, m)?"}
F -- yes --> G{"more c in range(1, n)?"}
G -- yes --> Q{"matrix[r][c] == 0?"}
Q -- yes --> M["matrix[r][0] = 0, matrix[0][c] = 0 — row 0 and column 0 are the marker lists"]
M --> G
Q -- no --> G
G -- no --> F
F -- no --> H{"more r in range(1, m)?"}
H -- yes --> J{"more c in range(1, n)?"}
J -- yes --> K{"matrix[r][0] == 0 or matrix[0][c] == 0?"}
K -- yes --> W["matrix[r][c] = 0"]
W --> J
K -- no --> J
J -- no --> H
H -- no --> A{"row0_zero?"}
A -- yes --> B["matrix[0][c] = 0 for c in range(n)"]
B --> C{"col0_zero?"}
A -- no --> C
C -- yes --> D["matrix[r][0] = 0 for r in range(m)"]
D --> E(["return None — zeroed in place"])
C -- no --> E
```
## Solution
```py
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
m, n = len(matrix), len(matrix[0])
# 1. remember if row 0 or column 0 have their own zeros
row0_zero = any(matrix[0][c] == 0 for c in range(n))
col0_zero = any(matrix[r][0] == 0 for r in range(m))
# 2. use row 0 and column 0 as marker lists
for r in range(1, m):
for c in range(1, n):
if matrix[r][c] == 0:
matrix[r][0] = 0
matrix[0][c] = 0
# 3. zero the inner cells from the markers
for r in range(1, m):
for c in range(1, n):
if matrix[r][0] == 0 or matrix[0][c] == 0:
matrix[r][c] = 0
# 4. handle row 0 and column 0 last
if row0_zero:
for c in range(n):
matrix[0][c] = 0
if col0_zero:
for r in range(m):
matrix[r][0] = 0
```
@@ -1,5 +0,0 @@
---
title: Matrix Index Math
description: 'A two-dimensional grid is read as a flat array through a formula, and the neighbors are found by arithmetic.'
hidden: true
---
@@ -1,3 +0,0 @@
import { defineMeta } from "blume";
export default defineMeta({ title: "Matrix Index Math", order: 10, icon: "grid-3x3", display: "page" });
@@ -1,3 +0,0 @@
import { defineMeta } from "blume";
export default defineMeta({ title: "2. Nodal & Grid", order: 2 });