【LeetCode】914. X of a Kind in a Deck of Cards 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/description/
题目描述
In a deck of cards, each card has an integer written on it.
Return true if and only if you can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where:
Each group has exactly X cards.
All the cards in each group have the same integer.
Example 1:
Input: [1,2,3,4,4,3,2,1]
Output: true
Explanation: Possible partition [1,1],[2,2],[3,3],[4,4]
Example 2:
Input: [1,1,1,2,2,2,3,3]
Output: false
Explanation: No possible partition.
Example 3:
Input: [1]
Output: false
Explanation: No possible partition.
Example 4:
Input: [1,1]
Output: true
Explanation: Possible partition [1,1]
Example 5:
Input: [1,1,2,2,2,2]
Output: true
Explanation: Possible partition [1,1],[2,2],[2,2]
Note:
- 1 <= deck.length <= 10000
- 0 <= deck[i] < 10000
题目大意
判断一堆牌能不能分成很多组,每个组是相同的元素,并且每个组最少两张牌。
解题方法
遍历
果然是个Easy的题目,很简单。
每个组的元素都是相同的情况下,分组之后每个组有多少个元素呢?
首先,要求每个组的元素都是相同的,因此元素个数最小的那个组限制了每个组的个数。
但不一定是所有元素个数的最小值,因为相同的元素可以分成很多组的,比如这个测试用例,每个组可以都是两个元素即可。
[1,1,1,1,2,2,2,2,2,2]
所以,可以用一个遍历,从2遍历到最少次数的数字
中的元素个数。道理是,把最少次数在划分和不划分的情况下,看其他组能不能按照这个个数进行均分。
最坏情况下的时间复杂度是O(N^2),空间复杂度是O(N)。
Python代码如下:
class Solution(object):
def hasGroupsSizeX(self, deck):
"""
:type deck: List[int]
:rtype: bool
"""
count = collections.Counter(deck)
X = min(count.values())
for x in range(2, X + 1):
if all(v % x == 0 for v in count.values()):
return True
return False
最大公约数
二刷的时候想到了最大公约数解法。
最终分的组的大小是多少,就是每个数字的次数的最大公约数。
比如例子:
[1,1,2,2,2,2]
最终分成了[1,1],[2,2],[2,2]
,每个组是2个数字,怎么得来的?看1
出现了2次,看2
出现了4次,最终的结果就是2和4的最大公约数2。
公约数能被所有的数字个数整除,所以最大的那个公约数,就是能被所有数字个数整除的最大组大小。
C++代码如下:
class Solution {
public:
bool hasGroupsSizeX(vector<int>& deck) {
unordered_map<int, int> counter;
for (int d : deck) {
counter[d] ++;
}
int res = 0;
for (auto& c : counter) {
res = gcd(c.second, res);
}
return res > 1;
}
int gcd(int x, int y) {
return y == 0 ? x : gcd(y, x % y);
}
};
日期
2018 年 9 月 30 日 —— 9月最后一天啦!
2018 年 11 月 24 日 —— 周日开始!一周就过去了~
2020 年 3 月 27 日 —— 开始整合资源
【LeetCode】914. X of a Kind in a Deck of Cards 解题报告(Python & C++)的更多相关文章
- [LeetCode] 914. X of a Kind in a Deck of Cards 一副牌中的X
In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ...
- [leetcode]914. X of a Kind in a Deck of Cards (easy)
原题 题目原意可转换为 两组有大于等于2的公因数 /** * @param {number[]} deck * @return {boolean} */ var hasGroupsSizeX = fu ...
- 【LeetCode】面试题62. 圆圈中最后剩下的数字 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 约瑟夫环 日期 题目地址:https://leetco ...
- 【Leetcode_easy】914. X of a Kind in a Deck of Cards
problem 914. X of a Kind in a Deck of Cards 题意:每个数字对应的数目可以均分为多组含有K个相同数目该数字的数组. 思路:使用 map 结构记录数组中每个元素 ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 914. X of a Kind in a Deck of Cards
In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ...
- 【LeetCode】1160. Find Words That Can Be Formed by Characters 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 日期 题目地址:https://leetco ...
- 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 preSum + 字典 日期 题目地址:https:/ ...
随机推荐
- ggplot2 图例及分页参数
图例: 1 theme(legend.title =element_blank()) 2 guides(fill = guide_legend(title = NULL)) # 去掉图例title 3 ...
- 学习java 7.26
学习内容: 进度条是图形界面中广浅个较大的文件时,操作系统会显示一个进度条,用于标识复制操作完成的比例:当启动Eclipse等程序时,因为需要加载较多的资源,故而启动速度较慢,程序也会在启动过程中显示 ...
- 面向多场景而设计的 Erda Pipeline
作者|林俊(万念) 来源|尔达 Erda 公众号 Erda Pipeline 是端点自研.用 Go 编写的一款企业级流水线服务.截至目前,已经为众多行业头部客户提供交付和稳定的服务. 为什么我们坚持自 ...
- Kafka入门教程(一)
转自:https://blog.csdn.net/yuan_xw/article/details/51210954 1 Kafka入门教程 1.1 消息队列(Message Queue) Messag ...
- 打破砂锅问到底!HTTP和HTTPS详解
HTTP 引自维基百科HTTP:超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的数 ...
- 【Reverse】每日必逆0x02
BUU SimpleRev 附件 https://files.buuoj.cn/files/7458c5c0ce999ac491df13cf7a7ed9f1/SimpleRev 题解 查壳 拖入iad ...
- 转 关于HttpClient,HttpURLConnection,OkHttp的用法
转自:https://www.cnblogs.com/zp-uestc/p/10371012.html 1 HttpClient入门实例 1.1发送get请求 1 2 3 4 5 6 7 8 9 10 ...
- MyBatis(1):实现MyBatis程序
一,MyBatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- 3.使用Spring Data ElasticSearch操作ElasticSearch(5.6.8版本)
1.引入maven坐标 <!--spring-data-elasticsearch--><dependency> <groupId>org.springframew ...
- JSP常用内置对象
1.request 1.1getAttribute(String name) 2.getAttributeName() 3.getCookies() 4.getCharacterEncoding() ...