[LeetCode-Sword Finger Offer] 61. Straight in playing cards

1. Topic

Randomly draw 5 cards from the playing cards to determine if it is a straight, that is, if the 5 cards are consecutive. 2~10 are the numbers themselves, A is 1, J is 11, Q is 12, K is 13, and the big and small kings are 0, which can be regarded as any number. A cannot be regarded as 14.

Example 1:

输入: [1,2,3,4,5]
输出: True

Example 2:

输入: [0,0,1,2,5]
输出: True

limit:

  • Array length is 5
  • The value of the array is [0, 13].

Two, solve

1. Set + traverse

Ideas:

According to the meaning of the question, the sufficient conditions for the 5 cards to be a straight are as follows:

  1. Except for the big and small kings, all cards are not repeated;
  2. This provided a maximum of five cards max, minimum min (except king size), need to satisfy: max - min < 5.
    1

Process :

  • Traverse five cards, and skip directly when encountering the king of big and small (ie 0) ;
  • Discrimination of repetition : Use Set to realize traversal judgment. The time complexity of Set's search method is O (1) O (1)O ( 1 )
  • Get the largest/smallest card : With the help of auxiliary traversal of ma and mi, you can traverse statistics.

It seems that it is not so easy to understand all at once. You can see the slides & code of reference 1 for combined understanding.

Code:

class Solution {
    
    
    public boolean isStraight(int[] nums) {
    
    
        Set<Integer> repeat = new HashSet<>();
        int max = 0, min = 14;
        for(int num : nums) {
    
    
            if(num == 0) continue; // 跳过大小王
            max = Math.max(max, num); // 最大牌
            min = Math.min(min, num); // 最小牌
            if(repeat.contains(num)) return false; // 若有重复,提前返回 false
            repeat.add(num); // 添加此牌至 Set
        }
        return max - min < 5; // 最大牌 - 最小牌 < 5 则可构成顺子
    }
}

Time complexity: O (n) = O (5) = O (1) O(n)=O(5)=O(1)O ( n )=O ( 5 )=O ( 1 )
space complexity: O (n) = O (5) = O (1) O(n)=O(5)=O(1)O ( n )=O ( 5 )=O ( 1 )

2. Sort + traverse

Ideas:

  • Sort the array first;
  • Discrimination of duplication : The same elements in the sorted array are adjacent to each other. Therefore, it can be judged by traversing the array and judging nums[i] = nums[i+1]whether it is true.
  • Obtain the largest/smallest card : After sorting, the last element nums[4]of the array is the largest card; the element nums[joker]is the smallest card, among which jokeris the number of big and small kings.

Code:

class Solution {
    
    
    public boolean isStraight(int[] nums) {
    
    
        int joker = 0;
        Arrays.sort(nums); // 数组排序
        for(int i = 0; i < 4; i++) {
    
    
            if(nums[i] == 0) joker++; // 统计大小王数量
            else if(nums[i] == nums[i + 1]) return false; // 若有重复,提前返回 false
        }
        return nums[4] - nums[joker] < 5; // 最大牌 - 最小牌 < 5 则可构成顺子
    }
}

Time complexity: O (nlogn) O(nlogn)O ( n l o g n )
space complexity: O (1) O(1)O ( 1 )

Three, reference

1. Interview question 61. Straight in playing cards (Set/Sort, clear illustration)

Guess you like

Origin blog.csdn.net/HeavenDan/article/details/110951432