To prove safety offer - whether continuous brand

Title Description

LL particularly good mood today, because he bought a deck of cards and found that there actually has two king, two small king (the deck was originally 54 ^ _ ^) ... he randomly drawn from the five brand, wanted to Cece own luck, see if you can be able to get straight, if able to get it, he decided to buy a sports lottery, hey! ! "Red A, 3 of spades, Wang, King, square piece 5", "Oh My God!" Straight ..... LL is not happy, he thought, decides \ Amy can be seen as any number, and regarded as A 1, J is 11, Q is 12, K 13. The above five cards can become "1,2,3,4,5" (king size and 2, respectively seen as 4), "So Lucky!". LL decided to buy sports lottery friends. Now, I ask you to use this piece of card simulate the above procedure, and then tell us how LL luck, if the card can be composed of straight to output true, otherwise it will output false. For convenience, you can think of king size is 0.

 

Thinking

If not a king size, five cards need to be straight is required: the maximum number of max - min = 4 Minimum Number

If there is a king size, five cards need to be straight is required: max - min = 3 or max - min = 4

If there are two king size, five cards need to be straight requires: max - min = 2 or max -min = 3 or max - min = 4

Thus in addition to the king size as long as no duplicate numbers, and max - min <= 4 is straight up.

import collections
class Solution:
    def IsContinuous(self, numbers):
        if not numbers:
            return False
        
        maxi = max(numbers)
        mini = 14
        nums = collections.defaultdict(lambda:0)
        
        for n in numbers:
            if nums[n] > 0 and n!=0:
                return False
            nums[n] += 1
            if n<mini and n!=0:
                mini = n
        if maxi-mini<=4:
            return True
        else:
            return False

 

Published 82 original articles · won praise 2 · Views 4350

Guess you like

Origin blog.csdn.net/qq_22498427/article/details/104819058