Skip to content

Commit c01c589

Browse files
add 981
1 parent 7ef2cd4 commit c01c589

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.github.contest
22

33

4-
import com.github.contest.bitManipulation.subsetXORSum
5-
import com.github.contest.strings.shiftingLetters
6-
import com.github.contest.strings.smallestStringProdVariant
7-
4+
import com.github.contest.strings.isNumber
85
import java.util.TreeMap
96

107

@@ -14,7 +11,7 @@ import java.util.TreeMap
1411

1512
fun main() {
1613

17-
subsetXORSum(intArrayOf(5, 1, 6))
14+
isNumber("6+1").also { println(it) }
1815
}
1916

2017
fun testing() {

contest/src/main/java/com/github/contest/design/DesignLeetcode.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,39 @@ class Trie() {
117117
return curr
118118
}
119119

120+
}
121+
122+
/**
123+
* 981. Time Based Key-Value Store
124+
*/
125+
126+
class TimeMap() {
127+
128+
private val store = mutableMapOf<String, MutableList<Pair<Int, String>>>()
129+
130+
fun set(key: String, value: String, timestamp: Int) {
131+
store.getOrPut(key) { mutableListOf(Pair(timestamp, value)) }.add(Pair(timestamp, value))
132+
}
133+
134+
fun get(key: String, timestamp: Int): String {
135+
val list = store[key] ?: return emptyString()
136+
var left = 0
137+
var right = list.size - 1
138+
var res = emptyString()
139+
140+
while (left <= right) {
141+
val mid = (left + right) / 2
142+
val (currentTimeStamp, currentValue) = list[mid]
143+
if (currentTimeStamp == timestamp) return currentValue
144+
if (currentTimeStamp < timestamp) {
145+
left = mid + 1
146+
res = currentValue
147+
} else right = mid
148+
}
149+
150+
return res
151+
}
152+
153+
private fun emptyString() = ""
154+
120155
}

0 commit comments

Comments
 (0)