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. 1 <= deck.length <= 10000
  2. 0 <= deck[i] < 10000

Approach #1: Array + GCD. [Java]

class Solution {
public boolean hasGroupsSizeX(int[] deck) {
int res = 0;
Map<Integer, Integer> map = new HashMap<>();
for (int d : deck) map.put(d, map.getOrDefault(d, 0) + 1);
for (int m : map.values()) res = gcd(res, m);
return res > 1;
}
public int gcd(int x, int y) {
return y > 0 ? gcd(y, x % y) : x;
}
}

  

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. Halcon开发环境和数据结构介绍——第1讲

    1.Halcon是什么?如何初步了解Halcon? 这点我讲得不太好,不如给大家看看三个链接: ① Halcon官方网站:https://www.mvtec.com/products/halcon/ ...

  2. Linux升级Ruby

    一.简介 Ruby 是一种开源的面向对象程序设计的服务器端脚本语言,在 20 世纪 90 年代中期由日本的松本行弘(まつもとゆきひろ/Yukihiro Matsumoto)设计并开发.在 Ruby 社 ...

  3. HDU_5688

    /* 度熊所居住的 D 国,是一个完全尊重人权的国度.以至于这个国家的所有人命名自己的名字都非常奇怪.一个人的名字由若干个字符组成,同样的,这些字符的全排列的结果中的每一个字符串,也都是这个人的名字. ...

  4. css之颜色篇

      app多采用浅灰#f5f5f5   白色一般用white,如果觉得白太直接了,可以加一点点灰,#fefefe,   这种情况下搭配#e4e4e4的浅灰边框最合适.

  5. 前端之JavaScript笔记3

    一 创建添加节点 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  6. 2018.09.01 poj2689 Prime Distance(埃式筛法)

    传送门 一道挺有趣的. 第一眼以为每个数都用miller_rabin判一次,但感觉会被卡时间啊. 继续分析发现可以晒出sqrt(r)中的所有素数,然后用类似埃式筛法的方法晒出[l,r]" r ...

  7. 一组RS485设备操作命令

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ZNJM ...

  8. MATLAB实现截位的问题

    讨论MATLAB怎样提取10进制中的位的方法,因为做FFT时要用到截位,相去验证它,向同庆请教, 原来只是除以2的N次方,取模取余就行了,可恨我还想了一下午,也没有一个好办法. 接下来的问题是,对于负 ...

  9. jdbcmysql

    做java开发难免会用到数据库,操作数据库也是java开发的核心技术.那我们现在就来谈谈javajdbc来操作mysql数据库吧 第一步:我们需要把mysql的驱动引进来这里引驱动就是把mysql-c ...

  10. ArcGIS Desktop python Add-in 测试一个插件

    a)制作一个插件文件 先找到工作目录,双击运行makeaddin.py脚本.这个脚本拷贝所有插件需要的文件和文件夹并在工作目录形成一个压缩文件.该压缩文件名为工作目录名称加上".esriad ...