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

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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 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 ...

  4. Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)

    Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...

  5. 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 ...

  6. [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 ...

  7. 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 ...

  8. [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 ...

  9. 【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 结构记录数组中每个元素 ...

随机推荐

  1. 【机器学习_8】pandas

    背景 关于同一个话题,不同作者也有不同行文结构.但要真正理解并会用,在我的经验里,是必须要自己重新组织的. 本文是基于以往看过的资料,从自身数据处理应用的角度出发,重新组织pandas应用结构,希望能 ...

  2. java Page分页显示

    //entity层实体类 import java.util.List; //分页展示 //相关属性:当前页,页大小(每页显示的条数),总页数,总条数,数据 //select * from t_user ...

  3. Springboot admin 发送邮件失败:com.sun.mail.smtp.SMTPSenderFailedException: 553 Mail from must equal authorized user

    发邮件已经是老生常谈了,今天又遇到了,而且又出了各种问题.我晕哦. 我的配置是: spring.mail.host=smtp..com spring.mail.username=klxxxx spri ...

  4. verilog 介绍

    Verilog HDL Verilog HDL是在C语言的基础上发展起来的一种硬件描述语言,语法较自由.VHDL和Verilog HDL两者相比,VHDL的书写规则比Verilog HDL烦琐一些,但 ...

  5. Android APK反编译(二)

    参考:APK反编译 工具介绍 apktool 作用:资源文件获取,可以提取出图片文件和布局文件进行使用查看 dex2jar 作用:将apk反编译成java源码(classes.dex转化成jar文件) ...

  6. python学习笔记----正则表达式

    正则: regular expression 常用的场景: #正则的包 >>> import re #match:开头匹配,匹配到,返回一个匹配对象,否则返回None >> ...

  7. 把一个syn报文给rst掉

    下面展示一个极其简单的例子,看如何使用netfilter来将一个指定端口的syn报文给rst掉. //************************************************* ...

  8. Windows 7升级1月更新汇总后导致SMBv2网络无法正常工作

    在本月的补丁星期二活动日中,微软面向Windows 7.Windows 2008 R2服务器系统推出了KB4480970的月度更新汇总.然而根据部分用户反馈,在安装该更新之后导致系统的网络设置无法正常 ...

  9. CPU TFLOPS 计算

    CPU TFLOPS 计算 姚伟峰 yaoweifeng0301@126.com] http://www.cnblogs.com/Matrix_Yao/ 深度学习任务是一个计算密集型任务,所以很关注计 ...

  10. [Ting's笔记Day9]活用套件Carrierwave gem:(4)使用Imagemagick修改图片大小

    前情提要: 这几天我都在实验Carrierwave这套图片上传套件,也顺利部署到Heroku架站正式环境了.:) 接下来我遇到了新的问题:要如何在上传的时候,让Carrierwave gem大型siz ...