[Leedcode] [JAVA] [914 title] [the common denominator]

【Problem Description】

给定一副牌,每张牌上都写着一个整数。

此时,你需要选定一个数字 X,使我们可以将整副牌按下述规则分成 1 组或更多组:

每组都有 X 张牌。
组内所有的牌上都写着相同的整数。
仅当你可选的 X >= 2 时返回 true。

 

示例 1:

输入:[1,2,3,4,4,3,2,1]
输出:true
解释:可行的分组是 [1,1],[2,2],[3,3],[4,4]
示例 2:

输入:[1,1,1,2,2,2,3,3]
输出:false
解释:没有满足要求的分组。
示例 3:

输入:[1]
输出:false
解释:没有满足要求的分组。
示例 4:

输入:[1,1]
输出:true
解释:可行的分组是 [1,1]
示例 5:

输入:[1,1,2,2,2,2]
输出:true
解释:可行的分组是 [1,1],[2,2],[2,2]

提示:

1 <= deck.length <= 10000
0 <= deck[i] < 10000
 

[Thinking] answer

1. The time complexity: O (N) space complexity: O (N)
  • Traversal time, count the number of each value, if a value is only one direct return false;
  • Enter [2, 2, 2, 2, 3, 3, 3, 3, 3, 3], in fact, is in line grouping meaning of the title, 2, 4, 3, there are six, the same 2 and 3 need to split into a set of two, i.e., whether the request twenty-two divisor number of elements have a common divisor.
public boolean hasGroupsSizeX(int[] deck) {
        //计数 
        int[] cnt = new int[10000];
        for(int x: deck){
            cnt[x]++; 
        }
        //避免虚幻内赋值
        int a=  cnt[0];
        for(int i: cnt){
            //出现单个返回错误
            if(i==1){
                return false;
            }
            if(i>1){
                //循环计算最大公约数
                a = gcd(a,i);
                if(a ==1 )
                {
                    return false;
                }
            }
        }

        return true;


    }
    /*
    最大公约数
    12 ➗ 9 =  1  ...... 3

     9 ➗ 3 =  3  ...... 0

     3返回 0跳出循环
    */
    public int gcd(int x, int y){
        int max = Math.max(x,y);
        int min = Math.min(x,y);
        while(min!= 0){
            int temp =min;
            min = max% min;
            max = temp;
        }
        return max;

/*
//递归求最大公约数(伟哥提供)
private int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }
    }
作者:liweiwei1419
链接:https://leetcode-cn.com/problems/x-of-a-kind-in-a-deck-of-cards/solution/qiu-jie-zui-da-gong-yue-shu-java-by-liweiwei1419/

2. ###### sweet Aunt streamline operations
2.1 3ms's knife

class Solution {
    public boolean hasGroupsSizeX(int[] deck) {
        // 计数
        int[] counter = new int[10000];
        for (int num: deck) {
            counter[num]++;
        }
        // 迭代求多个数的最大公约数
        int x = 0;
        for(int cnt: counter) {
            if (cnt > 0) {
                x = gcd(x, cnt); 
                if (x == 1) { // 如果某步中gcd是1,直接返回false
                    return false;
                }
            }
        }
        return x >= 2;
    }
    
    // 辗转相除法
    private int gcd (int a, int b) {
        return b == 0? a: gcd(b, a % b);
    }
}

作者:sweetiee
链接:https://leetcode-cn.com/problems/x-of-a-kind-in-a-deck-of-cards/solution/3ms-jian-dan-java-fu-zeng-reducexie-fa-miao-dong-b/

Gcd 2.2 Iterative process can be simplified with code reduce operator

class Solution {
    public boolean hasGroupsSizeX(int[] deck) {
        // 计数
        int[] counter = new int[10000];
        for (int num: deck) {
            counter[num]++;
        }
        // reduce求多个数的最大公约数
        // (因为这里当gcd==1的时候没有提前终止,并且本题数据量太小无法用并行流,因此耗时10ms,比for循环慢点)
        return Arrays.stream(counter).reduce(this::gcd).getAsInt() >= 2;
    }
    
    // 辗转相除法
    private int gcd (int a, int b) {
        return b == 0? a: gcd(b, a % b);
    }
}

作者:sweetiee
链接:https://leetcode-cn.com/problems/x-of-a-kind-in-a-deck-of-cards/solution/3ms-jian-dan-java-fu-zeng-reducexie-fa-miao-dong-b/

Wherein, reduce (this :: gcd) shall reduce ((a, b) - > gcd (a, b))
The following figure reduce ((a, b) - > a + b) is explained reduce operator:
image.png

【to sum up】

1. The greatest common divisor (Euclidean)
Examples of the greatest common divisor

    public int gcd(int x, int y){
        int max = Math.max(x,y);
        int min = Math.min(x,y);
        while(min!= 0){
            int temp =min;
            min = max% min;
            max = temp;
        }
        return max;
private int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }
 private int gcd (int a, int b) {
        return b == 0? a: gcd(b, a % b);
  1. Count the number of times you can use a hash table, you can also use an array.
  • Look at the data range of the title, if the title says, the input data contains only lowercase letters (uppercase characters) using an array of statistics is relatively easy
  • The underlying hash table is an array. Using arrays do the counting tasks themselves actually prepared the hash function
  1. Variable name only a few can write a few words with abbreviations, under normal circumstances, all with full name.
  • cnt represents the count, res represents result, ans represents the answer
Published 22 original articles · won praise 0 · Views 418

Guess you like

Origin blog.csdn.net/dadongwudi/article/details/105148901