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 chooseX >= 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 <= deck.length <= 10000
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的更多相关文章
- 【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, 没有想到把省下的红色开成一维. ...
随机推荐
- nginx中图片无法显示
如果没有配置虚拟主机,则修改nginx.conf. 如果已创建单独虚拟主机,则在vhost下找到指定的主机配置文件, 如:www.xxx.com.conf location ~ .*\.(gif|jp ...
- Codeforces 677C. Coloring Trees dp
C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- jquery阻止表单提交
<form action="" method="post" onSubmit="return confirm();" > < ...
- Spring 学习记录3 ConversionService
ConversionService与Environment的关系 通过之前的学习(Spring 学习记录2 Environment),我已经Environment主要是负责解析properties和p ...
- part1:7-Linux网络配置
1.虚拟机(Vmware)网络配置 VMware虚拟机对于不同的网络环境提供了三种网卡工作模式: Bridged:网桥模式: 在桥接模式下,计算机A充当路由器与虚拟机之间的“桥”,虚拟机通过计算机A的 ...
- 【Unity】2.3 项目浏览器和资源的组织
分类:Unity.C#.VS2015 创建日期:2016-03-29 一.Project-工程浏览器 前面我们说过,Unity中的Project View(称为工程浏览器)相当于VS2015中的解决方 ...
- IntelliJ IDEA 2017版 springloaded实现热部署
1.配置pom.xml文档 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- 给Notepad++换主题
Notepad++是一款不错的编辑器,很轻巧,我很喜欢它.再换个主题,加个代码高亮,看上去就更专业了.如果你是Mac用户,应该听说或使用过Textmate(什么?没听过,那你该补课了!),Textma ...
- 13.A={1,2,3,5}和为10的问题
题目:集合A={1,2,3,5},从中任取几个数相加等于10,并打印各得哪几个数?补充参照:http://www.cnblogs.com/tinaluo/p/5294341.html上午弄明白了幂集的 ...
- 自己动手绘制ToolBar
VC MFC工具栏(CToolBar)控件 工具栏 工具栏控件在控件面板里没有对应的选项(图标),但有一个工具栏控件类CToolBar,所以我们如果要创建一个工具栏控件并显示在窗口里的话,只能用代码来 ...