码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode 914. X of a Kind in a Deck of Cards


    You are given an integer array deck where deck[i] represents the number written on the ith card.

    Partition the cards into one or more groups such that:

    • Each group has exactly x cards where x > 1, and
    • All the cards in one group have the same integer written on them.

    Return true if such partition is possible, or false otherwise.

    Example 1:

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

    Example 2:

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

    Constraints:

    • 1 <= deck.length <= 104
    • 0 <= deck[i] < 104

    读题读了半天还读错了……其实是求,把一个数组里的数字分成数字相等的n组,每组的数字个数也要一样,那么它的本质就是求这个数组里每个数字出现次数的最大公约数。

    第一种方法是brute force,不用gcd的求法,就直接从最小的count遍历到2,看能不能被所有的count整除。

    1. class Solution {
    2. public boolean hasGroupsSizeX(int[] deck) {
    3. Map<Integer, Integer> map = new HashMap<>();
    4. for (int i : deck) {
    5. map.put(i, map.getOrDefault(i, 0) + 1);
    6. }
    7. int min = 10000;
    8. for (int i : map.keySet()) {
    9. min = Math.min(min, map.get(i));
    10. }
    11. for (int i = min; i >= 2; i--) {
    12. boolean found = true;
    13. for (int num : map.keySet()) {
    14. int value = map.get(num);
    15. if (value % i != 0) {
    16. found = false;
    17. continue;
    18. }
    19. }
    20. if (found) {
    21. return true;
    22. }
    23. }
    24. return false;
    25. }
    26. }

    gcd的求法已经忘了。回顾了一下,大概就是gcd(a, b),如果b == 0那么a就是gcd,要么就把a变成b,b变成a%b,继续recursive gcd。

    Java Program to Compute GCD - GeeksforGeeks

    对应到这道题上,就是计算完count以后遍历map,先全局记一个gcd,每次计算全局gcd和当前遍历到的数字的gcd,最后看全局gcd是否>=2。

    1. class Solution {
    2. public boolean hasGroupsSizeX(int[] deck) {
    3. Map<Integer, Integer> map = new HashMap<>();
    4. for (int i : deck) {
    5. map.put(i, map.getOrDefault(i, 0) + 1);
    6. }
    7. int g = -1;
    8. for (int i : map.keySet()) {
    9. int value = map.get(i);
    10. if (g == -1) {
    11. g = value;
    12. } else {
    13. g = gcd(g, value);
    14. }
    15. }
    16. return g >= 2;
    17. }
    18. private int gcd(int a, int b) {
    19. if (b == 0) {
    20. return a;
    21. } else {
    22. return gcd(b, a % b);
    23. }
    24. }
    25. }

  • 相关阅读:
    Redis学习笔记①基础篇_Redis快速入门
    RxJS 实战: 基于 BehaviorSubject 实现状态管理 & 结合 React
    cookie和session区别
    AI Agent框架(LLM Agent):LLM驱动的智能体如何引领行业变革,应用探索与未来展望
    SpringCloud的新闻资讯项目00--- 项目功能演示
    网络中数字证书原理及应用
    高通camera-sensor分辨率简单梳理
    用Java代码更改PDF页边距,批量处理PDF文档
    成为Linux大神——必须要具备的基本技能!
    前端开发之移动端基础
  • 原文地址:https://blog.csdn.net/qq_37333947/article/details/127907315
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号