作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/description/

题目描述

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. 1 <= deck.length <= 10000
  2. 0 <= deck[i] < 10000

题目大意

判断一堆牌能不能分成很多组,每个组是相同的元素,并且每个组最少两张牌。

解题方法

遍历

果然是个Easy的题目,很简单。

每个组的元素都是相同的情况下,分组之后每个组有多少个元素呢?

首先,要求每个组的元素都是相同的,因此元素个数最小的那个组限制了每个组的个数。

但不一定是所有元素个数的最小值,因为相同的元素可以分成很多组的,比如这个测试用例,每个组可以都是两个元素即可。

[1,1,1,1,2,2,2,2,2,2]

所以,可以用一个遍历,从2遍历到最少次数的数字中的元素个数。道理是,把最少次数在划分和不划分的情况下,看其他组能不能按照这个个数进行均分。

最坏情况下的时间复杂度是O(N^2),空间复杂度是O(N)。

Python代码如下:

class Solution(object):
def hasGroupsSizeX(self, deck):
"""
:type deck: List[int]
:rtype: bool
"""
count = collections.Counter(deck)
X = min(count.values())
for x in range(2, X + 1):
if all(v % x == 0 for v in count.values()):
return True
return False

最大公约数

二刷的时候想到了最大公约数解法。

最终分的组的大小是多少,就是每个数字的次数的最大公约数。

比如例子:

[1,1,2,2,2,2]

最终分成了[1,1],[2,2],[2,2],每个组是2个数字,怎么得来的?看1出现了2次,看2出现了4次,最终的结果就是2和4的最大公约数2。

公约数能被所有的数字个数整除,所以最大的那个公约数,就是能被所有数字个数整除的最大组大小。

C++代码如下:

class Solution {
public:
bool hasGroupsSizeX(vector<int>& deck) {
unordered_map<int, int> counter;
for (int d : deck) {
counter[d] ++;
}
int res = 0;
for (auto& c : counter) {
res = gcd(c.second, res);
}
return res > 1;
}
int gcd(int x, int y) {
return y == 0 ? x : gcd(y, x % y);
}
};

日期

2018 年 9 月 30 日 —— 9月最后一天啦!
2018 年 11 月 24 日 —— 周日开始!一周就过去了~
2020 年 3 月 27 日 —— 开始整合资源

【LeetCode】914. X of a Kind in a Deck of Cards 解题报告(Python & C++)的更多相关文章

  1. [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 ...

  2. [leetcode]914. X of a Kind in a Deck of Cards (easy)

    原题 题目原意可转换为 两组有大于等于2的公因数 /** * @param {number[]} deck * @return {boolean} */ var hasGroupsSizeX = fu ...

  3. 【LeetCode】面试题62. 圆圈中最后剩下的数字 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 约瑟夫环 日期 题目地址:https://leetco ...

  4. 【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 结构记录数组中每个元素 ...

  5. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  6. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  7. 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 ...

  8. 【LeetCode】1160. Find Words That Can Be Formed by Characters 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 日期 题目地址:https://leetco ...

  9. 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 preSum + 字典 日期 题目地址:https:/ ...

随机推荐

  1. 48-Merge Sorted Array

    $88. Merge Sorted Array My Submissions QuestionEditorial Solution Total Accepted: 98885 Total Submis ...

  2. TOMCAT 搭建

    第一步:下载 软件 和 JDK 第二个:https://www.oracle.com/java/technologies/javase-jdk16-downloads.html 传输到Linux里. ...

  3. acquire

    An acquired taste is an appreciation for something unlikely to be enjoyed by a person who has not ha ...

  4. angular中路由跳转并传值四种方式

    一.路由传值 步骤1 路由传递参数 注意 一定是要传递 索引值 let key = index 这种情况是在浏览器中可以显示对应的参数 这种的是问号 localhost:8080/news?id=2& ...

  5. 转 Android Studio中Junit调试

    转:https://blog.csdn.net/xanthus_li/article/details/54314189 在程序开发完成后,需要交给专业的调试人员进行相关的专业调试(白盒测试,黑盒测试, ...

  6. 使用 ACE 库框架在 UNIX 中开发高性能并发应用

    使用 ACE 库框架在 UNIX 中开发高性能并发应用来源:developerWorks 中国 作者:Arpan Sen ACE 开放源码工具包可以帮助开发人员创建健壮的可移植多线程应用程序.本文讨论 ...

  7. matplotlib如何绘制直方图、条形图和饼图

    1 绘制直方图: import matplotlib.pyplot as plt import numpy as np import matplotlib def hist1(): # 设置matpl ...

  8. String 、StringBuffer、StringBuilder的区别

    String:字符串常量 StringBuffer:字符串变量:线程安全的 StringBuilder:字符串变量:线程非安全的 三者执行速度比较:StringBuilder >  String ...

  9. 设计模式和java实现

    三种工厂模式:https://www.cnblogs.com/toutou/p/4899388.html 适配器模式:https://www.cnblogs.com/V1haoge/p/6479118 ...

  10. 机器学习算法中的评价指标(准确率、召回率、F值、ROC、AUC等)

    参考链接:https://www.cnblogs.com/Zhi-Z/p/8728168.html 具体更详细的可以查阅周志华的西瓜书第二章,写的非常详细~ 一.机器学习性能评估指标 1.准确率(Ac ...