Skip to content

Commit 4e58137

Browse files
Updated day26 solution
1 parent 61b5cea commit 4e58137

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int maxUncrossedLines(vector<int>& A, vector<int>& B) {
4+
int n=A.size();
5+
int m=B.size();
6+
7+
int dp[n+1][m+1];
8+
9+
10+
for(int i=0;i<=n;++i)
11+
{
12+
for(int j=0;j<=m;++j)
13+
{
14+
if(i==0 || j==0)
15+
dp[i][j]=0;
16+
else if(A[i-1]==B[j-1])
17+
dp[i][j]=dp[i-1][j-1]+1;
18+
else
19+
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
20+
}
21+
}
22+
return dp[n][m];
23+
}
24+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int findMaxLength(vector<int>& nums) {
4+
int sum=0;
5+
unordered_map<int,int> m;
6+
unsigned int longestContArray = 0;
7+
8+
for(int i=0;i<nums.size();i++){
9+
sum += (nums[i]==1)?1 : -1;
10+
11+
auto it = m.find(sum);
12+
if(sum == 0){
13+
if(longestContArray < i+1)
14+
longestContArray = i+1;
15+
}
16+
17+
else if(it != m.end()){
18+
if(longestContArray < i-it->second)
19+
longestContArray = i-it->second;
20+
}
21+
else if(it == m.end())
22+
m.insert({sum,i});
23+
}
24+
return longestContArray;
25+
}
26+
};

0 commit comments

Comments
 (0)