-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
43 lines (42 loc) · 1.25 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Collections.Generic;
namespace TimeBasedKeyValueStore
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
public class TimeMap
{
private Dictionary<string, List<int>> timeDict;
private Dictionary<string, List<string>> wordDict;
public TimeMap()
{
timeDict = new Dictionary<string, List<int>>();
wordDict = new Dictionary<string, List<string>>();
}
public void Set(string key, string value, int timestamp)
{
if (!timeDict.ContainsKey(key))
{
timeDict[key] = new List<int>();
wordDict[key] = new List<string>();
}
timeDict[key].Add(timestamp);
wordDict[key].Add(value);
}
public string Get(string key, int timestamp)
{
if (!timeDict.ContainsKey(key)) return "";
var times = timeDict[key];
var index = times.BinarySearch(timestamp);
if (index >= 0) return wordDict[key][index];
index = -(index + 1);
if (index == 0) return "";
return wordDict[key][index - 1];
}
}
}