Input: nums1 =[1,2,7], nums2 =[4,5,3]Output: 1Explanation: 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 is1.So, the answer is1.
Input: nums1 =[2,3,4,5,9], nums2 =[8,8,4,4,4]Output: 2Explanation: 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 is2.So, the answer is2.
To satisfy both conditions, we need to make the last element of each array the maximum in its array. Swapping at index i only affects nums1[i] and nums2[i], so we can enumerate all possible swap combinations for the last index and check if the conditions can be met with minimum swaps.
classSolution {
public:int minOperations(vector<int>& a, vector<int>& b) {
int n = a.size();
int ma =*max_element(a.begin(), a.end());
int mb =*max_element(b.begin(), b.end());
int ans = INT_MAX;
for (int swap_last =0; swap_last <2; ++swap_last) {
int x = a[n-1], y = b[n-1];
if (swap_last) swap(x, y);
if (x != ma && y != mb) continue;
int cnt = swap_last;
for (int i =0; i < n-1; ++i) {
bool need_a = (a[i] == ma && x != ma);
bool need_b = (b[i] == mb && y != mb);
if (need_a && need_b) cnt = INT_MAX;
elseif (need_a || need_b) cnt++;
}
ans = min(ans, cnt);
}
return ans == INT_MAX ?-1: ans;
}
};
classSolution {
publicintminOperations(int[] a, int[] b) {
int n = a.length;
int ma = Arrays.stream(a).max().getAsInt();
int mb = Arrays.stream(b).max().getAsInt();
int ans = Integer.MAX_VALUE;
for (int swapLast = 0; swapLast < 2; swapLast++) {
int x = a[n-1], y = b[n-1];
if (swapLast == 1) {
int tmp = x; x = y; y = tmp;
}
if (x != ma && y != mb) continue;
int cnt = swapLast;
for (int i = 0; i < n-1; i++) {
boolean needA = (a[i]== ma && x != ma);
boolean needB = (b[i]== mb && y != mb);
if (needA && needB) cnt = Integer.MAX_VALUE;
elseif (needA || needB) cnt++;
}
ans = Math.min(ans, cnt);
}
return ans == Integer.MAX_VALUE?-1 : ans;
}
}
classSolution {
funminOperations(a: IntArray, b: IntArray): Int {
val n = a.size
val ma = a.maxOrNull()!!val mb = b.maxOrNull()!!var ans = Int.MAX_VALUE
for (swapLast in0..1) {
var x = a[n-1]
var y = b[n-1]
if (swapLast ==1) {
val tmp = x; x = y; y = tmp
}
if (x != ma && y != mb) continuevar cnt = swapLast
for (i in0 until n-1) {
val needA = (a[i] == ma && x != ma)
val needB = (b[i] == mb && y != mb)
if (needA && needB) cnt = Int.MAX_VALUE
elseif (needA || needB) cnt++ }
ans = minOf(ans, cnt)
}
returnif (ans ==Int.MAX_VALUE) -1else ans
}
}
classSolution:
defminOperations(self, a: list[int], b: list[int]) -> int:
n = len(a)
ma, mb = max(a), max(b)
ans = float('inf')
for swap_last in [0, 1]:
x, y = a[-1], b[-1]
if swap_last:
x, y = y, x
if x != ma and y != mb:
continue cnt = swap_last
for i in range(n-1):
need_a = a[i] == ma and x != ma
need_b = b[i] == mb and y != mb
if need_a and need_b:
cnt = float('inf')
elif need_a or need_b:
cnt +=1 ans = min(ans, cnt)
return-1if ans == float('inf') else ans
impl Solution {
pubfnmin_operations(a: Vec<i32>, b: Vec<i32>) -> i32 {
let n = a.len();
let ma =*a.iter().max().unwrap();
let mb =*b.iter().max().unwrap();
letmut ans =i32::MAX;
for swap_last in0..2 {
let (mut x, mut y) = (a[n-1], b[n-1]);
if swap_last ==1 {
let tmp = x; x = y; y = tmp;
}
if x != ma && y != mb { continue; }
letmut cnt = swap_last;
for i in0..n-1 {
let need_a = a[i] == ma && x != ma;
let need_b = b[i] == mb && y != mb;
if need_a && need_b { cnt =i32::MAX; }
elseif need_a || need_b { cnt +=1; }
}
ans = ans.min(cnt);
}
if ans ==i32::MAX { -1 } else { ans }
}
}