Skip to content

Commit 741f46e

Browse files
committed
Day 26
1 parent 5e21d4c commit 741f46e

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

30 Days Challenge/Jump Game.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
bool canJump(vector<int> nums) {
4+
int maxIndex = nums.size() - 1;
5+
for(int i=nums.size()-2; i>=0; i--){
6+
if(i + nums[i] >= maxIndex){
7+
maxIndex = i;
8+
}
9+
}
10+
return maxIndex == 0;
11+
}
12+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
string text1;
3+
string text2;
4+
vector<vector<int>> vec;
5+
public:
6+
int longestCommonSubsequence(string text1, string text2) {
7+
vec = vector<vector<int>>(text1.length(), vector<int>(text2.length(), -1));
8+
this->text1 = text1;
9+
this->text2 = text2;
10+
return longestCommonSubsequence2(0, 0);
11+
}
12+
13+
int longestCommonSubsequence2(int s1, int s2) {
14+
if(s1 == text1.length() || s2 == text2.length()){
15+
return 0;
16+
}
17+
if(vec[s1][s2] != -1){
18+
return vec[s1][s2];
19+
}
20+
int l1 = longestCommonSubsequence2(s1, s2 + 1);
21+
int l2 = longestCommonSubsequence2(s1 + 1, s2);
22+
int lcs = max(l1, l2);
23+
if(text1[s1] == text2[s2]){
24+
int l3 = longestCommonSubsequence2(s1 + 1, s2 + 1) + 1;
25+
lcs = max(l1, l3);
26+
}
27+
vec[s1][s2] = lcs;
28+
return lcs;
29+
}
30+
};

0 commit comments

Comments
 (0)