LeetCode 822: Card Flipping Game Solution

Master LeetCode problem 822 (Card Flipping Game), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

822. Card Flipping Game

Problem Explanation

Explanation

To find the minimum possible good integer after flipping the cards, we need to consider each card and determine if its front or back face can be a good integer after flipping some cards. We can iterate through all possible good integers and keep track of which integers are not possible based on the fronts and backs of the cards.

  1. Create a set to store all front and back numbers that are facing up.
  2. Iterate through the cards and add both front and back numbers to the set if they are the same.
  3. Iterate through the set and remove numbers that are present in both fronts and backs arrays.
  4. Find the minimum number that is not present in the set. This will be the minimum good integer after flipping the cards.

Time complexity: O(n) Space complexity: O(n)

Solution Code

class Solution {
    public int flipgame(int[] fronts, int[] backs) {
        Set<Integer> sameNums = new HashSet<>();
        for (int i = 0; i < fronts.length; i++) {
            if (fronts[i] == backs[i]) {
                sameNums.add(fronts[i]);
            }
        }
        
        for (int i = 0; i < fronts.length; i++) {
            sameNums.remove(fronts[i]);
            sameNums.remove(backs[i]);
        }
        
        int minGood = Integer.MAX_VALUE;
        for (int num : sameNums) {
            minGood = Math.min(minGood, num);
        }
        
        return minGood == Integer.MAX_VALUE ? 0 : minGood;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 822 (Card Flipping Game)?

This page provides optimized solutions for LeetCode problem 822 (Card Flipping Game) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 822 (Card Flipping Game)?

The time complexity for LeetCode 822 (Card Flipping Game) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 822 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 822 in Java, C++, or Python.

Back to LeetCode Solutions