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
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
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 结构记录数组中每个元素 ...
随机推荐
- leetcode每日刷题计划-简单篇day9
Num 38 报数 Count and Say 题意读起来比较费劲..看懂了题还是不难的 注意最后的长度是sz的长度,开始写错写的len 在下次计算的时候len要更新下 说明 直接让char和int进 ...
- css边框的一些属性
边框样式值如下:none : 无边框.与任何指定的border-width值无关hidden : 隐藏边框.IE不支持dotted : 在MAC平台上IE4+与WINDOWS和UNIX平台上IE5.5 ...
- php把网络图片转Base64编码。
/** 把网络图片图片转成base64 * @param string $img 图片地址 * @return string */ /*网络图片转为base64编码*/ function imgtob ...
- MTK6261之Catcher工具的Database Path
在Catcher使用使用的时候我们常用要选择Database Path 设置数据库的路径,编译自动生成的文件: 设置路径选项: (一般是在对应工程文件的路径 \tst\database_classb) ...
- --- Android 设置为A2DP 接收器
http://www.it1352.com/88728.html external/bluetooth/bluedroid/include/bt_target.h: * Enable bluetoot ...
- 使用css设置三角形
1.在开发中,有时候会用到一些小三角形来强调或者标记元素,这样以便区分不同的项目,然后把三角形绘制成一个比较明显的颜色,就达到效果了,那怎么才能画出三角形呢,之前我也不清楚,最近看到了有些网页在使用, ...
- a 标签实现分享功能
在网页中,经常会用到分享功能,例如分享到qq,分享到微信,分享到微博等,但是怎么实现呢?一直没有想清楚这个问题,觉得好高大上的样子,于是在网上找了一些资料,也没有看出个什么所以然来: 于是有些心急了, ...
- 缓存:前端页面缓存、服务器缓存(依赖SQL)MVC3
缓存依赖数据库 第一步 1通过vs里面带的命令提示窗口. 2或者.NET Framework 版本 4(64 位系统)条件,%windir%\Microsoft.NET\Framework64\v4. ...
- jQuery 效果 - 动画 animate() 方法
我们先看一个demo <!DOCTYPE html> <html> <head> <script src="/jquery/jquery-1.11. ...
- 2017-11-11 Sa Oct Is it online
2017-11-11 Sa Oct Is it online 9:07 AM After breakfast I tried connecting to the course selection sy ...