Skip to content

Commit 2e54d6c

Browse files
authored
Create Day25.cpp
1 parent e4679dc commit 2e54d6c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Day25.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Author: Aryan Yadav
3+
Uncrossed Lines
4+
5+
Algorithm: Dynamic Programming(Memoization)
6+
Difficulty: Hard
7+
*/
8+
9+
using namespace std;
10+
class Solution {
11+
public:
12+
int solution(int i, int j, vector <int>&A, vector <int>&B, vector <vector <int> >& dp){
13+
if(i >= A.size()) return 0;
14+
if(j >= B.size()) return 0;
15+
if(dp[i][j] != -1) return dp[i][j];
16+
int nj = j;
17+
while(nj < B.size() && B[nj] != A[i]) nj++;
18+
int ret = max(solution(i + 1, j, A, B, dp), (nj < B.size() ? 1 :
19+
0) + solution(i + 1, nj + 1, A, B, dp));
20+
return dp[i][j] = ret;
21+
}
22+
23+
int maxUncrossedLines(vector<int>& A, vector<int>& B) {
24+
int n = A.size();
25+
int m = B.size();
26+
vector<vector<int>> dp(n, vector<int>(m, -1));
27+
return solution(0,0,A,B,dp);
28+
}
29+
};

0 commit comments

Comments
 (0)