914. X of a Kind in a Deck of Cards
In a deck of cards, each card has an integer written on it.
Return
trueif and only if you can chooseX >= 2such that it is possible to split the entire deck into 1 or more groups of cards, where:
- Each group has exactly
Xcards.- 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 <= 100000 <= deck[i] < 10000
Approach #1: Array + GCD. [Java]
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
int res = 0;
Map<Integer, Integer> map = new HashMap<>();
for (int d : deck) map.put(d, map.getOrDefault(d, 0) + 1);
for (int m : map.values()) res = gcd(res, m);
return res > 1;
}
public int gcd(int x, int y) {
return y > 0 ? gcd(y, x % y) : x;
}
}
Reference:
https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/discuss/175845/C%2B%2BJavaPython-Greatest-Common-Divisor
914. X of a Kind in a Deck of Cards的更多相关文章
- 【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] 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:// ...
- [leetcode]914. X of a Kind in a Deck of Cards (easy)
原题 题目原意可转换为 两组有大于等于2的公因数 /** * @param {number[]} deck * @return {boolean} */ var hasGroupsSizeX = fu ...
- 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 ...
- 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 ...
- 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, 没有想到把省下的红色开成一维. ...
随机推荐
- PAT 1055 集体照 (25)(STL-list+代码)
1055 集体照 (25)(25 分)提问 拍集体照时队形很重要,这里对给定的N个人K排的队形设计排队规则如下: 每排人数为N/K(向下取整),多出来的人全部站在最后一排: 后排所有人的个子都不比前排 ...
- MySql 几个小技巧
分页查看: 在 mysql 环境下,执行命令: pager more,之后的结果分屏了. 简明扼要地查看表结构: describe table_name
- 设计资源:三个精美APP原型例子下载
原型设计是整个产品生产过程中不可或缺的一环,无论你是移动端UI设计师或是网页设计师,原型设计都会让整个设计过程更加轻松.原型是产品概念的具象化,它让每个项目参与者都能查看并提出意见以便在产品发布前日臻 ...
- 写点C++ 学习记录 充数
#include "stdafx.h" #include <cstdlib> #include <iostream> using namespace std ...
- 彻底测试全部拷贝list相关操作的区别python
1.用浅拷贝后修改数字,可以起到与原数据分离的效果 import copy origin = [, , [, ]] #origin 里边有三个元素:, ,[, ] cop1=origin.copy() ...
- java 打包jar 并后台运行
编译java: javac main.java 运行: java main.class 生成jar: 第一步:新建 一个MANIFEST.MF 第二步:将如下信息放到该文件中 Manifest-Ver ...
- Highcharts做柱状图怎样样每个柱子都是不同的颜色显示
series: [{ data: [{'color':'#F6BD0F','y':11}, {'color':'#AFD8F8','y':12}, {'color':'#8BBA00','y':13} ...
- 【转】Paxos算法2-算法过程
——转自:{老码农的专栏} 1.编号处理 根据P2c ,proposer在提案前会先咨询acceptor查看其批准的最大的编号和value,再决定提交哪个value.之前我们一直强调更高编号的prop ...
- gj7 对象引用、可变性和垃圾回收
7.1 python变量到底是什么 #python和java中的变量本质不一样,python的变量实质上是一个指针 int str, 便利贴 a = 1 a = "abc" #1. ...
- Linux学习--2
字符串 字符串可以用单引号,也可以用双引号,也可以不用引号.单双引号的区别跟PHP类似 单引号字符串的限制:单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的:单引号字串中不能出现单引号(对 ...