Problem

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.

Examples

Example 1

1
2
3
4
5
6
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.

Example 2

1
2
3
4
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

Intuition

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.

Approach

  1. Create a set of numbers that appear on both sides of the same card (excluded set).
  2. For each number on the backs of all cards, if it is not in the excluded set, track the minimum value.
  3. Return the minimum found, or 0 if none exists.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
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;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func flipgame(fronts []int, backs []int) int {
    same := map[int]bool{}
    n, ans := len(fronts), 2001
    for i := 0; i < n; i++ {
        if fronts[i] == backs[i] {
            same[fronts[i]] = true
        }
    }
    for _, x := range fronts {
        if !same[x] && x < ans {
            ans = x
        }
    }
    for _, x := range backs {
        if !same[x] && x < ans {
            ans = x
        }
    }
    if ans == 2001 {
        return 0
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    public int flipgame(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
class Solution {
    fun flipgame(fronts: IntArray, backs: IntArray): Int {
        val same = mutableSetOf<Int>()
        val n = fronts.size
        var ans = 2001
        for (i in 0 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)
        return if (ans == 2001) 0 else ans
    }
}
1
2
3
4
5
6
7
8
class Solution:
    def flipgame(self, fronts: list[int], backs: list[int]) -> int:
        same = {x for x, y in zip(fronts, backs) if x == y}
        ans = 2001
        for x in fronts + backs:
            if x not in same:
                ans = min(ans, x)
        return 0 if ans == 2001 else 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 {
    pub fn flipgame(fronts: Vec<i32>, backs: Vec<i32>) -> i32 {
        let n = fronts.len();
        let mut same = HashSet::new();
        let mut ans = 2001;
        for i in 0..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
class Solution {
    flipgame(fronts: number[], backs: number[]): number {
        const same = new Set<number>();
        let ans = 2001;
        for (let i = 0; i < fronts.length; i++) {
            if (fronts[i] === backs[i]) same.add(fronts[i]);
        }
        for (const x of fronts) if (!same.has(x)) ans = Math.min(ans, x);
        for (const x of backs) if (!same.has(x)) ans = Math.min(ans, x);
        return ans === 2001 ? 0 : ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n)
  • 🧺 Space complexity: O(n)