lc周赛490-必须拿起的最小连续卡牌数

# 暴力法
class Solution:
    def minimumCardPickup(self, cards: List[int]) -> int:
        min_card = len(cards)
        find = False
        if len(cards) == 1:
            return - 1
        i = 0
        while i < len(cards):
            j = i + 1
            while j < len(cards):
                if cards[j] == cards[i]:
                    min_card = min(min_card, j - i + 1)
                    find = True
                    break
                j += 1
            i += 1
        return -1 if not find else min_card  

 

# 空间换时间
class Solution:
    def minimumCardPickup(self, cards: List[int]) -> int:
        min_card = len(cards)
        find = False
        if len(cards) == 1:
            return -1
        cards_map = {}
        i = 0
        while i < len(cards):
            if cards_map.get(cards[i]) is None:
                cards_map.setdefault(cards[i], i)
            else:
                min_card = min(i - cards_map.get(cards[i]) + 1, min_card)
                find = True
                cards_map[cards[i]] = i
            i += 1
        return -1 if not find else min_card   

 

posted @ 2022-05-01 11:37  stronger_el  阅读(41)  评论(0)    收藏  举报