zoukankan      html  css  js  c++  java
  • 981. Time Based Key-Value Store

    package LeetCode_981
    
    import java.util.*
    import kotlin.collections.HashMap
    
    /**
     * 981. Time Based Key-Value Store
     * https://leetcode.com/problems/time-based-key-value-store/
     * Create a timebased key-value store class TimeMap, that supports two operations.
    1. set(string key, string value, int timestamp)
    Stores the key and value, along with the given timestamp.
    2. get(string key, int timestamp)
    Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev <= timestamp.
    If there are multiple such values, it returns the one with the largest timestamp_prev.
    If there are no values, it returns the empty string ("").
    
    Example 1:
    Input:
    inputs = ["TimeMap","set","get","get","set","get","get"],
    inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]
    Output: [null,null,"bar","bar",null,"bar2","bar2"]
    Explanation:
    TimeMap kv;
    kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1
    kv.get("foo", 1);  // output "bar"
    kv.get("foo", 3); // output "bar" since there is no value corresponding to foo at timestamp 3 and timestamp 2,
    then the only value is at timestamp 1 ie "bar"
    kv.set("foo", "bar2", 4);
    kv.get("foo", 4); // output "bar2"
    kv.get("foo", 5); //output "bar2"
     * */
    class TimeMap() {
    
        /** Initialize your data structure here. */
    
        /*
        * solution 1: HashMap + TreeMap, key:String, value:TreeMap, key of TreeMap is timestamp, value of TreeMap is value,
        * Time: O(logk), k is number of timestamp,
        * Space: O(n*k), n is number of key,
        * */
        val map = HashMap<String, TreeMap<Int, String>>()
    
        fun set(key: String, value: String, timestamp: Int) {
            if (!map.containsKey(key)) {
                map.put(key, TreeMap())
            }
            map.get(key)!!.put(timestamp, value)
        }
    
        fun get(key: String, timestamp: Int): String {
            //get the first one which smaller than or equal to target
            if (map.containsKey(key)) {
                val treeMap = map.get(key)
                if (treeMap != null) {
                    val entry = treeMap.floorEntry(timestamp)
                    if (entry != null) {
                        return entry.value
                    }
                }
            }
            return ""
        }
    }
    /**
     * Your TimeMap object will be instantiated and called as such:
     * var obj = TimeMap()
     * obj.set(key,value,timestamp)
     * var param_2 = obj.get(key,timestamp)
     */
  • 相关阅读:
    物联网常见通信协议RFID、NFC、Bluetooth、ZigBee等梳理
    最全NB-IoT/eMTC物联网解决方案名录汇总
    解析:智慧医疗发展的5大趋势
    国家集成电路产业基金“二期”正在酝酿,规模直逼万亿
    Java基础【基本数据类型包装类、int与String 之间的相互转换】
    Java基础 【Arrays 类的使用】
    Java基础【冒泡、选择排序、二分查找】
    String、StringBuffer 的使用 ,两个面试问题
    使用Java Api 操作HDFS
    Linux下使用wget下载FTP服务器文件
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/14207057.html
Copyright © 2011-2022 走看看