Skip to content

Commit 960406a

Browse files
committed
Add solution #822
1 parent 6a09bda commit 960406a

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@
630630
819|[Most Common Word](./0819-most-common-word.js)|Easy|
631631
820|[Short Encoding of Words](./0820-short-encoding-of-words.js)|Medium|
632632
821|[Shortest Distance to a Character](./0821-shortest-distance-to-a-character.js)|Easy|
633+
822|[Card Flipping Game](./0822-card-flipping-game.js)|Medium|
633634
824|[Goat Latin](./0824-goat-latin.js)|Easy|
634635
827|[Making A Large Island](./0827-making-a-large-island.js)|Hard|
635636
830|[Positions of Large Groups](./0830-positions-of-large-groups.js)|Easy|

solutions/0822-card-flipping-game.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 822. Card Flipping Game
3+
* https://leetcode.com/problems/card-flipping-game/
4+
* Difficulty: Medium
5+
*
6+
* You are given two 0-indexed integer arrays fronts and backs of length n, where the ith card has
7+
* the positive integer fronts[i] printed on the front and backs[i] printed on the back. Initially,
8+
* each card is placed on a table such that the front number is facing up and the other is facing
9+
* down. You may flip over any number of cards (possibly zero).
10+
*
11+
* After flipping the cards, an integer is considered good if it is facing down on some card and
12+
* not facing up on any card.
13+
*
14+
* Return the minimum possible good integer after flipping the cards. If there are no good integers,
15+
* return 0.
16+
*/
17+
18+
/**
19+
* @param {number[]} fronts
20+
* @param {number[]} backs
21+
* @return {number}
22+
*/
23+
var flipgame = function(fronts, backs) {
24+
const impossibleValues = new Set();
25+
const n = fronts.length;
26+
27+
for (let i = 0; i < n; i++) {
28+
if (fronts[i] === backs[i]) {
29+
impossibleValues.add(fronts[i]);
30+
}
31+
}
32+
33+
let minGoodValue = Infinity;
34+
35+
for (let i = 0; i < n; i++) {
36+
if (!impossibleValues.has(fronts[i])) {
37+
minGoodValue = Math.min(minGoodValue, fronts[i]);
38+
}
39+
40+
if (!impossibleValues.has(backs[i])) {
41+
minGoodValue = Math.min(minGoodValue, backs[i]);
42+
}
43+
}
44+
45+
return minGoodValue === Infinity ? 0 : minGoodValue;
46+
};

0 commit comments

Comments
 (0)