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. MVC FormCollection 无法获取值的问题

     把action定义为[HttpPost],并且ajax.beginform中ajaxoption中定义为Post,在提交表单时就可以获取FormCollection的值了.httpGet或者后台不定 ...

  2. Macbook pro睡眠状态恢复后没声音的解决办法

    杀招: sudo killall coreaudiod macos会自动重启进程,恢复声音

  3. 注册带有Portal功能的DYN365_ENTERPRISE_PLAN1地址

    使用官方进入的注册页面注册后试用,发现没有Portal功能. https://trials.dynamics.com/Dynamics365/Signup 使用以下的地址注册可以产生Portal ht ...

  4. xtrabackup拷贝redolog前做的细节操作

    原文地址:http://www.innomysql.net/article/25590.html 前言 淘宝3月的数据库内核月报对 xtrabackup的备份原理 做了深入的分析,写的还是很不错.不过 ...

  5. Js中的this关键字(吉木自学)

    研究生毕业答辩完,开始继续为转行努力.小白要奋斗了,加油.本文引自JS核心系列:浅谈函数的作用域. 在一个函数中,this总是指向当前函数的所有者对象,this总是在运行时才能确定其具体的指向, 也才 ...

  6. vue-route 路由传参的使用

    1 router/index.js 中的定义 { path: '/product', component: ProductIndex, meta: { requiredAuth: true, }}, ...

  7. Git 初始状操作指引

    You have an empty repository To get started you will need to run these commands in your terminal. Ne ...

  8. process_进程池

    from multiprocessing import Poolimport os,timeimport multiprocessingdef func(msg): print("msg:& ...

  9. Java 中>>和>>>的区别

    Java 中>>和>>>的区别 Java中的位运算符: >>表示右移,如果该数为正,则高位补0,若为负数,则高位补1: >>>表示无符号右移 ...

  10. 36 The Benefits of Marriage 结婚的益处

    36 The Benefits of Marriage 结婚的益处 ①Being sociable looks like a good way to add years to your life.Re ...