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,64 @@
---
title: '133. Clone Graph'
description: Given a reference of a node in a connected undirected graph
sidebar:
label: 'Clone Graph'
badge: 'Medium'
---
<Badge variant="accent">Hash Table</Badge>
### Example 1:
- Input: `adjList = [[2,4],[1,3],[2,4],[1,3]]`
- Output: `[[2,4],[1,3],[2,4],[1,3]]`
- Explanation: There are `4` nodes in the graph. 1st `node` (val = 1)'s neighbors are 2nd `node` (val = `2`) and 4th `node` (val = `4`). 2nd `node` (val = 2)'s neighbors are 1st `node` (val = `1`) and 3rd `node` (val = `3`). 3rd `node` (val = 3)'s neighbors are 2nd `node` (val = `2`) and 4th `node` (val = `4`). 4th `node` (val = 4)'s neighbors are 1st `node` (val = `1`) and 3rd `node` (val = `3`).
### Example 2:
- Input: `adjList = [[]]`
- Output: `[[]]`
- Explanation: Note that the input contains one empty list. The graph consists of only one `node` with val = `1` and it does not have any neighbors.
### Example 3:
- Input: `adjList = []`
- Output: `[]`
- Explanation: This an empty graph, it does not have any nodes.
### Constraints:
- The number of nodes in the graph is in the range [0, 100].
- `1 <= Node.val <= 100`
- Node.val is unique for each node.
- There are no repeated edges and no self-loops in the graph.
- The Graph is connected and all nodes can be visited starting from the given node.
## Solution
```py
"""
# Definition for a Node.
class Node:
def __init__(self, val = 0, neighbors = None):
self.val = val
self.neighbors = neighbors if neighbors is not None else []
"""
from typing import Optional
class Solution:
def cloneGraph(self, node: Optional["Node"]) -> Optional["Node"]:
oldToNew = {}
def dfs(node):
if node in oldToNew:
return oldToNew[node]
copy = Node(node.val)
oldToNew[node] = copy
for n in node.neighbors:
copy.neighbors.append(dfs(n))
return copy
return dfs(node) if node else None
```
@@ -0,0 +1,51 @@
---
title: '200. Number of Islands'
description: Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands
sidebar:
label: 'Number of Islands'
badge: 'Medium'
---
<Badge variant="accent">Array</Badge>
### Example 1:
- Input: `grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"] ]`
- Output: `1`
### Example 2:
- Input: `grid = [ ["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"] ]`
- Output: `3`
### Constraints:
- `m == grid.length`
- `n == grid[i].length`
- `1 <= m, n <= 300`
- grid[i][j] is '0' or '1'.
## Solution
```py
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
rows, cols = len(grid), len(grid[0])
def dfs(r, c):
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != "1":
return
else:
grid[r][c] = "0"
dfs(r, c + 1) # right
dfs(r + 1, c) # bottom
dfs(r, c - 1) # left
dfs(r - 1, c) # top
num_islands = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == "1":
num_islands += 1
dfs(r, c)
return num_islands
```
@@ -0,0 +1,67 @@
---
title: '417. Pacific Atlantic Water Flow'
description: There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges
sidebar:
label: 'Pacific Atlantic Water Flow'
badge: 'Medium'
---
<Badge variant="accent">Array</Badge>
### Example 1:
- Input: `heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]`
- Output: `[[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]`
- Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below: `[0,4]`: `[0,4]` -> Pacific Ocean `[0,4]` -> Atlantic Ocean `[1,3]`: `[1,3]` -> `[0,3]` -> Pacific Ocean `[1,3]` -> `[1,4]` -> Atlantic Ocean `[1,4]`: `[1,4]` -> `[1,3]` -> `[0,3]` -> Pacific Ocean `[1,4]` -> Atlantic Ocean `[2,2]`: `[2,2]` -> `[1,2]` -> `[0,2]` -> Pacific Ocean `[2,2]` -> `[2,3]` -> `[2,4]` -> Atlantic Ocean `[3,0]`: `[3,0]` -> Pacific Ocean `[3,0]` -> `[4,0]` -> Atlantic Ocean `[3,1]`: `[3,1]` -> `[3,0]` -> Pacific Ocean `[3,1]` -> `[4,1]` -> Atlantic Ocean `[4,0]`: `[4,0]` -> Pacific Ocean `[4,0]` -> Atlantic Ocean Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
### Example 2:
- Input: `heights = [[1]]`
- Output: `[[0,0]]`
- Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.
### Constraints:
- `m == heights.length`
- `n == heights[r].length`
- `1 <= m, n <= 200`
- `0 <= heights[r][c] <= 10^5`
## Solution
```py
class Solution:
def pacificAtlantic(self, heights: list[list[int]]) -> list[list[int]]:
rows, cols = len(heights), len(heights[0])
pac, atl = set(), set()
def dfs(r, c, seen, prevHeight):
if (
(r, c) in seen
or r < 0
or c < 0
or r == rows
or c == cols
or heights[r][c] < prevHeight
):
return
seen.add((r, c))
dfs(r + 1, c, seen, heights[r][c])
dfs(r - 1, c, seen, heights[r][c])
dfs(r, c + 1, seen, heights[r][c])
dfs(r, c - 1, seen, heights[r][c])
for c in range(cols):
dfs(0, c, pac, heights[0][c])
dfs(rows - 1, c, atl, heights[rows - 1][c])
for r in range(rows):
dfs(r, 0, pac, heights[r][0])
dfs(r, cols - 1, atl, heights[r][cols - 1])
res = []
for r in range(rows):
for c in range(cols):
if (r, c) in pac and (r, c) in atl:
res.append([r, c])
return res
```
@@ -0,0 +1,5 @@
---
title: Graph DFS
description: 'Each edge is followed to its end, and a visited set prevents a repeat visit.'
hidden: true
---
@@ -0,0 +1,3 @@
import { defineMeta } from "blume";
export default defineMeta({ title: "Graph DFS", order: 16, icon: "waypoints", display: "group" });