<p>Given two integer arrays&nbsp;<code>arr1</code> and <code>arr2</code>, return the minimum number of operations (possibly zero) needed&nbsp;to make <code>arr1</code> strictly increasing.</p>

<p>In one operation, you can choose two indices&nbsp;<code>0 &lt;=&nbsp;i &lt; arr1.length</code>&nbsp;and&nbsp;<code>0 &lt;= j &lt; arr2.length</code>&nbsp;and do the assignment&nbsp;<code>arr1[i] = arr2[j]</code>.</p>

<p>If there is no way to make&nbsp;<code>arr1</code>&nbsp;strictly increasing,&nbsp;return&nbsp;<code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Replace <code>5</code> with <code>2</code>, then <code>arr1 = [1, 2, 3, 6, 7]</code>.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr1 = [1,5,3,6,7], arr2 = [4,3,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Replace <code>5</code> with <code>3</code> and then replace <code>3</code> with <code>4</code>. <code>arr1 = [1, 3, 4, 6, 7]</code>.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr1 = [1,5,3,6,7], arr2 = [1,6,3,3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> You can&#39;t make <code>arr1</code> strictly increasing.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr1.length, arr2.length &lt;= 2000</code></li>
	<li><code>0 &lt;= arr1[i], arr2[i] &lt;= 10^9</code></li>
</ul>

<p>&nbsp;</p>

from bisect import bisect_right

class Solution:
    def makeArrayIncreasing(self, arr1: List[int], arr2: List[int]) -> int:
        # strictly increasing: can only use every no from arr2 once
        # backtracking type solution:
        #  just try the replacement
        #     1. keep as is if possible (looking back), work on rest
        #     2. replace by numbers that fit (looking back)
        #     3. take the min of all these
        # as expected get a TLE (on 15/22); i.e. solution looks correct but too slow
        # can prune in the candidate trials, if get zero op more solution, don't have
        #   to try the other ones
        # didn't help make it quick enough; in a reduced case it did speed things up
        # removing duplicates from arr2 now TLE on (19/22)

        # don't have to try all candidates, the smallest that will fit will be best
        arr2 = list(set(arr2))
        arr2.sort()
        mem = {}
        def h(prev, xs, candidates):
            arg_str = str(prev) + ":" + str(len(xs)) + ":" + str(len(candidates))
            if arg_str in mem.keys():
                return mem[arg_str]
            #print(prev, xs, candidates, op_ct)
            if not xs:
                #print("xx")
                return 0

            if prev < xs[0]:
                keep_ct = h(xs[0], xs[1:], candidates)  # don't change element
            else:
                keep_ct = -666

            idx = bisect_right(candidates, prev)
            if 0<=idx<len(candidates):
                replace_ct = 1 + h(candidates[idx], xs[1:], candidates[idx:])
            else:
                replace_ct = -666

            op_cts = [c for c in [keep_ct, replace_ct] if c >= 0]
            mem[arg_str] = min(op_cts) if op_cts else -666
            return mem[arg_str]

        rv = h(-1, arr1, arr2)
        return rv if rv >=0 else -1
147300