914. X of a Kind in a Deck of Cards
In a deck of cards, each card has an integer written on it.
Return
trueif and only if you can chooseX >= 2such that it is possible to split the entire deck into 1 or more groups of cards, where:
- Each group has exactly
Xcards.- 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 <= 100000 <= 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的更多相关文章
- 【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 一副牌中的X
In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ...
- 【LeetCode】914. X of a Kind in a Deck of Cards 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 最大公约数 日期 题目地址: https:// ...
- [leetcode]914. X of a Kind in a Deck of Cards (easy)
原题 题目原意可转换为 两组有大于等于2的公因数 /** * @param {number[]} deck * @return {boolean} */ var hasGroupsSizeX = fu ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)
Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...
随机推荐
- Electronic Trading[z]
This article is to discuss the operation model between Fund Managers(Client) and Broker Firms. They ...
- Golang之时间格式化,计时器
地鼠敲下一堆代码,记录着当天的时间 package main import ( "fmt" "time" ) func getTime() { now := t ...
- Shell编程实例
一.简介 从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更 ...
- Bug:src/lxml/lxml.etree.c:84:20: 致命错误:Python.h:没有那个文件或目录
问题描述: pip批量安装软件包时,出现如上题目错误,卡在了lxm依赖于python中的python-devel 问题原因: 缺失python-devel开发包所导致,python.h存在于pytho ...
- LaTeX 公式(转自)Iowa_Battleship 神犇
传送门 (我这个蒟蒻只是mark一下 这个LaTex公式很全!!我是照着打数学公式的!! orz大佬Iowa
- 设计模式-生成者模式之c#代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- hdu-2844(完全背包+二进制优化模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 思路:问1-m能的得到的硬币的值,所以dp[i]==i即可. #include<iostr ...
- yum 报错:centos yum (28, 'Connection time-out') Trying other mirror.
前言: 在使用yum安装 软件时,经常出现 centos yum (28, 'Connection time-out') Trying other mirror. 或下面的那样情况imeout on ...
- IntelliJ IDEA 2017版 编译器使用学习笔记(三) (图文详尽版);IDE快捷键使用
一.列操作 功能:操作多行列执行相同的功能,达到一次修改多行同类型数据的情况,如图: Json字符串,转为枚举类的字段: 首先进行,快捷键一行快速操作 1.选中命令,s ...
- Easy CHM使用简明教程
近日整理硬盘,发现下载有许多DOC.JPEG.HTML等 格式的学习资料,也包括一些电子书资料:而其中的DOC.HTML等资料在学习浏览时显得很不方便,不同格式的文件需要使用不同的打开方式.近而发现电 ...