Problem statement

https://leetcode.com/problems/card-flipping-game/

Solution

What is actually asked in this problem: find minimum number, written on cards, such that it is not written on both sides of some card. So, first we just create set of all numbers on sides and then iterate over all cards once again and if we have equal number of both sides, we remove it from set. Finally, we return minimum number of the rest numbers.

Complexity

Time and space complexity is O(n).

Code

class Solution:
    def flipgame(self, fronts, backs):
        values = set(fronts + backs)
        for x, y in zip(fronts, backs):
            if x == y: values.discard(x)
                
        return min(values) if values else 0