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 ...
随机推荐
- A4988和CNC SHIELD使用方法 步进电机
接线视频 点这看视频 来源 https://www.basemu.com/a4988_pinout_and_how_to_use.html 注意要点 A4988既要12V外部供电,也要5V逻辑供电 我 ...
- 【HNOI 2018】排列
Problem Description 给定 \(n\) 个整数 \(a_1, a_2, \ldots , a_n(0 \le a_i \le n)\),以及 \(n\) 个整数 \(w_1, w_2 ...
- _luckdraw
该表可以控制进行抽奖.10连抽: `comment` 备注 `itemId` 物品ID `chance`几率 `itemCount` 数量
- Java原生API访问MongoDB
1.pom.xml <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java ...
- SR-IOV虚拟机的MTU与物理网卡的MTU
在进行SR-IOV虚拟机MTU方面的测试时,出现如下情况: 1)物理网卡PF的MTU值是4000: root@compute-1:~# ip l|more1: lo: <LOOPBACK,UP, ...
- Matlab:五点差分方法求解椭圆方程非导数边值问题
差分格式脚本文件: tic; clear clc M=32;%x的步数 N=16;%y的步数 h1=1/M;%x的步长 h2=1/N;%y的步长 x=0:h1:1; y=0:h2:1; u=zeros ...
- Oracle DB 使用RMAN恢复目录
• 对恢复目录和RMAN 资料档案库控制文件的使用进行比较• 创建和配置恢复目录• 在恢复目录中注册数据库• 同步恢复目录• 使用RMAN 存储脚本• 备份恢复目录• 创建虚拟专用目录 RMAN 资料 ...
- 微信支付---公众号支付和H5支付区别
微信支付分为如下几种:(来源https://pay.weixin.qq.com/wiki/doc/api/index.html) 本文主要讲解公众号支付和H5支付,两者均属于线上支付比较常用的方式: ...
- html5(二)
*{ margin:0px; padding:0px;} h1{ font:bold 20px verdana,sans-serif;} h1{ font:bold 14px verdana,sans ...
- .net core部署到Ubuntu碰到的问题
数据库连接的时候,会报错“MySql.Data.MySqlClient.MySqlException:“The host localhost does not support SSL connecti ...