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 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
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
Idea 1. count the occurences of each number in the deck and check if the greatest common divisor of all counts pair > 1
Time complexity: O(Nlog^2N), where N is the number of cards. gcd operation is O(log^2C) if there are C cards for number i. need to read further about it.
Space complexity: O(N)
class Solution {
int gcd(int a, int b) {
while(b != 0) {
int temp = b;
b = a%b;
a = temp;
}
return a;
}
public boolean hasGroupsSizeX(int[] deck) {
if(deck.length < 2) {
return false;
}
Map<Integer, Integer> intCnt = new HashMap<>();
for(int num: deck) {
intCnt.put(num, intCnt.getOrDefault(num, 0) + 1);
}
int preVal = -1;
for(int val: intCnt.values()) {
if(val == 1) {
return false;
}
if(preVal == -1) {
preVal = val;
}
else {
preVal = gcd(preVal, val);
if(preVal == 1) {
return false;
}
}
}
return preVal >= 2;
}
}
网上看到的超级简洁,自己的差好远,还有很长的路啊
class Solution {
int gcd(int a, int b) {
while(b != 0) {
int temp = b;
b = a%b;
a = temp;
}
return a;
}
public boolean hasGroupsSizeX(int[] deck) {
Map<Integer, Integer> intCnt = new HashMap<>();
for(int num: deck) {
intCnt.put(num, intCnt.getOrDefault(num, 0) + 1);
}
int res = 0;
for(int val: intCnt.values()) {
res = gcd(val, res);
}
return res >= 2;
}
}
X of a Kind in a Deck of Cards LT914的更多相关文章
- 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 ...
- 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, 没有想到把省下的红色开成一维. ...
- 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 choos ...
- [leetcode-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 choos ...
- Codeforces Round #385 (Div. 1) C. Hongcow Buys a Deck of Cards
地址:http://codeforces.com/problemset/problem/744/C 题目: C. Hongcow Buys a Deck of Cards time limit per ...
- [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_easy】914. X of a Kind in a Deck of Cards
problem 914. X of a Kind in a Deck of Cards 题意:每个数字对应的数目可以均分为多组含有K个相同数目该数字的数组. 思路:使用 map 结构记录数组中每个元素 ...
随机推荐
- Opencv与Qt (一)之运行测试读取图片
刚刚在vs上装好了QT和Opencv,试一下效果把. 我简单的创建了一个label,然后使用Opencv导入图像,因为Opencv导入图像是MAT格式的,在使用Qt的时候我们要把导入的图像转换成Qim ...
- 201772020113 李清华《面向对象程序设计(java)》第17周学习总结
1.实验目的与要求 (1) 掌握线程同步的概念及实现技术: (2) 线程综合编程练习 2.实验内容和步骤 实验1:测试程序并进行代码注释. 测试程序1: l 在Elipse环境下调试教材651页程序 ...
- Gitlab使用Webhook实现Push代码后的jenkins自动构建
本文出自https://www.cnblogs.com/kevingrace/p/6479813.html 怕以后找不到,所以先写到自己博客中 Gitlab利用Webhook实现Push代码后的jen ...
- Django08-批量创建数据
通过views.py文件中创建 第1种方法循环创建数据, 这种方法不推荐,因为每一次循环都会连接一次数据库,效率较慢 def user_list(request): user_all = models ...
- 给centos 7添加硬盘空间
前言 今天在安装ambari的时候,制作amberi本地yum源的时候出现了一个问题,虚拟机的磁盘空间不足了,后来发现是因为这个节点之前本来只打算作为数据节点使用的,后来所以后来只给它分配了20G的硬 ...
- cookie和session的区别及在Django中应用
Django中Cookie和session应用 什么是cookie? cookie是客户端浏览器上的一个文件,以键值对进行保存,类似于字典的 {'key' : 'value'} ,与服务器端没有关系, ...
- MySQL GTID复制Slave跳过错误事务Id以及复制排错问题总结
GTID复制典型的复制错误有两种:1,数据对象级别的错误,包括主库上update的数据在从库上不存在,主从逐渐冲突,库表索引等对象的冲突等等, 如果是纯粹的跳过错误的话,这一类的错误需要跳过思路是 ...
- js自动时间
function showTime() { //时间 nowtime = new Date(); year = nowtime.getFullYear(); month = nowtime.getMo ...
- Linux seq_printf输出内容不完整的问题
Linux seq_printf输出内容不完整的问题 写在前面的话:这是多年前在项目中遇到的问题,作为博客的开篇之作,有不足之处,请各位大侠斧正!谢谢! seq_file接口介绍 有许多种方法能够实现 ...
- oslo_db使用
oslo_db是openstak中封装数据库访问sqlachmy的模块,网上搜索的资源并不多,除了openstack官方文档,在实际使用中的例子凤毛麟角. 有感于资源太少,在学习heat源码的过程中, ...