Skip to content

Commit 1fe1766

Browse files
committed
finish MIT DP note
1 parent 68ab89d commit 1fe1766

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

algorithm/DP/DP_Note_MIT.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,55 @@ time/sub = O(n)
7777
4. topoorder: increasing substring size
7878
5. DP(0, n)
7979
### Edit distance
80+
Given two string x & y, what's the cheapest possible sequence of character edits (insert, delete, replace) to turn x -> y
81+
1. subprob: edit distance on x [i:] & y [j:] subprobs num: m * n, O(1) each
82+
2. guess: for each x[i], y[j]:
83+
- replace
84+
- delete x[i]
85+
- insert y[j]
86+
3.
87+
```
88+
DP(i, j) = min(
89+
cost of replace x[i] -> y[j] + DP(i + 1, j + 1),
90+
cost of insert y[j] + DP(i, j + 1),
91+
cost of delete x[i] + DP(i + 1, j)
92+
)
93+
```
94+
4. topo graph: matrix(i, j) insert go right, delete go down, replace go right-bottom
95+
5. DP(0, 0), O(mn)
8096
### Knapsack
97+
- list of items (size, value)
98+
- Knapsack size S
99+
1. subprobs: suffix i of items when remain size X
100+
2. guess: is item i in the subset or not (boolean)
101+
3. `DP(i, X) = max(DP(i + 1, X), DP(i + 1, X - si) + vi)`
102+
4. DP(0, S), O(nS)
81103
### Piano/Guitar fingering
104+
- given sequence of n notes, find fingering for each note (single note)
105+
- fingures: 1 ... F
106+
- difficulty measure: d(p, f, q, g) key p -> q, fingure f => g
107+
1. subprobs: how to play notes [i:] when use f for notes[i]
108+
2. guess: finger g for notes[i + 1]
109+
3. `DP(i, f) = min((DP(i + 1, g) + d(i, f, i + 1, g)) for g in 1...F)`
110+
4. toporder: `for i in reversed(range(n)): for f in 1 ... F`
111+
5. min(DP(0, f) for f in 1...F) => O(nF^2)
112+
- If need to combine with string (guitar) => generalize 'finger' to 'finger + string'
113+
- Multiple notes => the past need to remember is 'fingers to notes/null'
82114
### Tetris training
115+
- seq of n pieces
116+
- drop from top
117+
- full rows don't clear
118+
- width is small
119+
- initial empty
120+
- can you survive?
121+
1. subprob: suffix pieces[i:] given board skyline
122+
2. guess: how to play for each piece (4 * width choice)
83123
### Super Mario Bros I
124+
- given level n
125+
- small screen
126+
- configuratoin
127+
- everything on scree c(w, h) (for each pixel)
128+
- Mario's velocity
129+
- score
130+
- time
131+
- screen vs level

0 commit comments

Comments
 (0)