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
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
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 结构记录数组中每个元素 ...
随机推荐
- Flask对数据库的操作-----
首先得做好做基本的框架 # -*- encoding: utf-8 -*- from flask import Flask,render_template #导入第三方连接库sql点金术 from f ...
- GP工具环境变量名称列表
帮助地址:http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/#/Using_environment_settings/ ...
- SpringBoot多模块项目打包问题
项目结构图如下: 在SpringBoot多模块项目打包时遇见如下错误: 1.repackage failed: Unable to find main class -> [Help 1] 解决步 ...
- P4702 取石子
我什么时候写一下污污的小故事呢?反正不是现在. 题目描述 Alice 和 Bob 在玩游戏. 他们有 nn 堆石子,第 ii 堆石子有 a_iai 个,保证初始时 a_i \leq a_{i + 1 ...
- MPC学习笔记1:基于状态空间模型的预测控制(2)
基于估计的无约束预测控制 1.引言 基本上这两个部分都是在线性理论的框架下,利用状态空间法来建模.求解控制律.状态空间模型在理论分析上具有很强的优越性,但实际应用中能直接准确且经济地获取系统状态并不容 ...
- 使用cmd命令导入SQL文件
1 . 进入SQL安装目录下的bin目录,比如我的是在 C:\Program Files\MySQL\MySQL Server 5.5\bin目录下 2. 开始 --->运行--->输入c ...
- appium初步认识
Appium简介: appium官网:http://appium.io/ 一.什么是appium Appium是一个开源.跨平台的测试框架,可以用来测试原生及混合的移动端应用.Appium支持IOS. ...
- Windows10安装pycocotools方法,亲测可用!
如果遇到:No module named 'pycocotools' 错误,说明你的环境需要安装pycocotools,以下介绍在Windows10下安装pycocotools的方法,这是本人结合看过 ...
- Taro开发微信小程序之利用腾讯地图sdk标记
首先要下载腾讯地图提供的sdk,放在项目的对应目录下,引用. import QQMapWX from '../../sdks/qqmap-wx-jssdk' 设置好后,就可以开始使用了. let qq ...
- Node.js 薄荷网爬取
Node.js:是一个基于前端的服务器,主要的特点:单线程,异步I/O(对这个没有了解,开发起来真的会踩很多坑),事件驱动 前言:本人主要是一个以使用.Net平台下的语言,进行开发的一个菜鸡,之前面试 ...