题目:
On a table are N
cards, with a positive integer printed on the front and back of each card (possibly different).
We flip any number of cards, and after we choose one card.
If the number X
on the back of the chosen card is not on the front of any card, then this number X is good.
What is the smallest number that is good? If no number is good, output 0
.
Here, fronts[i]
and backs[i]
represent the number on the front and back of card i
.
A flip swaps the front and back numbers, so the value on the front is now on the back and vice versa.
Example:
Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3] Output:2
Explanation: If we flip the second card, the fronts are[1,3,4,4,7]
and the backs are[1,2,4,1,3]
. We choose the second card, which has number 2 on the back, and it isn't on the front of any card, so2
is good.
Note:
1 <= fronts.length == backs.length <= 1000.
1 <= fronts[i] <= 2000
.1 <= backs[i] <= 2000
.
思路:
注意到一个现象就是:如果某一张牌的正面和背面的数字是一样的,那么无论翻转还是不翻转,该数字都会出现在正面的,所以该数字不可能为good number。对于其余的数字,我们实际上只需要找出最小值即可,这是由于:一旦该最小值出现在某个牌的正面,我们只需要将该牌翻转即可(由于该牌的正反面不一样,所以将该牌翻转之后,出现在正面的一定是另外一个数字)。
在实现中,我们用哈希表存储正面和背面都一样的数字,然后对于不在哈希表中出现的所有数字,找出其最小值。
代码:
class Solution {
public:
int flipgame(vector<int>& fronts, vector<int>& backs) {
unordered_set<int> same;
for (int i = 0; i < fronts.size(); ++i) {
if (fronts[i] == backs[i]) {
same.insert(fronts[i]);
}
}
int res = INT_MAX;
for (int & i : fronts) {
if (same.count(i) == 0) {
res = min(res, i);
}
}
for (int & i : backs) {
if (same.count(i) == 0) {
res = min(res, i);
}
}
return res == INT_MAX ? 0 : res;
}
};