[LeetCode] 822. Card Flipping Game

Description

On a table are N cards, with a positive integer printed on the front and back of each card (possibly different).

We flip any number of cards, and after we choose one card.

If the number X on the back of the chosen card is not on the front of any card, then this number X is good.

What is the smallest number that is good? If no number is good, output 0.

Here, fronts[i] and backs[i] represent the number on the front and back of card i.

A flip swaps the front and back numbers, so the value on the front is now on the back and vice versa.

Example:

Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
Output: 2
Explanation: If we flip the second card, the fronts are [1,3,4,4,7] and the backs are [1,2,4,1,3].
We choose the second card, which has number 2 on the back, and it isn‘t on the front of any card, so 2 is good.

Note:

  1. 1 <= fronts.length == backs.length <= 1000.
  2. 1 <= fronts[i] <= 2000.
  3. 1 <= backs[i] <= 2000.

Analyse

桌子上有N张牌,正反面都印有正整数

可以翻转任意张牌,然后选一张牌,如果这张牌背面的数字X在牌的正面没有,这个数字X就是要输出的答案

一张牌的正反面如果是同一个数字,那这个数字肯定不是答案,这个例子里的1,4就不可能是答案,在剩下的元素中输出最小的那个

fronts = [1,2,4,4,7]
backs  = [1,3,4,1,3]

写出的第一个版本

bool isExist(int value, vector<int>& vec)
{
    vector<int>::iterator it = find(vec.begin(), vec.end(), value);
    if (it != vec.end())
    {
        return true;
    }

    return false;
}

int flipgame(vector<int>& fronts, vector<int>& backs) {
    int min = 2001;
    int tmp;

    vector<int> intersect = {};

    for (int i = 0; i < fronts.size(); i++)
    {
        if (fronts[i] == backs[i])
        {
            intersect.push_back(fronts[i]);
            continue;
        }
    }

    for (int i = 0; i < fronts.size(); i++)
    {
        if (!isExist(fronts[i], intersect))
        {
            tmp = fronts[i] <
            if (isExist(backs[i], intersect))
            {
                continue;
            }
            else
            {
                tmp = backs[i];
            }
        }
        else
        {
            if (isExist(backs[i], intersect))
            {
                tmp = fronts[i];
            }
            else
            {
                tmp = fronts[i] < backs[i] ? fronts[i] : backs[i];
            }
        }

        min = min < tmp ? min : tmp;
    }

    return min == 2001 ? 0 : min;
}

把代码简化一下

bool isExist(int value, vector<int>& vec)
{
    vector<int>::iterator it = find(vec.begin(), vec.end(), value);
    if (it != vec.end())
    {
        return true;
    }

    return false;
}

int flipgame(vector<int>& fronts, vector<int>& backs) {
    int min = 2001;
    int tmp;

    vector<int> intersect = {};

    for (int i = 0; i < fronts.size(); i++)
    {
        if (fronts[i] == backs[i])
        {
            intersect.push_back(fronts[i]);
            continue;
        }
    }

    for (int i = 0; i < fronts.size(); i++)
    {
        if (!isExist(fronts[i], intersect))
        {
            tmp = fronts[i] <
            if (isExist(backs[i], intersect))
            {
                continue;
            }
            else
            {
                tmp = backs[i];
            }
        }
        else
        {
            if (isExist(backs[i], intersect))
            {
                tmp = fronts[i];
            }
            else
            {
                tmp = fronts[i] < backs[i] ? fronts[i] : backs[i];
            }
        }

        min = min < tmp ? min : tmp;
    }

    return min == 2001 ? 0 : min;
}

继续优化,把vector换成unordered_set,在LeetCode上就会有巨大的提升

int flipgame(vector<int>& fronts, vector<int>& backs) {
    int min = 2001;
    int tmp;

    unordered_set<int> intersect = {};

    for (int i = 0; i < fronts.size(); i++)
    {
        if (fronts[i] == backs[i])
        {
            intersect.insert(fronts[i]);
            continue;
        }
    }

    for (int i = 0; i < fronts.size(); i++)
    {
        if (intersect.count(fronts[i]) == 0)
        {
            min = min < fronts[i] ? min : fronts[i];
        }

        if (intersect.count(backs[i]) == 0)
        {
            min = min < backs[i] ? min : backs[i];
        }
    }

    return min == 2001 ? 0 : min;
}

看看LeetCode上评价最高的版本

思路是差不多的,改成了用数组存储,有点bitmap的思想,

int flipgame(vector<int>& fronts, vector<int>& backs) {
    int res[2002] = {0};  // -1: bad. 1:exist.
    for (int i = 0; i < fronts.size(); i++)
    {
        if (fronts[i] == backs[i])
        {
            res[fronts[i]] = -1;
        }
        else
        {
            if (res[fronts[i]] != -1)
                res[fronts[i]] = 1;
            if (res[backs[i]] != -1)
                res[backs[i]] = 1;
        }
    }

    for (int i = 0; i < 2002; i++)
    {
        if (res[i] == 1)
            return i;
    }
    return 0;
}

比如

fronts = [1,2,4,4,7]
backs  = [1,3,4,1,3]

res[1] = -1;

res[2] = 1;

res[3] = 1;

res[4] = -1;

res[7] = 1;

for循环的时候从index小的开始,遇到的第一个值为1的就是要找的值

Reference

  1. set_intersection - C++ Reference
  2. find - C++ Reference

原文地址:https://www.cnblogs.com/arcsinw/p/10317250.html

时间: 2024-10-08 06:14:13

[LeetCode] 822. Card Flipping Game的相关文章

[LeetCode] Score After Flipping Matrix 翻转数组后的分数

We have a two dimensional matrix?A?where each value is?0?or?1. A move consists of choosing any row or column, and toggling each value in that row or column: changing all?0s to?1s, and all?1s to?0s. After making any number of moves, every row of this

【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)

Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranking: 440/2797.这次题目似乎比较简单,因为我比赛的时候前三题全做出来了(1:12:39),然后第四题有思路,正在写,没写完,比赛完了写完提交也对了. p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [821]

[LeetCode] 832. Flipping an Image_Easy

Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally results in [0,

Leetcode#832. Flipping an Image(翻转图像)

题目描述 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]. 反转图片的意思是图片中的 0 全部被 1 替换, 1 全部被 0 替换.例如,反转 [0, 1, 1] 的结果是 [1, 0, 0]. 示例 1: 输入: [[1,1,0],[1,0,1],[0,0,0]] 输出: [[1,0,0],[0,1,0],[1,1,1]] 解释: 首先翻转每一行: [[0

[LeetCode&amp;Python] Problem 832. Flipping an Image

Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally results in [0,

[LeetCode] Flipping an Image 翻转图像

Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally results in [0,

(Easy) Flipping an Image LeetCode

Description: Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally re

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar