-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlc1035.java
More file actions
62 lines (60 loc) · 1.46 KB
/
Copy pathlc1035.java
File metadata and controls
62 lines (60 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Solution {
public int maxUncrossedLines(int[] A, int[] B) {
int m = A.length;
int n = B.length;
int[][] dp= new int[m][n];
int count = 0;
for(int i=0;i<m;i++)
{
//dp[i] = new int[n];
for(int j=0;j<n;j++)
{
dp[i][j] = 0;
}
}
boolean foundMatch = false;
for(int i=0;i<m;i++)
{
if(foundMatch)
{
dp[i][0] = 1;
}
else if(B[0]==A[i])
{
dp[i][0] = 1;
count = 1;
foundMatch =true;
}
}
foundMatch = false;
for(int j=0;j<n;j++)
{
if(foundMatch)
{
dp[0][j] = 1;
}
else if(B[j]==A[0])
{
dp[0][j] = 1;
count = 1;
foundMatch =true;
}
}
for(int i=1;i<m;i++)
{
for(int j=1;j<n;j++)
{
if(A[i]==B[j])
{
dp[i][j] =1+ Math.min(dp[i-1][j-1],Math.min(dp[i][j-1],dp[i-1][j]));
}
else
{
dp[i][j] = Math.max(dp[i-1][j-1],Math.max(dp[i][j-1],dp[i-1][j]));
}
count = Math.max(dp[i][j],count);
}
}
return count;
}
}