返回介绍

solution / 0800-0899 / 0822.Card Flipping Game / README_EN

发布于 2024-06-17 01:03:34 字数 5006 浏览 0 评论 0 收藏 0

822. Card Flipping Game

中文文档

Description

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

Solutions

Solution 1

class Solution:
  def flipgame(self, fronts: List[int], backs: List[int]) -> int:
    s = {a for a, b in zip(fronts, backs) if a == b}
    return min((x for x in chain(fronts, backs) if x not in s), default=0)
class Solution {
  public int flipgame(int[] fronts, int[] backs) {
    Set<Integer> s = new HashSet<>();
    int n = fronts.length;
    for (int i = 0; i < n; ++i) {
      if (fronts[i] == backs[i]) {
        s.add(fronts[i]);
      }
    }
    int ans = 9999;
    for (int v : fronts) {
      if (!s.contains(v)) {
        ans = Math.min(ans, v);
      }
    }
    for (int v : backs) {
      if (!s.contains(v)) {
        ans = Math.min(ans, v);
      }
    }
    return ans % 9999;
  }
}
class Solution {
public:
  int flipgame(vector<int>& fronts, vector<int>& backs) {
    unordered_set<int> s;
    int n = fronts.size();
    for (int i = 0; i < n; ++i) {
      if (fronts[i] == backs[i]) {
        s.insert(fronts[i]);
      }
    }
    int ans = 9999;
    for (int& v : fronts) {
      if (!s.count(v)) {
        ans = min(ans, v);
      }
    }
    for (int& v : backs) {
      if (!s.count(v)) {
        ans = min(ans, v);
      }
    }
    return ans % 9999;
  }
};
func flipgame(fronts []int, backs []int) int {
  s := map[int]struct{}{}
  for i, a := range fronts {
    if a == backs[i] {
      s[a] = struct{}{}
    }
  }
  ans := 9999
  for _, v := range fronts {
    if _, ok := s[v]; !ok {
      ans = min(ans, v)
    }
  }
  for _, v := range backs {
    if _, ok := s[v]; !ok {
      ans = min(ans, v)
    }
  }
  return ans % 9999
}
function flipgame(fronts: number[], backs: number[]): number {
  const s: Set<number> = new Set();
  const n = fronts.length;
  for (let i = 0; i < n; ++i) {
    if (fronts[i] === backs[i]) {
      s.add(fronts[i]);
    }
  }
  let ans = 9999;
  for (const v of fronts) {
    if (!s.has(v)) {
      ans = Math.min(ans, v);
    }
  }
  for (const v of backs) {
    if (!s.has(v)) {
      ans = Math.min(ans, v);
    }
  }
  return ans % 9999;
}
public class Solution {
  public int Flipgame(int[] fronts, int[] backs) {
    var s = new HashSet<int>();
    int n = fronts.Length;
    for (int i = 0; i < n; ++i) {
      if (fronts[i] == backs[i]) {
        s.Add(fronts[i]);
      }
    }
    int ans = 9999;
    for (int i = 0; i < n; ++i) {
      if (!s.Contains(fronts[i])) {
        ans = Math.Min(ans, fronts[i]);
      }
      if (!s.Contains(backs[i])) {
        ans = Math.Min(ans, backs[i]);
      }
    }
    return ans % 9999;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文