Skip to content

Commit d0173b1

Browse files
committed
add leetcode posts
1 parent ac341f9 commit d0173b1

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

posts/250325.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: 'leetcode @ class'
3+
tags: 'journal'
4+
date: 'Mar 25, 2025'
5+
---
6+
7+
in between listening to the lectures i was leetcoding. my deep learning class taught about cnn. cnn is a type of neural network architecture that can learn spatial patterns through heirarchical feature extractions, mimicking human visual processing.
8+
9+
core components:
10+
11+
- convolutional layer: apply filters to detect features (edge -> shape -> object)
12+
- filters/kernels: matrices (3x3 or 5x5) that slide across images
13+
- output dim: (input size - filter size) / stride + 1.
14+
- padding. add border elements to preserve spatial dimensions
15+
- optimal padding: (filter size - 1) / 2
16+
17+
example:
18+
19+
Padding = (filter size - 1) / 2
20+
For 3x3 filter: Padding = (3 - 1) / 2 = 1
21+
22+
Input: 5x5 image
23+
Add 1-pixel border around image → 7x7
24+
Filter: 3x3
25+
Output size = (7 - 3) / 1 + 1 = 5x5
26+
27+
this helps maintain output size to match input size.
28+
29+
what do 1x1 filters do?
30+
31+
- dimensionality reduction (reduce channels but preserve spatial dim)
32+
- computational efficiency
33+
34+
GoogleNet/inception networks popularized this
35+
36+
- reduce parameters
37+
- control network depth

posts/260325.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: 'leetcode everywhere'
3+
tags: 'journal'
4+
date: 'Mar 26, 2025'
5+
---
6+
7+
took meeting with USF professor. i shared about my interview and my professor mentioned DP is fun. learning about DP and seeing how elegant the solutions are can be fun, but thinking about how i only have 25 min to solve a DP question is highly stress-inducing. my stomach squeezes into a knot everytime it think about it. i have not solved enough questinos to have a clear thought process. my pattern matching algorithm has not seen enough data. went to capitol one cafe to do some more leetcode. got my hair cut because the edges 'have been poking on my eye. the rest of the night was more interview prep. my life has been consumed by this interview.

posts/270325.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
title: 'dynamic programming 101'
3+
tags: 'journal'
4+
date: 'Mar 27, 2025'
5+
---
6+
7+
took the time to learn the framework for dp. below are some notes from the leetcode explore for DP.
8+
9+
characteristics of dp:
10+
11+
- break down into **overlapping** subproblems
12+
- an optimal solution can be formed
13+
14+
what dp is not:
15+
16+
- **greedy** problems have optimal substructure, but not overlapping subproblems
17+
- **divide and conquer** breaks a problem into subprobelms, but they are not overlapping
18+
19+
why it helps?
20+
21+
- it improves time complexity compared to brute force
22+
- ex: fib(n) has exponential time complexity for brute force, while linear with DP, as it reuses results of subproblems raather than recalculating results for previously seen subproblems
23+
24+
top-down vs bottom up
25+
26+
- top down (tabulation)
27+
28+
- **iteration** and starts with a basecase
29+
- order matters
30+
- usually faster, no recursion overhead
31+
32+
```py
33+
# fibonacci
34+
F = [0] * (n+1)
35+
F[0] = 1
36+
F[1] = 1
37+
for i in range(2, n):
38+
F[i] = F[i-1] + F[i-2]
39+
```
40+
41+
- bottom-up (memoization)
42+
43+
- **recursion** and made efficient with memoization
44+
- easier to write, order does not matter
45+
- slower
46+
47+
```py
48+
memo = {}
49+
def f(i):
50+
if i < 2:
51+
return i
52+
53+
if i not in memo:
54+
memo[i] = f(i-1) + f(i-2)
55+
56+
return memo[i]
57+
```
58+
59+
when to use it?
60+
61+
1. problem asks for optimum value (max/min) of something
62+
- min cost of doing ... , how many ways are there to ... , longest possible ...
63+
- not enough by itself, could be greedy
64+
2. future "decisions" depends on earlier decisions
65+
- ex: house robber
66+
- nums = [2,7,9,3,1]
67+
- greedy solution is to rob 7, but you miss out on 9 (your early decision affects future decisions)
68+
- ex: longest increasing subsequence
69+
- nums = [1,2,6,3,5]
70+
- important decision is choosing 6 or not, this affects the future (whether you can take 3 and 5)
71+
72+
framework
73+
74+
1. a function that computes answer to problem for every given state
75+
- ex: climbing stairs, we have dp(i) which returns number of ways to climb ith step.
76+
2. a recurrence relation to transition between states
77+
- to climb the 10th stair, we need to climb from the 8th or 9th
78+
- so # ways to climb 10th stair is # ways to climb 8th + # ways to climb 9th stair
79+
- dp(i) = dp(i-1) + dp(i-2)
80+
- finding this is the most difficult part
81+
3. base cases (to prevent it going infinitely)
82+
- ask yourself: what state can you find answer without using DP?
83+
- there is one way to climb first stair, and two ways to climb two stairs,
84+
- base case = dp(1) = 1 and dp(2) = 2
85+
86+
recurrence -> O(2^n)
87+
88+
```py
89+
def climbStairs(self, n: int) -> int:
90+
def dp(i):
91+
"""A function that returns the answer to the problem for a given state."""
92+
# Base cases: when i is 3 there are i ways to reach the ith stair.
93+
if i <= 2:
94+
return i
95+
96+
# If i is not a base case, then use the recurrence relation.
97+
return dp(i - 1) + dp(i - 2)
98+
99+
return dp(n)
100+
```
101+
102+
add memoization -> O(n)
103+
104+
```py
105+
def climbStairs(self, n: int) -> int:
106+
def dp(i):
107+
if i <= 2:
108+
return i
109+
if i not in memo:
110+
# Instead of just returning dp(i - 1) + dp(i - 2), calculate it once and then
111+
# store the result inside a hashmap to refer to in the future.
112+
memo[i] = dp(i - 1) + dp(i - 2)
113+
114+
return memo[i]
115+
116+
memo = {}
117+
return dp(n)
118+
```
119+
120+
bottom up approach
121+
122+
```py
123+
def climbStairs(self, n: int) -> int:
124+
if n == 1:
125+
return 1
126+
127+
# An array that represents the answer to the problem for a given state
128+
dp = [0] * (n + 1)
129+
130+
# base case
131+
dp[1] = 1
132+
dp[2] = 2
133+
134+
for i in range(3, n + 1):
135+
dp[i] = dp[i - 1] + dp[i - 2] # Recurrence relation
136+
137+
return dp[n]
138+
```

0 commit comments

Comments
 (0)