Skip to content

Commit 14197c5

Browse files
authored
GH-53: Dynamic Programming (Intermediate) in C++ (#55)
1 parent b21ab53 commit 14197c5

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

concepts/cpp/dp_intermediate.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ There are two uses for dynamic programming:
1616

1717
## 💢 Problem: Longest Common Subsequences
1818

19-
LCS Problem Statement: Given two sequences, find the length of longest subsequence present in both of them.
19+
**Problem Statement**: Given two sequences, find the length of longest subsequence present in both of them.
2020

2121
* A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. For example, “abc”, “abg”, “bdf”, “aeg”, ‘”acefg”, .. etc are subsequences of “abcdefg”.
2222

@@ -140,9 +140,81 @@ GXTXAYB</pre>
140140
</tbody>
141141
</table>
142142
143+
## 💢 Problem: Longest Palindromic Subsequence (LPS)
144+
145+
**Problem Statement**: Given a sequence, find the length of the longest palindromic subsequence in it.
146+
147+
### Examples
148+
149+
* LPS of input sequence "bbbab" is "bbbb" of length 4.
150+
* LPS of input sequence "cbbd" is "bb" of length 2.
151+
152+
### Algorithms
153+
154+
Use dynamic programming with Memoization
155+
156+
```cpp
157+
// Algorithm: Longest Palindromic Subsequence
158+
// Input : A sequence
159+
// Output : Length of longest palindromic subsequence
160+
161+
string s
162+
int dp[N][N];
163+
if (s[i] == s[j]){
164+
dp[i][j] = 2 + dp[i-1][j-1]
165+
} else {
166+
dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
167+
}
168+
```
169+
170+
**Complexity**
171+
172+
* Time: $O(N^2)$
173+
* Space: $O(N^2)$
174+
175+
### Implementation
176+
177+
```cpp
178+
class Solution {
179+
public:
180+
int longestPalindromeSubseq(string s) {
181+
int n = s.size();
182+
int dp[n][n];
183+
for(int i=0; i<n; i++){
184+
dp[i][i] = 1;
185+
}
186+
for(int i=n-2; i>=0; i--){
187+
for(int j=i+1; j<n; j++){
188+
if(j - i == 1) {
189+
dp[i][j] = s[i] == s[j] ? 2 : 1;
190+
} else {
191+
if(s[i] == s[j]){
192+
dp[i][j] = 2 + dp[i+1][j-1];
193+
} else {
194+
dp[i][j] = max(dp[i][j-1], dp[i+1][j]);
195+
}
196+
}
197+
}
198+
}
199+
return dp[0][n-1];
200+
}
201+
};
202+
```
203+
204+
### 🌟 Public Related Problems
205+
206+
* [Leetcode 516. Longest Palindromic Subsequence (medium)](https://leetcode.com/problems/longest-palindromic-subsequence/description/)
207+
143208
## 🔗 Further Reading
144209
210+
Longest Common Subsequence
211+
145212
* [Longest common subsequence problem](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem), wikipedia
146213
* ▶️ [Dynamic Programming | Set 4 (Longest Common Subsequence) | GeeksforGeeks](https://www.youtube.com/watch?v=HgUOWB0StNE&ab_channel=GeeksforGeeks), GeeksforGeeks, 2017
147214
* [Longest Common Subsequence | DP-4](https://www.geeksforgeeks.org/longest-common-subsequence-dp-4/), GeeksforGeeks, 2022
148215
* ▶️ [Longest Common Subsequence (Dynamic Programming)](https://www.youtube.com/watch?v=Qf5R-uYQRPk&ab_channel=CSDojo), CS Dojo, 2016
216+
217+
Longest Palindromic Subsequence
218+
219+
* [Longest Palindromic Subsequence | DP-12](https://www.geeksforgeeks.org/longest-palindromic-subsequence-dp-12/), GeeksforGeeks, 2022
220+
* ▶️ [DP - 9: Longest Palindrome Subsequence](https://www.youtube.com/watch?v=_AcULHRds3I&ab_channel=CodingSimplified), Coding Simplified, 2020

problems/anhv_practice.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ anhv;codeforces1760F;2022-11-24;practice;fail
55
anhv;codeforces1527A;2022-11-25;practice;fail
66
anhv;codeforces1527A;2022-11-25;practice;success
77
anhv;codeforces1398C;2022-11-29;practice;fail
8-
anhv;codeforces1398C;2022-11-29;practice;editorial-success
8+
anhv;codeforces1398C;2022-11-29;practice;editorial-success
9+
anhv;leetcode516;2022-12-01;practice;success

tools/rules.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
search: "## 🔗 Further Reading\n"
1111
- name: Section "Further Reading" should contains at least 3 items
1212
type: regex
13-
search: "## 🔗 Further Reading\n\n(\\*.*\n){3,}"
13+
search: "## 🔗 Further Reading\n\n(.*\n)*(\\*.*\n){3,}"
1414
- name: Section "Further Reading" should contains at least 1 video
1515
type: regex
16-
search: "## 🔗 Further Reading\n\n\\*(.*\n.*)*.*▶️"
16+
search: "## 🔗 Further Reading\n\n(.*\n)*\\*(.*\n.*)*.*▶️"

0 commit comments

Comments
 (0)