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 结构记录数组中每个元素 ...
随机推荐
- 搭建IntelliJ IDEA授权服务器
地址:https://blog.csdn.net/maozexijr/article/details/79072287 https://www.jianshu.com/p/754d8f907f2 ...
- Lightgbm 随笔
lightGBM LightGBM 是一个梯度 boosting 框架,使用基于学习算法的决策树.它可以说是分布式的,高效的,有以下优势: 更快的训练效率 低内存使用 更高的准确率 支持并行化学习 可 ...
- 我的coding地址
https://dev.tencent.com/u/dtid_d6b0780bdefc3f9c/follower#1
- 循环队列搜索 Search in Rotated Sorted Array
这里比较重要的是,不要一上来就判断mid 和 target有没有关系.因为数组是无序的,这样的判断毫无结论,只会搞的更复杂.应该先想办法判断出哪一侧是有序的. class Solution { pub ...
- 初探LaTeX
第一次使用LaTeX 步骤 1 安装LaTeX 通过官网http://www.tug.org/mactex/mactex-download.html下载Mac版LaTeX. 安装成功后会出现 步骤 ...
- 如何引入.graphql文件并优雅的使用fragment
你还在为代码中放入长长的模版字符串所苦恼吗,如下图代码片段: ps:这个是grqphql client在nodejs后端项目的实践,如果你是在前端使用graphql,并使用了webpack,那么这些问 ...
- MVC ScriptBundle自定义排序。
今天发现MVC的ScriptBundle @Scripts.Render()后是按照我也不知道顺序显示在页面上的,后果就是jquery.min.js被排在了后面(反正我下面那堆默认jquery.min ...
- python flask 解决中文乱码
response = make_response(output_string)response.headers['Content-Type'] = 'text/plain;charset=UTF-8' ...
- 软件工程小组讨论设计NABCD
项目名称:失物招领平台 项目工作小组:冰淇淋队 项目简介:目前同学们丢了东西都qq空间转发或者某个特定的qq群发消息,qq空间转发浪费了别人的时间,qq群发消息也浪费了别人的时间.怎么样才能浪费最少的 ...
- Java框架spring 学习笔记(一):SpringBean、ApplicationContext 容器、BeanFactory容器
Spring容器是Spring框架的核心,容器可以创建对象并创建的对象连接在一起,配置和管理他们的整个生命周期.Spring 容器使用依赖注入(DI)来作为管理应用程序的组件,被称为 Spring B ...