> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Card Flipping Game Python Solution with Tests

> Tested Python solution for LeetCode 822 with 19 pytest cases. Generate a practice environment with lcpy.

LeetCode 822, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table). [View on LeetCode](https://leetcode.com/problems/card-flipping-game/description/).

Generate this problem as a practice environment: tested reference solution, 19 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 822   # by problem number
lcpy gen -s card_flipping_game   # by problem name
```

## Problem

You are given two \<strong>0-indexed\</strong> integer arrays \<code>fronts\</code> and \<code>backs\</code> of length \<code>n\</code>, where the \<code>i\<sup>th\</sup>\</code> card has the positive integer \<code>fronts\[i]\</code> printed on the front and \<code>backs\[i]\</code> 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 \<strong>good\</strong> if it is facing down on some card and \<strong>not\</strong> facing up on any card.

Return \<em>the minimum possible good integer after flipping the cards\</em>. If there are no good integers, return \<code>0\</code>.

### Examples

```
Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
Output: 2
Explanation:
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].
2 is the minimum good integer as it appears facing down but not facing up.
It can be shown that 2 is the minimum possible good integer obtainable after flipping some cards.
```

```
Input: fronts = [1], backs = [1]
Output: 0
Explanation:
There are no good integers no matter how we flip the cards, so we return 0.
```

### Constraints

* n == fronts.length == backs.length
* 1 \<= n \<= 1000
* 1 \<= fronts\[i], backs\[i] \<= 2000

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/card_flipping_game/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/card_flipping_game/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def flipgame(self, fronts: list[int], backs: list[int]) -> int:
        stuck = {f for f, b in zip(fronts, backs, strict=True) if f == b}
        candidates = [x for x in fronts + backs if x not in stuck]
        return min(candidates, default=0)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.