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
  3. Explanation: No possible partition.

Example 3:

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

Example 4:

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

Example 5:

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

Note:

  1. 1 <= deck.length <= 10000
  2. 0 <= deck[i] < 10000

Approach #1: Array + GCD. [Java]

  1. class Solution {
  2. public boolean hasGroupsSizeX(int[] deck) {
  3. int res = 0;
  4. Map<Integer, Integer> map = new HashMap<>();
  5. for (int d : deck) map.put(d, map.getOrDefault(d, 0) + 1);
  6. for (int m : map.values()) res = gcd(res, m);
  7. return res > 1;
  8. }
  9. public int gcd(int x, int y) {
  10. return y > 0 ? gcd(y, x % y) : x;
  11. }
  12. }

  

Reference:

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

914. X of a Kind in a Deck of Cards的更多相关文章

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

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

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

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

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

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

  5. codeforces 744C Hongcow Buys a Deck of Cards

    C. Hongcow Buys a Deck of Cards time limit per test 2 seconds memory limit per test 256 megabytes in ...

  6. [Swift]LeetCode914.一副牌中的X | 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 ...

  7. X of a Kind in a Deck of Cards LT914

    In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ...

  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. Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)

    Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...

随机推荐

  1. MVC加载部分视图Partial

    加载部分视图的方法:Partial() .RenderPartial() . Action() .RenderAction() . RenderPage() partial 与 RenderParti ...

  2. js验证input输入正整数 和 输入的金额小数点后保留两位(PC端键盘输入)

    // 验证开头不为零的正整数 WST.zhengZhengShuIn = function (className){ var rex = /^[1-9]{1}[0-9]*$/;//正整数 $(&quo ...

  3. sql ltrim/rtrim 字段中为中文时出现?的问题

    字段存储为中文,类型为nvarchar,使用ltrim时结果集中出现的问号,我的解决办法是:将问号replace掉

  4. 想要打动HR的心,UX设计师求职信究竟应该怎么写?

    在努力准备申请一份UX设计师职位时,你最烦心和担忧的事哪一个环节?是写一份UX设计师简历?回答面试官的问题?还是在一遍遍的煎熬中等待一个面试电话?是的,这些都是不轻松的事儿,但还有一个同样糟心的事,那 ...

  5. 检测空值,以及会不会出现mapping类型不一致的问题

    /// <summary> /// 检测空值,以及会不会出现mapping类型不一致的问题 /// </summary> /// <typeparam name=&quo ...

  6. 【JS】 伪主动触发input:file的click事件

    大家用到input:file标签时,对于input:file的样式难看的处理方法一般有2种: 采用透明化input:file标签的方法,上面放的input:file标签,下面放的是其他标签,实际点击的 ...

  7. scrapy windows 安装

    windows 7 系统下参照官网安装总是会提示出错,现在整理一下安装的流程 1.安装 python 2.7,添加环境变量 C:\Python27\;C:\Python27\Scripts\; 在 C ...

  8. Python安装setuptools遇到的MARKER_EXPR错误

    # python setup.py install Traceback (most recent call last):   File "setup.py", line 11, i ...

  9. Android中的Service 与 Thread 的区别[转]

    很多时候,你可能会问,为什么要用 Service,而不用 Thread 呢,因为用 Thread 是很方便的,比起 Service 也方便多了,下面我详细的来解释一下. 1). Thread:Thre ...

  10. 使用Revel(go)开发网站

    Revel很好的利用了Go语言的goroutine,把每一个request都分配到了goroutine里.不用再写一大堆的回调.如果你写过nodejs的话就会深刻的体会到callback hell是什 ...