You are given two 0-indexed integer arrays fronts and backs of length n, where the ith card has the positive integer fronts[i] printed on the front and backs[i] printed on the back. Initially, each card is placed on a table such that the front number is facing up and the other is facing down.
You may flip over any number of cards (possibly zero).
After flipping the cards, an integer is considered good if it is facing down on some card and not facing up on any card.
Return the minimum possible good integer after flipping the cards. If there are no good integers, return 0.
Input: fronts =[1,2,4,4,7], backs =[1,3,4,1,3]Output: 2Explanation:
If we flip the second card, the face up numbers are [1,3,4,4,7] and the face down are [1,2,4,1,3].2is the minimum good integer as it appears facing down but not facing up.It can be shown that 2is the minimum possible good integer obtainable after flipping some cards.
If a number appears on both sides of the same card, it can never be a good integer (since it will always be facing up on some card). For all other numbers, if it appears on the back of any card and is not excluded, it is a candidate for the answer. The minimum such number is the answer.
classSolution {
public:int flipgame(vector<int>& fronts, vector<int>& backs) {
unordered_set<int> same;
int n = fronts.size(), ans = INT_MAX;
for (int i =0; i < n; ++i) if (fronts[i] == backs[i]) same.insert(fronts[i]);
for (int x : fronts) if (!same.count(x)) ans = min(ans, x);
for (int x : backs) if (!same.count(x)) ans = min(ans, x);
return ans == INT_MAX ?0: ans;
}
};
funcflipgame(fronts []int, backs []int) int {
same:=map[int]bool{}
n, ans:= len(fronts), 2001fori:=0; i < n; i++ {
iffronts[i] ==backs[i] {
same[fronts[i]] = true }
}
for_, x:=rangefronts {
if !same[x] &&x < ans {
ans = x }
}
for_, x:=rangebacks {
if !same[x] &&x < ans {
ans = x }
}
ifans==2001 {
return0 }
returnans}
1
2
3
4
5
6
7
8
9
10
classSolution {
publicintflipgame(int[] fronts, int[] backs) {
Set<Integer> same =new HashSet<>();
int n = fronts.length, ans = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) if (fronts[i]== backs[i]) same.add(fronts[i]);
for (int x : fronts) if (!same.contains(x)) ans = Math.min(ans, x);
for (int x : backs) if (!same.contains(x)) ans = Math.min(ans, x);
return ans == Integer.MAX_VALUE? 0 : ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
funflipgame(fronts: IntArray, backs: IntArray): Int {
val same = mutableSetOf<Int>()
val n = fronts.size
var ans = 2001for (i in0 until n) if (fronts[i] == backs[i]) same.add(fronts[i])
for (x in fronts) if (x !in same) ans = minOf(ans, x)
for (x in backs) if (x !in same) ans = minOf(ans, x)
returnif (ans ==2001) 0else ans
}
}
1
2
3
4
5
6
7
8
classSolution:
defflipgame(self, fronts: list[int], backs: list[int]) -> int:
same = {x for x, y in zip(fronts, backs) if x == y}
ans =2001for x in fronts + backs:
if x notin same:
ans = min(ans, x)
return0if ans ==2001else ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use std::collections::HashSet;
impl Solution {
pubfnflipgame(fronts: Vec<i32>, backs: Vec<i32>) -> i32 {
let n = fronts.len();
letmut same = HashSet::new();
letmut ans =2001;
for i in0..n {
if fronts[i] == backs[i] {
same.insert(fronts[i]);
}
}
for&x in fronts.iter().chain(backs.iter()) {
if!same.contains(&x) && x < ans {
ans = x;
}
}
if ans ==2001 { 0 } else { ans }
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
flipgame(fronts: number[], backs: number[]):number {
constsame=newSet<number>();
letans=2001;
for (leti=0; i<fronts.length; i++) {
if (fronts[i] ===backs[i]) same.add(fronts[i]);
}
for (constxoffronts) if (!same.has(x)) ans= Math.min(ans, x);
for (constxofbacks) if (!same.has(x)) ans= Math.min(ans, x);
returnans===2001?0 : ans;
}
}