842. Card Flipping Game
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum "good" integer we can achieve by flipping any number of cards. A number is "good" if it appears on the back (face down) of some card and not on the front (face up) of any card. We have two arrays, fronts and backs, representing the numbers on the cards.
Solution
Card Flipping Game in Python
class Solution:
def flipgame(self, fronts: List[int], backs: List[int]) -> int:
# A set to store numbers that appear on both sides of the same card.
# These numbers can never be "good" because we can't hide them.
impossible = {f for f, b in zip(fronts, backs) if f == b}
# A candidate for a good number is any number on any card
# that is not in the 'impossible' set. We want the minimum such number.
# We can use a generator expression to find all candidates.
candidates = (num for num in fronts + backs if num not in impossible)
# The min() function with a generator and a default value is a concise way
# to find the minimum candidate or return 0 if there are none.
return min(candidates, default=0)
Card Flipping Game in Java
class Solution {
public int flipgame(int[] fronts, int[] backs) {
// A set to store numbers that appear on both sides of the same card.
// These numbers can never be "good" because we can't hide them.
Set<Integer> impossible = new HashSet<>();
for (int i = 0; i < fronts.length; i++) {
if (fronts[i] == backs[i]) {
impossible.add(fronts[i]);
}
}
// Initialize the result with a value larger than any possible card number.
int minGoodNum = 2001; // Constraints: 1 <= fronts[i], backs[i] <= 2000
// Iterate through all numbers on the cards (both fronts and backs).
for (int i = 0; i < fronts.length; i++) {
// Check the number on the front of the card.
// If it's not an impossible number, it's a candidate for the minimum good number.
if (!impossible.contains(fronts[i])) {
minGoodNum = Math.min(minGoodNum, fronts[i]);
}
// Check the number on the back of the card.
if (!impossible.contains(backs[i])) {
minGoodNum = Math.min(minGoodNum, backs[i]);
}
}
// If minGoodNum was not updated, it means all numbers were impossible.
// In this case, there are no good integers, so return 0.
// Otherwise, return the minimum good number found.
return (minGoodNum == 2001) ? 0 : minGoodNum;
}
}
Card Flipping Game in C++
class Solution {
public:
int flipgame(vector<int>& fronts, vector<int>& backs) {
// A set to store numbers that appear on both sides of the same card.
// These numbers can never be "good" because we can't hide them.
std::unordered_set<int> impossible;
for (int i = 0; i < fronts.size(); ++i) {
if (fronts[i] == backs[i]) {
impossible.insert(fronts[i]);
}
}
// Initialize the result with a value larger than any possible card number.
int min_good_num = 2001; // Constraints: 1 <= fronts[i], backs[i] <= 2000
// Iterate through all numbers on the cards (both fronts and backs).
for (int i = 0; i < fronts.size(); ++i) {
// Check the number on the front of the card.
// If it's not an impossible number, it's a candidate for the minimum good number.
if (impossible.find(fronts[i]) == impossible.end()) {
min_good_num = std::min(min_good_num, fronts[i]);
}
// Check the number on the back of the card.
if (impossible.find(backs[i]) == impossible.end()) {
min_good_num = std::min(min_good_num, backs[i]);
}
}
// If min_good_num was not updated, it means all numbers were impossible.
// In this case, there are no good integers, so return 0.
// Otherwise, return the minimum good number found.
return (min_good_num == 2001) ? 0 : min_good_num;
}
};
Card Flipping Game in TypeScript
function flipgame(fronts: number[], backs: number[]): number {
// A set to store numbers that appear on both sides of the same card.
// These numbers can never be "good" because we can't hide them.
const impossible = new Set<number>();
for (let i = 0; i < fronts.length; i++) {
if (fronts[i] === backs[i]) {
impossible.add(fronts[i]);
}
}
// Initialize the result with a value larger than any possible card number.
let minGoodNum = 2001; // Constraints: 1 <= fronts[i], backs[i] <= 2000
// Iterate through all numbers on the cards (both fronts and backs).
for (let i = 0; i < fronts.length; i++) {
// Check the number on the front of the card.
// If it's not an impossible number, it's a candidate for the minimum good number.
if (!impossible.has(fronts[i])) {
minGoodNum = Math.min(minGoodNum, fronts[i]);
}
// Check the number on the back of the card.
if (!impossible.has(backs[i])) {
minGoodNum = Math.min(minGoodNum, backs[i]);
}
}
// If minGoodNum was not updated, it means all numbers were impossible.
// In this case, there are no good integers, so return 0.
// Otherwise, return the minimum good number found.
return (minGoodNum === 2001) ? 0 : minGoodNum;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.