-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1187.make-array-strictly-increasing.py
77 lines (53 loc) · 2.16 KB
/
1187.make-array-strictly-increasing.py
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
71
72
73
74
75
76
77
from lc import *
class Solution:
def makeArrayIncreasing(self, a: List[int], b: List[int]) -> int:
b.sort();
@cache
def f(i,x):
return i<len(a) and min(f(i+1,a[i]) if x<a[i] else inf, 1+f(i+1,b[j]) if (j:=bisect_right(b,x))<len(b) else inf)
r = f(0,-inf)
return(-1,r)[r<inf]
class Solution:
def makeArrayIncreasing(self, a: List[int], b: List[int]) -> int:
b.sort();f=cache(lambda i,x:i<len(a)and min(f(i+1,a[i]) if x<a[i] else inf,1+f(i+1,b[j]) if (j:=bisect_right(b,x))<len(b) else inf));r=f(0,-inf);return(-1,r)[r<inf]
class Solution:
def makeArrayIncreasing(self, a: List[int], b: List[int]) -> int:
b.sort();f=cache(lambda i,x:i<len(a)and min(f(i+1,a[i])if x<a[i]else inf,(j:=bisect_right(b,x))<len(b)and 1+f(i+1,b[j])or inf));r=f(0,-inf);return(-1,r)[r<inf]
class Solution:
def makeArrayIncreasing(self, a, b):
t=inf;b.sort();f=cache(lambda i,x:i<len(a)and min(f(i+1,a[i])if x<a[i]else t,(j:=bisect_right(b,x))<len(b)and 1+f(i+1,b[j])or t));r=f(0,-t);return(-1,r)[r<t]
test('''
1187. Make Array Strictly Increasing
Hard
835
19
Add to List
Share
Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing.
In one operation, you can choose two indices 0 <= i < arr1.length and 0 <= j < arr2.length and do the assignment arr1[i] = arr2[j].
If there is no way to make arr1 strictly increasing, return -1.
Example 1:
Input: arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
Output: 1
Explanation: Replace 5 with 2, then arr1 = [1, 2, 3, 6, 7].
Example 2:
Input: arr1 = [1,5,3,6,7], arr2 = [4,3,1]
Output: 2
Explanation: Replace 5 with 3 and then replace 3 with 4. arr1 = [1, 3, 4, 6, 7].
Example 3:
Input: arr1 = [1,5,3,6,7], arr2 = [1,6,3,3]
Output: -1
Explanation: You can't make arr1 strictly increasing.
Constraints:
1 <= arr1.length, arr2.length <= 2000
0 <= arr1[i], arr2[i] <= 10^9
Accepted
14,400
Submissions
31,560
Seen this question in a real interview before?
Yes
No
Use dynamic programming.
The state would be the index in arr1 and the index of the previous element in arr2 after sorting it and removing duplicates.
''')