Skip to content

Commit 820fc4a

Browse files
authored
fix: typos and inconsistencies in intro-to-dp (#1381)
1 parent 7a9c1c9 commit 820fc4a

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/dynamic_programming/intro-to-dp.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Our recursive function currently solves fibonacci in exponential time. This mean
2525
2626
To increase the speed, we recognize that the number of subproblems is only $O(n)$. That is, in order to calculate $f(n)$ we only need to know $f(n-1),f(n-2), \dots ,f(0)$. Therefore, instead of recalculating these subproblems, we solve them once and then save the result in a lookup table. Subsequent calls will use this lookup table and immediately return a result, thus eliminating exponential work!
2727
28-
Each recursive call will check against a lookup table to see if the value has been calculated. This is done in $O(1)$ time. If we have previously calcuated it, return the result, otherwise, we calculate the function normally. The overall runtime is $O(n)$! This is an enormous improvement over our previous exponential time algorithm!
28+
Each recursive call will check against a lookup table to see if the value has been calculated. This is done in $O(1)$ time. If we have previously calcuated it, return the result, otherwise, we calculate the function normally. The overall runtime is $O(n)$. This is an enormous improvement over our previous exponential time algorithm!
2929
3030
```cpp
3131
const int MAXN = 100;
@@ -44,7 +44,7 @@ int f(int n) {
4444

4545
With our new memoized recursive function, $f(29)$, which used to result in *over 1 million calls*, now results in *only 57* calls, nearly *20,000 times* fewer function calls! Ironically, we are now limited by our data type. $f(46)$ is the last fibonacci number that can fit into a signed 32-bit integer.
4646

47-
Typically, we try to save states in arrays,if possible, since the lookup time is $O(1)$ with minimal overhead. However, more generically, we can save states anyway we like. Other examples include maps (binary search trees) or unordered_maps (hash tables).
47+
Typically, we try to save states in arrays, if possible, since the lookup time is $O(1)$ with minimal overhead. However, more generically, we can save states any way we like. Other examples include binary search trees (`map` in C++) or hash tables (`unordered_map` in C++).
4848

4949
An example of this might be:
5050

@@ -86,7 +86,7 @@ This approach is called top-down, as we can call the function with a query value
8686
## Bottom-up Dynamic Programming
8787

8888
Until now you've only seen top-down dynamic programming with memoization. However, we can also solve problems with bottom-up dynamic programming.
89-
Bottom up is exactly the opposite of top-down, you start at the bottom (base cases of the recursion), and extend it to more and more values.
89+
Bottom-up is exactly the opposite of top-down, you start at the bottom (base cases of the recursion), and extend it to more and more values.
9090

9191
To create a bottom-up approach for fibonacci numbers, we initilize the base cases in an array. Then, we simply use the recursive definition on array:
9292

@@ -107,7 +107,7 @@ Of course, as written, this is a bit silly for two reasons:
107107
Firstly, we do repeated work if we call the function more than once.
108108
Secondly, we only need to use the two previous values to calculate the current element. Therefore, we can reduce our memory from $O(n)$ to $O(1)$.
109109
110-
An example of a bottom up dynamic programming solution for fibonacci which uses $O(1)$ might be:
110+
An example of a bottom-up dynamic programming solution for fibonacci which uses $O(1)$ might be:
111111
112112
```cpp
113113
const int MAX_SAVE = 3;
@@ -123,19 +123,19 @@ int f(int n) {
123123
}
124124
```
125125

126-
Note that we've changed the constant from `MAXN` TO `MAX_SAVE`. This is because the total number of elements we need to have access to is only 3. It no longer scales with the size of input and is, by definition, $O(1)$ memory. Additionally, we use a common trick (using the modulo operator) only maintaining the values we need.
126+
Note that we've changed the constant from `MAXN` TO `MAX_SAVE`. This is because the total number of elements we need to access is only 3. It no longer scales with the size of input and is, by definition, $O(1)$ memory. Additionally, we use a common trick (using the modulo operator) only maintaining the values we need.
127127

128-
That's it. That's the basics of dynamic programming: Don't repeat work you've done before.
128+
That's it. That's the basics of dynamic programming: Don't repeat the work you've done before.
129129

130130
One of the tricks to getting better at dynamic programming is to study some of the classic examples.
131131

132132
## Classic Dynamic Programming Problems
133133
| Name | Description/Example |
134134
| ---------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
135-
| 0-1 knapsack | Given $W$, $N$, and $N$ items with weights $w_i$ and values $v_i$, what is the maximum $\sum_{i=1}^{k} v_i$ for each subset of items of size $k$ ($1 \le k \le N$) while ensuring $\sum_{i=1}^{k} w_i \le W$? |
135+
| 0-1 Knapsack | Given $W$, $N$, and $N$ items with weights $w_i$ and values $v_i$, what is the maximum $\sum_{i=1}^{k} v_i$ for each subset of items of size $k$ ($1 \le k \le N$) while ensuring $\sum_{i=1}^{k} w_i \le W$? |
136136
| Subset Sum | Given $N$ integers and $T$, determine whether there exists a subset of the given set whose elements sum up to the $T$. |
137-
| Longest Increasing Subsequence | You are given an array containing $N$ integers. Your task is to determine the LIS in the array, i.e., a subsequence where every element is larger than the previous one. |
138-
| Counting all possible paths in a matrix. | Given $N$ and $M$, count all possible distinct paths from $(1,1)$ to $(N, M)$, where each step is either from $(i,j)$ to $(i+1,j)$ or $(i,j+1)$. |
137+
| Longest Increasing Subsequence (LIS) | You are given an array containing $N$ integers. Your task is to determine the LIS in the array, i.e., a subsequence where every element is larger than the previous one. |
138+
| Counting Paths in a 2D Array | Given $N$ and $M$, count all possible distinct paths from $(1,1)$ to $(N, M)$, where each step is either from $(i,j)$ to $(i+1,j)$ or $(i,j+1)$. |
139139
| Longest Common Subsequence | You are given strings $s$ and $t$. Find the length of the longest string that is a subsequence of both $s$ and $t$. |
140140
| Longest Path in a Directed Acyclic Graph (DAG) | Finding the longest path in Directed Acyclic Graph (DAG). |
141141
| Longest Palindromic Subsequence | Finding the Longest Palindromic Subsequence (LPS) of a given string. |
@@ -159,7 +159,7 @@ Of course, the most important trick is to practice.
159159
* [LeetCode - 221. Maximal Square](https://leetcode.com/problems/maximal-square/description/)
160160
* [LeetCode - 1039. Minimum Score Triangulation of Polygon](https://leetcode.com/problems/minimum-score-triangulation-of-polygon/description/)
161161

162-
## Dp Contests
162+
## DP Contests
163163
* [Atcoder - Educational DP Contest](https://atcoder.jp/contests/dp/tasks)
164164
* [CSES - Dynamic Programming](https://cses.fi/problemset/list/)
165165

0 commit comments

Comments
 (0)