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 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
这道题仔细分析之后会得到X 一定得能被deck.length整除,也一定得被每个元素出现的次数整除,才能return true
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
if(deck == null || deck.length < 2){
return false;
}
int min = Integer.MAX_VALUE;
Map<Integer, Integer> map = new HashMap<>();
for(int num : deck){
map.put(num, map.getOrDefault(num,0)+1);
}
for(int x = 2 ; x<=deck.length; x++){
if(deck.length % x != 0){
continue;
}
int count = 0;
for(int key : map.keySet()){
count++;
if(map.get(key) % x != 0){
break;
}
if(count == map.size()){
return true;
}
}
}
return false;
}
}
LeetCode - X of a Kind in a Deck of Cards的更多相关文章
- [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:// ...
- [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 ...
- 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 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 ...
- 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 ...
- Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)
Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...
- [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 ...
随机推荐
- 编码原则 之 Explicit Dependencies Principle
Explicit Dependencies Principle The Explicit Dependencies Principle states: Methods and classes shou ...
- linux使用代理进行apt安装 以 nord 为例
我的环境:(不必完全一样,只是提一下)----------- linux系统:kali 桌面:xface ----------------------------------------------- ...
- gym 101081 gym F. Auction of Services 最小生成树+倍增LCA
F. Auction of Services time limit per test 2.0 s memory limit per test 256 MB input standard input o ...
- Lab 7-1
Analyze the malware found in the file Lab07-01.exe. Questions and Short Answers How does this progra ...
- Docker Image管理学习笔记,ZT
Docker Image管理学习笔记 http://blog.csdn.net/junjun16818/article/details/38423391
- Dedecms V5.7 关于session
在dedecmsv5.7里面使用session的话要注意开启方式!和PHP源码里的使用方式不一样!!! 开启session,前面必须要@ @session_start(); 启动session,前面必 ...
- 解决安卓UI刷新卡屏,只显示最后一处刷新的问题
---恢复内容开始--- 错误1 安卓的机制决定了只有UI线程(也就是主线程)才能更新UI界面 否则会导致UI界面混乱的问题 这就说明了在new Thread中直接showImage是会报“出现非主线 ...
- PHP文件PHP代码及运行(适合PHP初学者)
本文转自:https://blog.csdn.net/cnds123/article/details/80700444 如果在warmpserver上运行php只显示源代码,可能是在用记事本保存后缀为 ...
- linux 基础命令,未完待续
1, cd 进入系统根目录 cd / 进入当前用户的主目录 cd ~ 进入当前目录的上一级目录 cd .. 跳转到指定目录,从根目录开始 cd /apps/ 2, pwd 查看当前工作目录的完整路径 ...
- Win10系列:C#应用控件基础11
RichEditBox控件 富文本格式是一种跨平台的文档格式,在这种格式的文档中可以编辑文本.图片.链接等内容.通过RichEditBox控件可以对富文本格式的文档进行编辑. 在XAML文件中,Ric ...