Problem (verbatim):
“A multiset (also known as a bag) is a mutable, unordered collection of distinct objects that may appear more than once in the collection.
Implement a multiset that implements the following methods:
• add(element)
• remove(element)
• count(for: element)”
总结(思路要点):
用哈希表(字典 /Counter)记录每个元素出现次数:
add(x):freq[x] += 1。remove(x): 若存在且freq[x] > 0则减一,减到 0 可删键(未说明报错,通常可忽略无效删除)。count(x): 返回freq.get(x, 0)。
时间复杂度均为均摊 O(1),空间为去重元素个数。
VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 OpenAI、Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备 Stripe 或类似工程向公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。