数组算法特殊套路:田忌赛马,按字典序去重,O(1)时间

田忌赛马
A最大的数 > B最大的数,则A最大的数上场,否则用A最小的数兑掉B最大的数

"""
优势洗牌
给定两个大小相等的数组 nums1 和 nums2,nums1 相对于 nums 的优势可以用满足 nums1[i] > nums2[i] 的索引 i 的数目来描述。

返回 nums1 的任意排列,使其相对于 nums2 的优势最大化。
"""
class Solution:
    def advantageCount(self, nums1: List[int], nums2: List[int]) -> List[int]:
        n = len(nums1)
        nums1.sort(reverse=True)
        nums2A = [(i, x) for i,x in enumerate(nums2)]
        nums2A.sort(key=lambda x: x[1], reverse=True)

        left, right = 0, n - 1
        res = [0] * n
        for i, x in nums2A:
            if nums1[left] > x:
                res[i] = nums1[left]
                left += 1
            else:
                res[i] = nums1[right]
                right -= 1
        return res

"""
去除重复字母
    给你一个字符串 s ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 返回结果的字典序最小(要求不能打乱其他字符的相对位置)。
"""
class Solution:
    def removeDuplicateLetters(self, s: str) -> str:
        stack = []
        seen = set()
        # 统计s中各字符的次数
        count = collections.Counter(s)
        for c in s:    # 遍历s
            if c not in seen:
                # c不在集合中,需要入栈
                # 需要保持字典序:剔除(比c字典序大,且还有机会入栈的)字符
                # c入栈,c入集合
                while stack and c < stack[-1] and count[stack[-1]] > 0:
                    seen.discard(stack.pop())
                seen.add(c)
                stack.append(c)
            # c剩余数量减1
            count[c] -= 1
        return ''.join(stack)
插入、删除和随机访问都是 O(1) 的容器

class RandomizedSet:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.nums = []
        self.maps = {}


    def insert(self, val: int) -> bool:
        """
        Inserts a value to the set. Returns true if the set did not already contain the specified element.
        """
        if val in self.maps:
            return False
        self.nums.append(val)
        self.maps[val] = len(self.nums) - 1
        return True


    def remove(self, val: int) -> bool:
        """
        Removes a value from the set. Returns true if the set contained the specified element.
        """
        if val not in self.maps:
            return False
        i = self.maps[val]
        self.maps[self.nums[-1]] = i
        self.nums[i] = self.nums[-1]
        self.nums.pop()
        self.maps.pop(val)
        return True

    def getRandom(self) -> int:
        """
        Get a random element from the set.
        """
        i = random.randint(0, len(self.nums) - 1)
        return self.nums[i]



# Your RandomizedSet object will be instantiated and called as such:
# obj = RandomizedSet()
# param_1 = obj.insert(val)
# param_2 = obj.remove(val)
# param_3 = obj.getRandom()
posted @ 2022-07-07 14:59  daxiacet  阅读(75)  评论(0编辑  收藏  举报