[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 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
这道题给了一堆牌,问我们能不能将这副牌分成若干堆,每堆均有X个,且每堆的牌数字都相同(这里不考虑花色)。既然要将相同的牌归类,肯定要统计每种牌出现的个数,所以使用一个 HashMap 来建立牌跟其出现次数之间的映射。由于每堆X个,则若果某张牌的个数小于X,则肯定无法分,所以X的范围是可以确定的,为 [2, mn],其中 mn 是数量最少的牌的个数。遍历一遍 HashMap,找出最小的映射值 mn,若 mn 小于2,可以直接返回 false。否则就从2遍历到 mn,依次来检验候选值X。检验的方法是看其他每种牌的个数是否能整除候选值X,不一定非要相等,比如 [1, 1, 2, 2, 2, 2], K=2 时就可以分为三堆 [1, 1], [2, 2], [2, 2],即相同的牌也可以分到其他堆里,所以只要每种牌的个数能整除X即可,一旦有牌数不能整除X了,则当前X一定不行,还得继续检验下一个X值;若所有牌数都能整除X,可以返回 true。循环结束后返回 false,参见代码如下:
解法一:
class Solution {
public:
bool hasGroupsSizeX(vector<int>& deck) {
unordered_map<int, int> cardCnt;
for (int card : deck) ++cardCnt[card];
int mn = INT_MAX;
for (auto &a : cardCnt) mn = min(mn, a.second);
if (mn < 2) return false;
for (int i = 2; i <= mn; ++i) {
bool success = true;
for (auto &a : cardCnt) {
if (a.second % i != 0) {
success = false;
break;
}
}
if (success) return true;
}
return false;
}
};
上面的解法是博主自己的解法,论坛上好多人使用了一个基于最大公约数 Greatest Common Divisor 的解法,写起来很简洁,但需要记住最大公约函数的写法,或者直接使用内置的 gcd 函数(感觉有点耍赖哈~)。其实原理都差不多,这里是找每种牌数之间的最大公约数,只要这个 gcd 是大于1的,就表示可以找到符合题意的X,参见代码如下:
解法二:
class Solution {
public:
bool hasGroupsSizeX(vector<int>& deck) {
unordered_map<int, int> cardCnt;
for (int card : deck) ++cardCnt[card];
int res = 0;
for (auto &a : cardCnt) {
res = gcd(a.second, res);
}
return res > 1;
}
int gcd(int a, int b) {
return a == 0 ? b : gcd(b % a, a);
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/914
参考资料:
https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 914. X of a Kind in a Deck of Cards 一副牌中的X的更多相关文章
- [leetcode]914. X of a Kind in a Deck of Cards (easy)
原题 题目原意可转换为 两组有大于等于2的公因数 /** * @param {number[]} deck * @return {boolean} */ var hasGroupsSizeX = fu ...
- LeetCode.914-一副牌中的X(X of a Kind in a Deck of Cards)
这是悦乐书的第352次更新,第377篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第214题(顺位题号是914).在一副牌中,每张牌上都写有一个整数. 当且仅当您可以选择 ...
- 【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】914. X of a Kind in a Deck of Cards 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 最大公约数 日期 题目地址: 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] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- LeetCode - 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每天一题】Search in Rotated Sorted Array(在旋转数组中搜索)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., ...
随机推荐
- Allure自动化测试报告我是这样用的
关于自动化测试报告: 之前用过testNG自带的测试报告.优化过reportNG的测试报告.extentreport.Zreport(大飞总原创),这些是我之前都用过的,也是在去年雯姐和我说过Allu ...
- Nginx Cache-Control
转自:https://www.cnblogs.com/sfnz/p/5383647.html HTTP协议的Cache-Control指定请求和响应遵循的缓存机制.在请求消息或响应消息中设置 Cach ...
- python asyncio 获取协程返回值和使用callback
1. 获取协程返回值,实质就是future中的task import asyncioimport timeasync def get_html(url): print("start get ...
- 什么是JavaBean?
什么是JavaBean? 首先明确的是JavaBean是一种Java类,而且是一种特殊的.可重用的类. 必须具有无参数的构造器,所有的属性都是private的,通过提供setter和getter方法来 ...
- Visual Studio Code 小记
1. 改变语言 如图: 2. 设置皮肤 如图: 3. Visual Studio Code关闭右侧预览功能 4. 关闭预览模式 5. VS Code 优秀的主题 a. Atom One Dark Th ...
- Winform 美化
首先,我们先来实现主界面的扁平化 此处分为两个步骤,第一步是更改winform自带的MainForm窗体属性,第二步是添加窗体事件. 将主窗体FormBorderStyle更改为None,这样就得到了 ...
- C# WinForm自定义通用分页控件
大家好,前几天因工作需要要开发一个基于WinForm的小程序.其中要用到分页,最开始的想法找个第三方的dll用一下,但是后来想了想觉得不如自己写一个玩一下 之前的web开发中有各式各样的列表组件基本都 ...
- linux安装和使用zookeeper
一.安装条件 想要安装zookeeper,必须先在linux中安装好jdk.安装步骤见: https://www.cnblogs.com/expiator/p/9987351.html 二.下载并解压 ...
- SQLAlchemy多表操作
目录 SQLAlchemy多表操作 一对多 数据准备 具体操作 多对多 数据准备 操作 其它 SQLAlchemy多表操作 一对多 数据准备 models.py from sqlalchemy.ext ...
- 一款好用的SSH工具“FinalShell”
FinalShell是一体化的的服务器,网络管理软件,不仅是ssh客户端,还是功能强大的开发,运维工具,充分满足开发,运维需求. 特色功能:免费海外服务器远程桌面加速,ssh加速,双边tcp加速,内网 ...