mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
61 lines
2.9 KiB
HTML
61 lines
2.9 KiB
HTML
<p>You are given two <strong>0-indexed</strong> integer arrays, <code>nums1</code> and <code>nums2</code>, both having length <code>n</code>.</p>
|
|
|
|
<p>You are allowed to perform a series of <strong>operations</strong> (<strong>possibly none</strong>).</p>
|
|
|
|
<p>In an operation, you select an index <code>i</code> in the range <code>[0, n - 1]</code> and <strong>swap</strong> the values of <code>nums1[i]</code> and <code>nums2[i]</code>.</p>
|
|
|
|
<p>Your task is to find the <strong>minimum</strong> number of operations required to satisfy the following conditions:</p>
|
|
|
|
<ul>
|
|
<li><code>nums1[n - 1]</code> is equal to the <strong>maximum value</strong> among all elements of <code>nums1</code>, i.e., <code>nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1])</code>.</li>
|
|
<li><code>nums2[n - 1]</code> is equal to the <strong>maximum</strong> <strong>value</strong> among all elements of <code>nums2</code>, i.e., <code>nums2[n - 1] = max(nums2[0], nums2[1], ..., nums2[n - 1])</code>.</li>
|
|
</ul>
|
|
|
|
<p>Return <em>an integer denoting the <strong>minimum</strong> number of operations needed to meet <strong>both</strong> conditions</em>, <em>or </em><code>-1</code><em> if it is <strong>impossible</strong> to satisfy both conditions.</em></p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums1 = [1,2,7], nums2 = [4,5,3]
|
|
<strong>Output:</strong> 1
|
|
<strong>Explanation:</strong> In this example, an operation can be performed using index i = 2.
|
|
When nums1[2] and nums2[2] are swapped, nums1 becomes [1,2,3] and nums2 becomes [4,5,7].
|
|
Both conditions are now satisfied.
|
|
It can be shown that the minimum number of operations needed to be performed is 1.
|
|
So, the answer is 1.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums1 = [2,3,4,5,9], nums2 = [8,8,4,4,4]
|
|
<strong>Output:</strong> 2
|
|
<strong>Explanation:</strong> In this example, the following operations can be performed:
|
|
First operation using index i = 4.
|
|
When nums1[4] and nums2[4] are swapped, nums1 becomes [2,3,4,5,4], and nums2 becomes [8,8,4,4,9].
|
|
Another operation using index i = 3.
|
|
When nums1[3] and nums2[3] are swapped, nums1 becomes [2,3,4,4,4], and nums2 becomes [8,8,4,5,9].
|
|
Both conditions are now satisfied.
|
|
It can be shown that the minimum number of operations needed to be performed is 2.
|
|
So, the answer is 2.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums1 = [1,5,4], nums2 = [2,5,3]
|
|
<strong>Output:</strong> -1
|
|
<strong>Explanation:</strong> In this example, it is not possible to satisfy both conditions.
|
|
So, the answer is -1.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= n == nums1.length == nums2.length <= 1000</code></li>
|
|
<li><code>1 <= nums1[i] <= 10<sup>9</sup></code></li>
|
|
<li><code>1 <= nums2[i] <= 10<sup>9</sup></code></li>
|
|
</ul>
|