-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
24 lines (20 loc) · 726 Bytes
/
Solution.cs
File metadata and controls
24 lines (20 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Solution
{
public int MaxDotProduct(int[] nums1, int[] nums2)
{
return DP(0, 0, nums1, nums2);
}
Dictionary<(int, int), int> memo = [];
int DP(int p1, int p2, int[] nums1, int[] nums2)
{
int n1 = nums1.Length, n2 = nums2.Length;
if (p1 >= n1 || p2 >= n2) return int.MinValue / 2;
var key = (p1, p2);
if (memo.TryGetValue(key, out int cache)) return cache;
int ans = int.MinValue;
ans = Math.Max(ans, DP(p1 + 1, p2, nums1, nums2));
ans = Math.Max(ans, DP(p1, p2 + 1, nums1, nums2));
ans = Math.Max(ans, nums1[p1] * nums2[p2] + Math.Max(0, DP(p1 + 1, p2 + 1, nums1, nums2)));
return memo[key] = ans;
}
}