b_lc_区间内查询数字的频率(二分找到下标)

求arr[left...right] 中某个元素出现的频率。数据很大

思路:

class RangeFreqQuery:
    
    def __init__(self, arr: List[int]):
        mp = collections.defaultdict(int)
        for i in range(len(arr)):
            if arr[i] not in mp:
                mp[arr[i]] = []
            mp[arr[i]].append(i)
        self.m = mp

    def query(self, left: int, right: int, value: int) -> int:
        if value not in self.m:
            return 0
        pos = self.m.get(value)
        return bisect.bisect_right(pos, right) - bisect.bisect_left(pos, left)

这题大E了,用了复杂的方法去做...

posted @ 2021-11-21 12:39  童年の波鞋  阅读(30)  评论(0编辑  收藏  举报