【LeetCode】822. Card Flipping Game 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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 <= fronts.length == backs.length <= 1000.
- 1 <= fronts[i] <= 2000.
- 1 <= backs[i] <= 2000.
题目大意
有一些正反面都有数字的牌,我们可以做很多次翻转操作,每次翻转时如果这个牌背面的数字没有在这群牌的正面出现过,那么就可以把这个牌翻转过来。求翻转哪个牌可以之后,可以使得所有牌正面中的最小值最小。
解题方法
这个题的英文描述不清,我尽量翻译的清楚了。
所以,如果一个牌正反面相等,那么翻转不翻转没什么意义。否则可以翻转,求翻哪些之后会得到最小,就是如果不翻转的最小值和翻转了之后的最小值的最小值。使用set保存一下正反面相等的数字,这些是一定不能在正面出现的,然后找出不在这个set里面的正反面的最小值即可。
怎么理解?首先正反面相同的翻转没有意义,然后找在正反面的最小值把它翻转到正面来。那么有人会想,如果翻转这个牌和其他的正面的牌有冲突怎么办?其实,如果和set里面的牌有冲突没有意义,如果和不在set里面的正面的牌有冲突就把这个冲突的牌也翻转即可。所以,不用考虑这么多。。
时间复杂度是O(N),空间复杂度最坏是O(N).
代码如下:
class Solution:
def flipgame(self, fronts, backs):
"""
:type fronts: List[int]
:type backs: List[int]
:rtype: int
"""
s = set()
res = float('inf')
for f, b in zip(fronts, backs):
if f == b:
s.add(f)
for f in fronts:
if f not in s:
res = min(res, f)
for b in backs:
if b not in s:
res = min(res, b)
return 0 if res == float('inf') else res
参考资料:
https://zxi.mytechroad.com/blog/hashtable/leetcode-822-card-flipping-game/
日期
2018 年 9 月 27 日 ———— 今天起得格外早
【LeetCode】822. Card Flipping Game 解题报告(Python)的更多相关文章
- [LeetCode] 822. Card Flipping Game
Description On a table are N cards, with a positive integer printed on the front and back of each ca ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
随机推荐
- WPS表格数据透视表的美化和布局
设计--分类汇总--在组的底部显示所有分类汇总 把二级分类单独放在一类中 设计--报表布局--以表格形式显示 快速调整表格的外观 分析--+/-按钮 设置字段的数字格式以万元为单位 选中任 ...
- 学习java 7.24
学习内容: Swing编程 由于Swing的所有组件完全采用Java 实现,不再调用本地平台的GUl,所以导致Swing图形界面的显示速度要比AWT图形界面的显示速度慢一些,但相对于快速发展的硬件设施 ...
- absent, absolute, absorb
absent Absenteeism is a habitual [习惯性的] pattern of absence from a duty or obligation [职责] without go ...
- 移动开发之h5学习大纲
移动开发学习形式:授课.自学 1.html5 css3 htm5shiv.js response.js 2.流式布局 自适应布局 盒模型 弹性盒模型 响应式布局3.iscroll swiper boo ...
- C++自定义字符串类
//header.h #ifndef _HEADER_H #define _HEADER_H #define defaultSize 128 #include<iostream> #inc ...
- Shell学习(二)——变量和基本数据类型
参考博客: [1]LinuxShell脚本--变量和数据类型 [2]shell只读变量删除 一.变量 定义变量的语法 定义变量时,变量名和变量值之间使用"="分隔,并且等号两边不能 ...
- What all is inherited from parent class in C++?
派生类可以从基类中继承: (1)基类中定义的每个数据成员(尽管这些数据成员在派生类中不一定可以被访问): (2)基类中的每个普通成员函数(尽管这些成员函数在派生类中不一定可以被访问): (3)The ...
- android 调用相机拍照及相册
调用系统相机拍照: private Button btnDyxj; private ImageView img1; private File tempFile; btnDyxj = (Button) ...
- jQuery全局进行方法扩展
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>01 ...
- Java SPI机制,你了解过吗?
Life moves pretty fast,if you don't stop and look around once in a while,you will miss it 为什么需要SPI? ...