Skip to content

Commit 223ae23

Browse files
committed
Day-55 Dynamic programming problems 2
1 parent c538b24 commit 223ae23

File tree

3 files changed

+151
-3
lines changed

3 files changed

+151
-3
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
| Current Status| Stats |
66
| :------------: | :----------: |
7-
| Total Problems | 75 |
8-
| Current Streak | 54 |
9-
| Longest Streak | 54 ( August 17, 2015 - October 9, 2015 ) |
7+
| Total Problems | 77 |
8+
| Current Streak | 55 |
9+
| Longest Streak | 55 ( August 17, 2015 - October 10, 2015 ) |
1010

1111
</center>
1212

@@ -79,6 +79,11 @@ Include contains single header implementation of data structures and some algori
7979
| Problem 6: Implement a method to perform basic string compression. Example string **aabcccccaaa** should be compressed to **a2b1c5a3**, however if compressed string is bigger than original string, return original string| [1-6-string-compression.cpp](cracking_the_coding_interview_problems/1-6-string-compression.cpp)|
8080
| Problem 7: Rotate the matrix clockwise( & anticlockwise) by 90 degrees| [1-7-matrix-rotation.cpp](cracking_the_coding_interview_problems/1-7-matrix-rotation.cpp)|
8181

82+
#Dynamic Programming Problems
83+
| Problem | Solution |
84+
| :------------ | :----------: |
85+
| N<sup>th</th> Fibonacci term using different memoization techniques | [fibonacci.cpp](dynamic_programming_problems/fibonacci.cpp)|
86+
| Longest Common Subsequence Problem | [lcs.cpp](dynamic_programming_problems/lcs.cpp) |
8287

8388
### Tree Problems
8489
| Problem | Solution |
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Calculating Fibonacci series term of index N using DP memoization.
3+
*/
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
//bottom up
9+
10+
int fib1(int n) {
11+
std::vector<int> fib(n, 0);
12+
if ( n == 0 || n == 1 ) {
13+
return 1;
14+
}
15+
fib[0] = 1;
16+
fib[1] = 1;
17+
for ( int i = 2; i < n; ++i ) {
18+
fib[i] = fib[i-1] + fib[i-2];
19+
}
20+
return fib[n-1];
21+
}
22+
23+
//top down
24+
25+
std::vector<int> fib(1000, 0);
26+
int fib2( int n ) {
27+
if ( n == 0 ) {
28+
return 0;
29+
}
30+
if ( n == 1 || n == 2 ) {
31+
return 1;
32+
}
33+
if ( fib[n] != 0 ) {
34+
return fib[n];
35+
}
36+
fib[n] = fib2(n-1) + fib2(n-2);
37+
return fib[n];
38+
}
39+
40+
//leverage the fact we are just using last 2 term
41+
int fib3( int n ) {
42+
int a = 0;
43+
int b = 1;
44+
for ( int i = 2; i <= n; ++i ) {
45+
int c = a + b;
46+
a = b;
47+
b = c;
48+
}
49+
return b;
50+
}
51+
52+
53+
int main()
54+
{
55+
std::cout << "Demonstrating fibonacci term calculation:\n";
56+
std::cout << fib1(9) << " " << fib2(9) << " " << fib3(9) << std::endl;
57+
return 0;
58+
}

dynamic_programming_problems/lcs.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Given two strings X(1..m) and Y(1..n). Find the longest common substring
3+
* that appears left to right (but not necessarily in a contiguous manner)
4+
* in both strings. For example X = "ABCBDAB" and Y = "BDCABA"
5+
* LCS(X,Y) = {"BCBA", "BDAB", "BCAB"}.
6+
* LCS-LENGTH(X,Y) = 4
7+
*/
8+
9+
10+
#include <iostream>
11+
#include <string>
12+
#include <vector>
13+
14+
size_t max( size_t a, size_t b ) {
15+
return a > b ? a : b;
16+
}
17+
18+
size_t max(size_t a, size_t b, size_t c) {
19+
return (max(a,b) > c ) ? max(a,b) : c ;
20+
}
21+
22+
23+
24+
25+
void longest_common_subsequence(std::vector<std::vector<size_t>> & lcs,
26+
std::string s1, std::string s2)
27+
{
28+
size_t m = s1.length();
29+
size_t n = s2.length();
30+
31+
for ( size_t j = 0; j <= n; ++j ) {
32+
lcs[0][j] = 0;
33+
}
34+
35+
for ( size_t i = 0; i <= m; ++i ) {
36+
lcs[i][0] = 0;
37+
}
38+
39+
for( size_t i = 0; i < m; ++i ) {
40+
for( size_t j = 0; j < n; ++j ) {
41+
lcs[i+1][j+1] = lcs[i][j]; //getting previous max val
42+
if (s1[i] == s2[j]) {
43+
lcs[i+1][j+1]++;
44+
} else {
45+
lcs[i+1][j+1] = max(lcs[i][j+1], lcs[i+1][j]);
46+
}
47+
}
48+
}
49+
std::cout << "Longest common subsequence length - " << lcs[m][n] << std::endl;
50+
51+
size_t i = m;
52+
size_t j = n;
53+
size_t index = lcs[m][n];
54+
std::string seq(lcs[m][n], ' ');
55+
while( i > 0 && j > 0 ) {
56+
if ( s1[i-1] == s2[j-1] ) {
57+
seq[index -1 ] = s1[i-1];
58+
--index;
59+
--i;
60+
--j;
61+
} else if ( lcs[i-1][j] > lcs[i][j-1] ) {
62+
--i;
63+
} else {
64+
--j;
65+
}
66+
67+
}
68+
std::cout << "One of possible many Longest Common Subsequence is : " << seq << std::endl;
69+
70+
}
71+
72+
int main()
73+
{
74+
std::string str1, str2;
75+
std::cout << "Longest common subsequence Problem:\n";
76+
std::cout << "Enter string 1:";
77+
std::cin >> str1;
78+
std::cout << "Enter string 2:";
79+
std::cin >> str2;
80+
81+
std::vector<std::vector<size_t>> lcs( str1.length() + 1, std::vector<size_t>(str2.length() + 1));
82+
83+
longest_common_subsequence(lcs, str1, str2);
84+
return 0;
85+
}

0 commit comments

Comments
 (0)