Generation

class from descriptionTue, 14 Feb 2023

Design and implement a data structure for a cache, which has the following functions: get(key) Return the value associated with the specified key if it exists in the cache, else return -1 . put(key, value, weight) Associate value with key in the cache, such that value might be later retrieved by get(key) . Cache has a fixed capacity, and when such capacity is reached, key with the least score must be invalidated until the number of key s falls within cache capacity. The score is calculated as follows: - weight ∕ [ln(current_time - last_accessed_time + 1) + 1] Implementation of the cache should be optimized on the time complexity of get(key) .

type Cache struct { cache map[int]CacheNode capacity int } type CacheNode struct { key int value int weight int }

Questions about programming?Chat with your personal AI assistant