← 返回 databricks 的题目列表Space-Efficient Versioned Set Snapshot Design
类型:online_judge
databricks
Design a space-efficient snapshot set. You need to provide interfaces for adding versions, adding sets, removing sets, and taking snapshots. Ensure optimal time and space efficiency in your implementation.
Interface functions:
add_version(number: int) -> None: Adds a new version number.
add_set(item_set: Set[int]) -> None: Adds a new set.
remove_set(item_set: Set[int]) -> None: Removes a set.
snapshot() -> Dict[int, Set[int]]: Retrieves the current snapshot of all versions, returning a dictionary where keys are version numbers and values are the corresponding sets.
Initial conditions and assumptions:
Initially, there are no versions or sets.
All version numbers are unique.
Each set's elements are unique.
Except for the remove function, where the input set must exist, all operations assume valid inputs.
Test cases:
Create an instance, call add_version(1), add_set({1, 2, 3}), snapshot() should return {1: {1, 2, 3}}
Call add_version(2), add_set({3, 4}), snapshot() should return {1: {1, 2, 3}, 2: {3, 4}}
Call remove_set({1, 2, 3}), snapshot() should return {2: {3, 4}}
Call add_version(3), add_set({5}), snapshot() should return {2: {3, 4}, 3: {5}}
Call add_version(4), without calling add or remove set methods, snapshot() should return {2: {3, 4}, 3: {5}}
Example
Input
add_version(1)
add_set({1, 2, 3})
snapshot()