Skip to content
LC-2260 Medium LeetCode

2260. Minimum Consecutive Cards to Pick Up

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 53% Topics: Array, Hash Table, Sliding Window
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(n)

# hash table
class Solution(object):
    def minimumCardPickup(self, cards):
        """
        :type cards: List[int]
        :rtype: int
        """
        lookup = {}
        result = float("inf")
        for i, x in enumerate(cards):
            if x in lookup:
                result = min(result, i-lookup[x]+1)
            lookup[x] = i
        return result if result != float("inf") else -1

Solution from kamyu104/LeetCode-Solutions · MIT