|
| 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