822. Card Flipping Game LeetCode Solution
In this guide, you will get 822. Card Flipping Game LeetCode Solution with the best time and space complexity. The solution to Card Flipping Game problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Card Flipping Game solution in C++
- Card Flipping Game solution in Java
- Card Flipping Game solution in Python
- Additional Resources
Problem Statement of Card Flipping Game
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.
Example 1:
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:
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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
822. Card Flipping Game LeetCode Solution in C++
class Solution {
public:
int flipgame(vector<int>& fronts, vector<int>& backs) {
constexpr int kMax = 2001;
int ans = kMax;
unordered_set<int> 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.contains(front))
ans = min(ans, front);
for (const int back : backs)
if (!same.contains(back))
ans = min(ans, back);
return ans == kMax ? 0 : ans;
}
};
/* code provided by PROGIEZ */
822. Card Flipping Game LeetCode Solution in Java
class Solution {
public int flipgame(int[] fronts, int[] backs) {
final int kMax = 2001;
int ans = kMax;
Set<Integer> 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;
}
}
// code provided by PROGIEZ
822. Card Flipping Game LeetCode Solution in Python
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])
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.