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 ...
随机推荐
- Leetcode480-Binary Tree Paths-Easy
480. Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Example Example 1: Input: ...
- Qt获取选择的文件夹和文件路径
获取文件夹路径 static QString getExistingDirectory(QWidget *parent = Q_NULLPTR, const QString &caption ...
- 巧用同步请求处理react页面刷新问题
很多时候,我们会遇到这种情况,组件加载需要请求后台数据,然后填充组件.那么我们一般会这样处理:如[使用异步请求的方式]代码: 加载组价的时候,未获得数据,render一个空的div: 然后异步请求数据 ...
- List 常用方法
List 计算集合中某属性值的总和 list.Sum(ins=>ins.Field); Sort()方法,摘要:使用默认比较器对整个 System.Collections.Generic.Lis ...
- 『TensorFlow』张量拼接_调整维度_切片
1.tf.concat tf.concat的作用主要是将向量按指定维连起来,其余维度不变:而1.0版本以后,函数的用法变成: t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, ...
- openLDAP安装时无法操作根节点数据,提示的是This base cannot be created with PLA.
1.无法操作根节点数据,提示的是This base cannot be created with PLA. 解决办法 1)添加一个base.ldif文件,里面的dc和配置文件里的保持一致即可 dn: ...
- (译)xDS REST and gRPC protocol
xDS REST and gRPC protocol 原文地址:xDS REST and gRPC protocol. envoy可通过文件系统.一个或多个管理服务器来发现各种动态资源.这些服务发现和 ...
- IE9下table th不显示边框解决方法
添加下面两行样式即可 th, td{ background-clip: padding-box; /*背景被裁剪到内边距框*/ position:relative; }
- Python3版本中的filter函数,map函数和reduce函数
一.filter函数: filter()为已知的序列的每个元素调用给定的布尔函数,调用中,返回值为非零的元素将被添加至一个列表中 def f1(x): if x>20: return True ...
- 移动应用调试之Inspect远程调试
移动应用调试之Inspect远程调试 一.准备工作 chrome浏览器,建议最新版本 如果你点击inspect打开的DevTools窗口一片空白,且刷新无效时,那极有可能是由于被墙的缘故. 二.Ins ...