[LeetCode] Card Flipping Game

822. Card Flipping Game

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int flipgame(vector<int>& fronts, vector<int>& backs) {
unordered_set<int> exc, cand;
int n = fronts.size(), res = INT_MAX;
for(int i = 0; i < n; i++) {
if(fronts[i] == backs[i]) exc.insert(fronts[i]);
else {
cand.insert(fronts[i]);
cand.insert(backs[i]);
}
}
for(auto n : cand) {
if(!exc.count(n)) res = min(res, n);
}
return res == INT_MAX ? 0 : res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/26/PS/LeetCode/card-flipping-game/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.