-
Notifications
You must be signed in to change notification settings - Fork 0
/
No_1035.cs
38 lines (34 loc) · 1.08 KB
/
No_1035.cs
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
using System.Globalization;
namespace LeetCode
{
public class No_1035
{
public int MaxUncrossedLines(int[] nums1, int[] nums2)
{
int n = nums1.Length + 1, m = nums2.Length + 1;
int[] rec = new int[m], rec1 = new int[m];
for (int i = 1; i < n; i++)
{
for (int j = 1; j < m; j++)
{
rec1[j] = nums1[i - 1] == nums2[j - 1] ? 1 + rec[j - 1] : Math.Max(rec1[j - 1], rec[j]);
}
rec1.CopyTo(rec, 0);
}
return rec1[m - 1];
}
public int MaxUncrossedLines1(int[] nums1, int[] nums2)
{
int n = nums1.Length, m = nums2.Length;
int[,] rec = new int[n + 1, m + 1];
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
rec[i, j] = nums1[i - 1] == nums2[j - 1] ? 1 + rec[i - 1, j - 1] : Math.Max(rec[i, j - 1], rec[i - 1, j]);
}
}
return rec[n, m];
}
}
}