LeetCode 822: Card Flipping Game
Problem Description
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.
- Create a set to store all front and back numbers that are facing up.
- Iterate through the cards and add both front and back numbers to the set if they are the same.
- Iterate through the set and remove numbers that are present in both fronts and backs arrays.
- 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)
Solutions
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;
}
}
Related LeetCode Problems
Loading editor...