Last active 1729748267

Revision 215c103b3bc7ad2540ecf41c860efb8db8c224d2

leet.cs Raw
1int Foo(int[] arr1, int[] arr2) {
2 Array.Sort(arr2);
3 Dictionary<(int, int), int> dictionary = new Dictionary<(int, int), int>();
4
5 int Bar(int i, int prev) {
6 if (i == arr1.Length) return 0;
7 if (dictionary.ContainsKey((i, prev))) return dictionary[(i, prev)];
8
9 int cost = int.MaxValue;
10 if (arr1[i] > prev) {
11 cost = Bar(i + 1, arr1[i]);
12 }
13
14 int idx = Array.BinarySearch(arr2, prev + 1);
15 if (idx < 0) idx = ~idx;
16 if (idx < arr2.Length) {
17 cost = Math.Min(cost, 1 + Bar(i + 1, arr2[idx]));
18 }
19
20 dictionary[(i, prev)] = cost;
21 return cost;
22 }
23
24 int result = Bar(0, int.MinValue);
25 return result == int.MaxValue ? -1 : result;
26}
27
28int[] arr1 = {1, 5, 3, 6, 7};
29int[] arr2 = {1, 3, 2, 4};
30int result = Foo(arr1, arr2);
31Console.WriteLine(result);