# 暴力法
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