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:

  1. Input: [1,2,3,4,4,3,2,1]
  2. Output: true
  3. Explanation: Possible partition [1,1],[2,2],[3,3],[4,4]

Example 2:

  1. Input: [1,1,1,2,2,2,3,3]
  2. Output: false
  3. Explanation: No possible partition.

Example 3:

  1. Input: [1]
  2. Output: false
  3. Explanation: No possible partition.

Example 4:

  1. Input: [1,1]
  2. Output: true
  3. Explanation: Possible partition [1,1]

Example 5:

  1. Input: [1,1,2,2,2,2]
  2. Output: true
  3. Explanation: Possible partition [1,1],[2,2],[2,2]

Note:

    1. 1 <= deck.length <= 10000
    2. 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)

  1. class Solution {
  2. int gcd(int a, int b) {
  3. while(b != 0) {
  4. int temp = b;
  5. b = a%b;
  6. a = temp;
  7. }
  8. return a;
  9. }
  10. public boolean hasGroupsSizeX(int[] deck) {
  11. if(deck.length < 2) {
  12. return false;
  13. }
  14.  
  15. Map<Integer, Integer> intCnt = new HashMap<>();
  16. for(int num: deck) {
  17. intCnt.put(num, intCnt.getOrDefault(num, 0) + 1);
  18. }
  19.  
  20. int preVal = -1;
  21. for(int val: intCnt.values()) {
  22. if(val == 1) {
  23. return false;
  24. }
  25. if(preVal == -1) {
  26. preVal = val;
  27. }
  28. else {
  29. preVal = gcd(preVal, val);
  30. if(preVal == 1) {
  31. return false;
  32. }
  33. }
  34. }
  35.  
  36. return preVal >= 2;
  37. }
  38. }

网上看到的超级简洁,自己的差好远,还有很长的路啊

  1. class Solution {
  2. int gcd(int a, int b) {
  3. while(b != 0) {
  4. int temp = b;
  5. b = a%b;
  6. a = temp;
  7. }
  8. return a;
  9. }
  10. public boolean hasGroupsSizeX(int[] deck) {
  11. Map<Integer, Integer> intCnt = new HashMap<>();
  12. for(int num: deck) {
  13. intCnt.put(num, intCnt.getOrDefault(num, 0) + 1);
  14. }
  15.  
  16. int res = 0;
  17. for(int val: intCnt.values()) {
  18. res = gcd(val, res);
  19. }
  20.  
  21. return res >= 2;
  22. }
  23. }

X of a Kind in a Deck of Cards LT914的更多相关文章

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

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

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

  4. Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)

    Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...

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

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

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

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

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

随机推荐

  1. 搭建IntelliJ IDEA授权服务器

    地址:https://blog.csdn.net/maozexijr/article/details/79072287    https://www.jianshu.com/p/754d8f907f2 ...

  2. Lightgbm 随笔

    lightGBM LightGBM 是一个梯度 boosting 框架,使用基于学习算法的决策树.它可以说是分布式的,高效的,有以下优势: 更快的训练效率 低内存使用 更高的准确率 支持并行化学习 可 ...

  3. 我的coding地址

    https://dev.tencent.com/u/dtid_d6b0780bdefc3f9c/follower#1

  4. 循环队列搜索 Search in Rotated Sorted Array

    这里比较重要的是,不要一上来就判断mid 和 target有没有关系.因为数组是无序的,这样的判断毫无结论,只会搞的更复杂.应该先想办法判断出哪一侧是有序的. class Solution { pub ...

  5. 初探LaTeX

    第一次使用LaTeX 步骤  1 安装LaTeX 通过官网http://www.tug.org/mactex/mactex-download.html下载Mac版LaTeX. 安装成功后会出现 步骤 ...

  6. 如何引入.graphql文件并优雅的使用fragment

    你还在为代码中放入长长的模版字符串所苦恼吗,如下图代码片段: ps:这个是grqphql client在nodejs后端项目的实践,如果你是在前端使用graphql,并使用了webpack,那么这些问 ...

  7. MVC ScriptBundle自定义排序。

    今天发现MVC的ScriptBundle @Scripts.Render()后是按照我也不知道顺序显示在页面上的,后果就是jquery.min.js被排在了后面(反正我下面那堆默认jquery.min ...

  8. python flask 解决中文乱码

    response = make_response(output_string)response.headers['Content-Type'] = 'text/plain;charset=UTF-8' ...

  9. 软件工程小组讨论设计NABCD

    项目名称:失物招领平台 项目工作小组:冰淇淋队 项目简介:目前同学们丢了东西都qq空间转发或者某个特定的qq群发消息,qq空间转发浪费了别人的时间,qq群发消息也浪费了别人的时间.怎么样才能浪费最少的 ...

  10. Java框架spring 学习笔记(一):SpringBean、ApplicationContext 容器、BeanFactory容器

    Spring容器是Spring框架的核心,容器可以创建对象并创建的对象连接在一起,配置和管理他们的整个生命周期.Spring 容器使用依赖注入(DI)来作为管理应用程序的组件,被称为 Spring B ...