Medium草稿★★★★
Hash MapBinary SearchDesign
Patterns🏗️ 資料結構設計🎯 二分搜尋#️⃣ 雜湊
尚未複習過

解法已隱藏 — 先讀題目敘述、自己想想看,再點上方按鈕揭曉。

981Time Based Key-Value StoreBinary SearchMediumBinary Search

設計一個基於時間的鍵值資料結構,可以儲存同一個 key 在不同 timestamp 的值,並且能查詢在指定 timestamp 或之前最近的值。實作 set(key, value, timestamp)get(key, timestamp) 兩個方法。

Example:

Input: [“TimeMap”, “set”, “get”, “get”, “set”, “get”] > [[], [“foo”,“bar”,1], [“foo”,1], [“foo”,3], [“foo”,“bar2”,4], [“foo”,4]] Output: [null, null, “bar”, “bar”, null, “bar2”]

Intuition

TIP

由於 timestamp 嚴格遞增,每個 key 對應的 (timestamp, value) 列表天然有序,可以用二分搜尋找「不超過指定時間」的最近值。

Approaches

1. Linear Search — set / O(n)
  • Idea: get 時線性掃描所有時間戳找到最近的
  • Time: set O(1)get O(n)
  • Space: O(n)
Linear Search Code
class TimeMap() {
    private val map = mutableMapOf<String, MutableList<Pair<Int, String>>>()

    fun set(key: String, value: String, timestamp: Int) {
        map.getOrPut(key) { mutableListOf() }.add(Pair(timestamp, value))
    }

    fun get(key: String, timestamp: Int): String {
        val list = map[key] ?: return ""
        var result = ""
        for ((t, v) in list) {
            if (t <= timestamp) result = v
            else break
        }
        return result
    }
}
⭐ 2. HashMap + Binary Search — set / O(n)
  • Idea: 用 HashMap 儲存每個 key 的時間序列,get 時用二分搜尋找右邊界
  • Time: set O(1)get O(log n)
  • Space: O(n)
class TimeMap() {
    private val map = mutableMapOf<String, MutableList<Pair<Int, String>>>()

    fun set(key: String, value: String, timestamp: Int) {
        map.getOrPut(key) { mutableListOf() }.add(Pair(timestamp, value))
    }

    fun get(key: String, timestamp: Int): String {
        val list = map[key] ?: return ""
        var left = 0
        var right = list.size - 1
        var result = ""
        while (left <= right) {
            val mid = left + (right - left) / 2
            if (list[mid].first <= timestamp) {
                result = list[mid].second
                left = mid + 1
            } else {
                right = mid - 1
            }
        }
        return result
    }
}

🔑 Takeaways