Skip to content

Commit 85b7c65

Browse files
authored
Update dynamic_programming.md
1 parent c1cdcb5 commit 85b7c65

File tree

1 file changed

+69
-49
lines changed

1 file changed

+69
-49
lines changed

notes/dynamic_programming.md

Lines changed: 69 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -82,148 +82,168 @@ In the tabulation approach, we systematically fill in the table from the smalles
8282

8383
The choice between recursion and tabulation is often a matter of trade-offs. Recursion is usually simpler and more intuitive, but it can be slower for large inputs due to repeated calculations and function call overhead. Tabulation, on the other hand, can be faster and more efficient because it eliminates the need for recursion, but it can be less intuitive and requires understanding the problem thoroughly to figure out the correct sequence of solving sub-problems.
8484

85-
## Common terms made simple
85+
86+
## Common Terms Made Simple
8687

8788
### Recursion
88-
Recursion is a way to solve problems by using a function that calls itself with a smaller problem. The function keeps breaking the problem into smaller pieces until it can solve the smallest problem. Then, it builds the answer from the smallest problems. Recursion is used for problems that have smaller, repeated problems, like the Fibonacci sequence or the Tower of Hanoi.
89+
90+
Recursion is a problem-solving technique where a function calls itself to solve smaller instances of the same problem. The function continues to break down the problem into more manageable pieces until it reaches the simplest form, known as the base case, which it can solve directly. After reaching the base case, the function then builds up the solution to the original problem by combining the results of the smaller problems. Recursion is particularly useful for problems that can be divided into similar subproblems, such as the Fibonacci sequence or the Tower of Hanoi.
91+
92+
Example: Calculating the factorial of a number using recursion.
8993

9094
```
9195
Factorial(5) = 5 * Factorial(4)
92-
= 5 * (4 * Factorial(3))
93-
= 5 * (4 * (3 * Factorial(2)))
94-
= 5 * (4 * (3 * (2 * Factorial(1))))
95-
= 5 * (4 * (3 * (2 * 1)))
96-
= 120
96+
= 5 * (4 * Factorial(3))
97+
= 5 * (4 * (3 * Factorial(2)))
98+
= 5 * (4 * (3 * (2 * Factorial(1))))
99+
= 5 * (4 * (3 * (2 * 1)))
100+
= 120
97101
```
98102

99-
#### Subset
100-
A subset is a collection of elements from a larger set. Every set is considered a subset of itself, and the empty set is a subset of every set. Subsets are often used in combinatorial mathematics to describe the possible combinations of elements chosen from a larger set.
103+
### Subset
101104

102-
Example:
105+
A subset is a collection of elements that are all drawn from a larger set. Every set is considered to be a subset of itself, and the empty set is a subset of every set. Subsets are fundamental in combinatorial mathematics, where they help describe the different possible groupings of elements that can be selected from a larger set.
103106

104-
```
105-
Set A = {1, 2, 3}
107+
Example:
106108

107-
Subsets of A = { }, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}
109+
Given Set `A = {1, 2, 3}`, the subsets of `A` are:
110+
111+
```
112+
{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}
108113
```
109114

110-
#### Subarray
111-
A subarray is a contiguous portion of an array or a sequence of elements within an array. A subarray is defined by its start and end indices and can be extracted from the original array using these indices.
115+
### Subarray
112116

113-
Example:
117+
A subarray is a contiguous segment of an array. It consists of a sequence of elements that are adjacent to each other within the original array. The start and end indices define the subarray and determine which elements are included. Unlike subsets, the elements in a subarray maintain their order and contiguity from the original array.
114118

115-
```
116-
Array A = [1, 2, 3, 4, 5]
119+
Example:
120+
121+
Given Array `A = [1, 2, 3, 4, 5]`, the subarrays of `A` include:
117122

118-
Subarrays of A: [1], [1, 2], [1, 2, 3], [2], [2, 3], [2, 3, 4], [3], [3, 4], [3, 4, 5], [4], [4, 5], [5]
123+
```
124+
[1], [1, 2], [1, 2, 3], [2], [2, 3], [2, 3, 4], [3], [3, 4], [3, 4, 5], [4], [4, 5], [5]
119125
```
120126

121-
#### Substring
122-
A substring is a contiguous sequence of characters within a string. A substring is defined by its start and end indices and can be extracted from the original string using these indices.
127+
### Substring
123128

124-
Example:
129+
A substring is a contiguous sequence of characters within a string. Like subarrays, substrings maintain the order and contiguity of the characters from the original string. The start and end indices define the substring, and they determine which characters are included.
125130

126-
```
127-
String S = "hello"
131+
Example:
128132

129-
Substrings of S: "h", "he", "hel", "hell", "hello", "e", "el", "ell", "ello", "l", "ll", "llo", "l", "lo", "o"
133+
Given String `S = "hello"`, the substrings of `S` include:
134+
135+
```
136+
"h", "he", "hel", "hell", "hello", "e", "el", "ell", "ello", "l", "ll", "llo", "l", "lo", "o"
130137
```
131138

132-
#### Subsequence
133-
A subsequence is a sequence of elements within a larger sequence (such as a string or an array) that can be extracted by deleting some of the elements in the original sequence. A subsequence doesn't have to be contiguous, unlike a substring or a subarray.
139+
### Subsequence
134140

135-
Example:
141+
A subsequence is a sequence that can be derived from another sequence (such as a string or an array) by deleting some or none of the elements without changing the order of the remaining elements. Unlike substrings and subarrays, a subsequence does not need to be contiguous. This means that elements can be selected from anywhere in the original sequence, as long as their relative order is preserved.
136142

137-
```
138-
String S = "abc"
143+
Example:
144+
145+
Given String `S = "abc"`, the subsequences of `S` include:
139146

140-
Subsequences of S: "", "a", "b", "c", "ab", "ac", "bc", "abc"
147+
```
148+
"", "a", "b", "c", "ab", "ac", "bc", "abc"
141149
```
142150

151+
## List of Problems
152+
143153
### Fibonacci Sequence
144-
The Fibonacci sequence is a series of numbers where a number is the sum of the two preceding ones, starting with 0 and 1. It's a classic example of recursive algorithms and dynamic programming.
154+
155+
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting with 0 and 1. This sequence is a classic example used to demonstrate recursive algorithms and dynamic programming techniques.
145156

146157
* [C++](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/cpp/fibonacci/)
147158
* [Python](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/python/fibonacci/)
148159

149160
### Grid Traveler
150-
Grid Traveler problem entails finding the total number of ways to traverse a grid (m x n) from the top-left to the bottom-right corner, provided you can only move right or down.
161+
162+
The Grid Traveler problem involves finding the total number of ways to traverse an `m x n` grid from the top-left corner to the bottom-right corner, with the constraint that you can only move right or down.
151163

152164
* [C++](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/cpp/grid_traveler)
153165
* [Python](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/python/grid_traveler)
154166

155-
### Climbing stairs
156-
The Climbing Stairs problem involves determining the number of distinct ways to reach the top of a staircase that has 'n' steps, with the condition that you can climb either 1, 2, or 3 steps at a time.
167+
### Climbing Stairs
157168

158-
#### Implementation
169+
The Climbing Stairs problem requires determining the number of distinct ways to reach the top of a staircase with 'n' steps, given that you can climb either 1, 2, or 3 steps at a time.
159170

160171
* [C++](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/cpp/climb_stairs)
161172
* [Python](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/python/climbing_stairs)
162173

163174
### Can Sum
164-
The Can Sum problem involves determining if it's possible to achieve a target sum using any number of elements from a specific list of numbers. Each number from the list can be used multiple times.
165175

166-
#### Implementation
176+
The Can Sum problem involves determining if it is possible to achieve a target sum using any number of elements from a given list of numbers. Each number in the list can be used multiple times.
167177

168178
* [C++](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/cpp/can_sum)
169179
* [Python](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/python/can_sum)
170180

171181
### How Sum
172-
The How Sum problem extends the Can Sum problem by identifying which elements from the list can sum up to the target.
182+
183+
The How Sum problem extends the Can Sum problem by identifying which elements from the list can be combined to sum up to the target value.
173184

174185
* [C++](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/cpp/how_sum)
175186
* [Python](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/python/how_sum)
176187

177188
### Best Sum
189+
178190
The Best Sum problem further extends the How Sum problem by finding the smallest combination of numbers that add up to exactly the target sum.
179191

180192
* [C++](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/cpp/best_sum)
181193
* [Python](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/python/best_sum)
182194

183195
### Can Construct
184-
The Best Sum problem further extends the How Sum problem by finding the smallest combination of numbers that add up to exactly the target sum.
196+
197+
The Can Construct problem involves determining if a target string can be constructed from a given list of substrings.
185198

186199
* [C++](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/cpp/can_construct)
187200
* [Python](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/python/can_construct)
188201

189202
### Count Construct
203+
190204
The Count Construct problem expands on the Can Construct problem by determining the number of ways a target string can be constructed using a list of substrings.
191205

192206
* [C++](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/cpp/count_construct)
193207
* [Python](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/python/count_construct)
194208

195209
### All Constructs
196-
The All Constructs problem is a variation of the Count Construct problem, which identifies all the possible combinations of strings from a list that can be used to form the target string.
210+
211+
The All Constructs problem is a variation of the Count Construct problem, which identifies all the possible combinations of substrings from a list that can be used to form the target string.
197212

198213
* [C++](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/cpp/all_construct)
199214
* [Python](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/python/all_construct)
200215

201216
### Coins
202-
The Coins problem aims to find the minimum number of coins that make a given value, given an infinite supply of each of coin denominations.
217+
218+
The Coins problem aims to find the minimum number of coins needed to make a given value, provided an infinite supply of each coin denomination.
203219

204220
* [C++](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/cpp/coin_change)
205221
* [Python](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/python/coins)
206222

207-
### Longest common subsequence
208-
The Longest Common Subsequence problem involves finding the longest subsequence that two sequences have in common.
223+
### Longest Common Subsequence
224+
225+
The Longest Common Subsequence problem involves finding the longest subsequence that two sequences have in common, where a subsequence is derived by deleting some or no elements without changing the order of the remaining elements.
209226

210227
* [C++](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/cpp/longest_common_subsequence)
211228
* [Python](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/python/longest_common_subsequence)
212229

213-
### Longest increasing subarray
214-
The Longest Increasing Subarray problem involves identifying the longest contiguous subarray in which the elements are strictly increasing.
230+
### Longest Increasing Subarray
231+
232+
The Longest Increasing Subarray problem involves identifying the longest contiguous subarray where the elements are strictly increasing.
215233

216234
* [C++](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/cpp/longest_increasing_subarray)
217235
* [Python](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/python/longest_increasing_subarray)
218236

219237
### Knuth-Morris-Pratt
220-
KMP is a pattern searching algorithm that searches for occurrences of a "word" within a main "text string". It uses preprocessing over the pattern to achieve linear time complexity.
238+
239+
The Knuth-Morris-Pratt (KMP) algorithm is a pattern searching algorithm that looks for occurrences of a "word" within a main "text string" using preprocessing over the pattern to achieve linear time complexity.
221240

222241
* [C++](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/cpp/kmp)
223242
* [Python](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/python/kmp)
224243

225-
### Minimum insertions to form a palindrome
226-
This problem involves finding the minimum number of insertions needed to transform a given string into a palindrome.
244+
### Minimum Insertions to Form a Palindrome
245+
246+
This problem involves finding the minimum number of insertions needed to transform a given string into a palindrome. The goal is to make the string read the same forwards and backwards with the fewest insertions possible.
227247

228248
* [C++](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/cpp/minimum_insertions_for_palindrome)
229249
* [Python](https://github.com/djeada/Algorithms-And-Data-Structures/blob/master/src/dynamic_programming/python/minimum_insertions_for_palindrome)

0 commit comments

Comments
 (0)