-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1187.cs
70 lines (59 loc) · 1.9 KB
/
Solution1187.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution1187
{
/// <summary>
/// 1187. Make Array Strictly Increasing - Hard
/// <a href="https://leetcode.com/problems/make-array-strictly-increasing">See the problem</a>
/// </summary>
public int MakeArrayIncreasing(int[] arr1, int[] arr2)
{
Array.Sort(arr2);
var sortedArr2 = new List<int>();
// Remove duplicates
foreach (int num in arr2)
{
if (sortedArr2.Count == 0 || sortedArr2[^1] != num)
sortedArr2.Add(num);
}
var memo = new Dictionary<(int, int), int>();
int result = Dfs(0, -1);
return result == int.MaxValue ? -1 : result;
int Dfs(int i, int prev)
{
if (i == arr1.Length) return 0;
if (memo.ContainsKey((i, prev))) return memo[(i, prev)];
int minOps = int.MaxValue;
// Option 1: Keep arr1[i] if valid
if (arr1[i] > prev)
{
int keep = Dfs(i + 1, arr1[i]);
minOps = Math.Min(minOps, keep);
}
// Option 2: Replace arr1[i] with a higher element from arr2
int idx = UpperBound(sortedArr2, prev);
if (idx < sortedArr2.Count)
{
int replace = Dfs(i + 1, sortedArr2[idx]);
if (replace != int.MaxValue)
minOps = Math.Min(minOps, 1 + replace);
}
memo[(i, prev)] = minOps;
return minOps;
}
int UpperBound(List<int> list, int val)
{
int l = 0, r = list.Count;
while (l < r)
{
int m = (l + r) / 2;
if (list[m] <= val)
l = m + 1;
else
r = m;
}
return l;
}
}
}