# 822. Card Flipping Game

• Time: $O(n)$
• Space: $O(n)$
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class Solution { public: int flipgame(vector& fronts, vector& backs) { constexpr int kMax = 2001; int ans = kMax; unordered_set same; for (int i = 0; i < fronts.size(); ++i) if (fronts[i] == backs[i]) same.insert(fronts[i]); for (const int front : fronts) if (!same.count(front)) ans = min(ans, front); for (const int back : backs) if (!same.count(back)) ans = min(ans, back); return ans == kMax ? 0 : ans; } }; 
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Solution { public int flipgame(int[] fronts, int[] backs) { final int kMax = 2001; int ans = kMax; Set same = new HashSet<>(); for (int i = 0; i < fronts.length; ++i) if (fronts[i] == backs[i]) same.add(fronts[i]); for (final int front : fronts) if (!same.contains(front)) ans = Math.min(ans, front); for (final int back : backs) if (!same.contains(back)) ans = Math.min(ans, back); return ans == kMax ? 0 : ans; } } 
 1 2 3 4 5 6 7 class Solution: def flipgame(self, fronts: List[int], backs: List[int]) -> int: same = {front for front, back in zip(fronts, backs) if front == back} return min([num for num in fronts + backs if num not in same] or [0])