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:

  1. Input: [1,2,3,4,4,3,2,1]
  2. Output: true
  3. Explanation: Possible partition [1,1],[2,2],[3,3],[4,4]

Example 2:

  1. Input: [1,1,1,2,2,2,3,3]
  2. Output: false Explanation: No possible partition.

Example 3:

  1. Input: [1]
  2. Output: false Explanation: No possible partition.

Example 4:

  1. Input: [1,1]
  2. Output: true Explanation: Possible partition [1,1]

Example 5:

  1. Input: [1,1,2,2,2,2]
  2. Output: true Explanation: Possible partition [1,1],[2,2],[2,2]

Note:

  1. 1 <= deck.length <= 10000
  2. 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,参见代码如下:


解法一:

  1. class Solution {
  2. public:
  3. bool hasGroupsSizeX(vector<int>& deck) {
  4. unordered_map<int, int> cardCnt;
  5. for (int card : deck) ++cardCnt[card];
  6. int mn = INT_MAX;
  7. for (auto &a : cardCnt) mn = min(mn, a.second);
  8. if (mn < 2) return false;
  9. for (int i = 2; i <= mn; ++i) {
  10. bool success = true;
  11. for (auto &a : cardCnt) {
  12. if (a.second % i != 0) {
  13. success = false;
  14. break;
  15. }
  16. }
  17. if (success) return true;
  18. }
  19. return false;
  20. }
  21. };

上面的解法是博主自己的解法,论坛上好多人使用了一个基于最大公约数 Greatest Common Divisor 的解法,写起来很简洁,但需要记住最大公约函数的写法,或者直接使用内置的 gcd 函数(感觉有点耍赖哈~)。其实原理都差不多,这里是找每种牌数之间的最大公约数,只要这个 gcd 是大于1的,就表示可以找到符合题意的X,参见代码如下:


解法二:

  1. class Solution {
  2. public:
  3. bool hasGroupsSizeX(vector<int>& deck) {
  4. unordered_map<int, int> cardCnt;
  5. for (int card : deck) ++cardCnt[card];
  6. int res = 0;
  7. for (auto &a : cardCnt) {
  8. res = gcd(a.second, res);
  9. }
  10. return res > 1;
  11. }
  12. int gcd(int a, int b) {
  13. return a == 0 ? b : gcd(b % a, a);
  14. }
  15. };

Github 同步地址:

https://github.com/grandyang/leetcode/issues/914

参考资料:

https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/

https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/discuss/175845/C%2B%2BJavaPython-Greatest-Common-Divisor

https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/discuss/309992/Java-Easy-to-understand-2-ms-faster-than-98.92-38.5-MB-less-than-99.23

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 914. X of a Kind in a Deck of Cards 一副牌中的X的更多相关文章

  1. [leetcode]914. X of a Kind in a Deck of Cards (easy)

    原题 题目原意可转换为 两组有大于等于2的公因数 /** * @param {number[]} deck * @return {boolean} */ var hasGroupsSizeX = fu ...

  2. LeetCode.914-一副牌中的X(X of a Kind in a Deck of Cards)

    这是悦乐书的第352次更新,第377篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第214题(顺位题号是914).在一副牌中,每张牌上都写有一个整数. 当且仅当您可以选择 ...

  3. 【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 结构记录数组中每个元素 ...

  4. 【LeetCode】914. X of a Kind in a Deck of Cards 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 最大公约数 日期 题目地址: https:// ...

  5. 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 ...

  6. [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 ...

  7. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  8. 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 ...

  9. 【LeetCode每天一题】Search in Rotated Sorted Array(在旋转数组中搜索)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., ...

随机推荐

  1. Vue.js 源码分析(十七) 指令篇 v-if、v-else-if和v-else 指令详解

    v-if 指令用于条件性地渲染一块内容.这块内容只会在指令的表达式返回true值的时候被渲染. v-else-if,顾名思义,充当 v-if 的“else-if 块”,可以连续使用: 也可以使用 v- ...

  2. 知识图谱与Bert结合

    论文题目: ERNIE: Enhanced Language Representation with Informative Entities(THU/ACL2019) 本文的工作也是属于对BERT锦 ...

  3. 转 推荐 33 个 IDEA 最牛配置,写代码太爽了!

    本文由 简悦 SimpRead 转码, 原文地址 https://mp.weixin.qq.com/s/neyvJouuG1Rmxn3BwfRXVg 作者:琦彦 blog.csdn.net/fly91 ...

  4. WebGIS之MapBox篇

    前面在Arcgis的基础上玩了玩,这不最近又去摸索了一下Web上开源的GIS;这次选择了基于MapBox来实现一些效果: 1.加载自己发布的本地瓦片效果 2.加载热力图.Echarts.三位建筑.路况 ...

  5. C#通过PInvoke调用c++函数的备忘录

    目前知道的情况被调用的C/C++函数只能是全局函数 不能调用类中的成员方法 被调用的C函数必须使用extern “C“包含,保证采用的导出函数名生成规则和.NET一致 函数调用约定通常使用WINAPI ...

  6. Java学习——包装类

    Java学习——包装类 摘要:本文主要介绍了Java中常用的包装类和基本类型之间的转换,包装类或基本类型和String之间的转换. 部分内容来自以下博客: https://www.cnblogs.co ...

  7. 在React中使用react-router-dom路由

    1,路由组件的基本实现 使用React构建的单页面应用,要想实现页面间的跳转,首先想到的就是使用路由.在React中,常用的有两个包可以实现这个需求,那就是react-router和react-rou ...

  8. 一句DELETE引发的加班(Mysql 恢复Delete删除的数据)

    本机用的Navicat连mysql测试DB又连了正式DB,因为本地与正式要频繁操作所以都打开了很多查询,本来要DELETE删除测试DB的数据,没看清在正式环境执行了.共删除了325条数据,然后在网上找 ...

  9. page的js访问全局变量:app.globalData.openid

    page获取app.js:const app = getApp(); page的js访问全局变量(get/set):const app = getApp(); app.globalData.openi ...

  10. vue学习指南:第九篇(详细) - Vue的 Slot-插槽

    Slot  v-slot 插槽元素 浏览器在解析时候首先把它当作标签来解析,只有遇到不认识的就不管了,直接跳过,当你发现是组件,在以组件形式解析. 使用插槽的好处? 比如一个网站 分布顶部都是一样的, ...