You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: notes/dynamic_programming.md
+69-49Lines changed: 69 additions & 49 deletions
Original file line number
Diff line number
Diff line change
@@ -82,148 +82,168 @@ In the tabulation approach, we systematically fill in the table from the smalles
82
82
83
83
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.
84
84
85
-
## Common terms made simple
85
+
86
+
## Common Terms Made Simple
86
87
87
88
### 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.
89
93
90
94
```
91
95
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
97
101
```
98
102
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
101
104
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.
103
106
104
-
```
105
-
Set A = {1, 2, 3}
107
+
Example:
106
108
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:
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
112
116
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.
114
118
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:
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
123
128
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.
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
134
140
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.
136
142
137
-
```
138
-
String S = "abc"
143
+
Example:
144
+
145
+
Given String `S = "abc"`, the subsequences of `S` include:
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.
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.
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
157
168
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.
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.
165
175
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.
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.
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.
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.
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.
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.
0 commit comments