← 返回 netflix 的题目列表Weighted Cache: Implementation of Get and Put Methods
类型:online_judge
Implement a weighted cache system with the following features:
Supports key-value pairs, with an initial total weight limit.
Provide get(key) and put(key, value, weight) methods.
When adding a new key-value pair causes the total weight to exceed the limit, remove the key-value pair with the largest weight.
Requirements:
Ensure the get and put methods have a time complexity of O(logN).
get method: If the key exists, return the corresponding value, otherwise, return None.
put method: Accepts a key, value, and weight, and stores them in the cache.
Use a TreeMap or similar data structure for implementation. Provide a complete code solution and 5 test cases.
Example
Input
put a 1 2
put b 2 3
get a