|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +#region Approach 1 (Linear Search of Values) |
| 6 | +public class TimeMap |
| 7 | +{ |
| 8 | + private Dictionary<(string key, string value), int> _timeMap; |
| 9 | + |
| 10 | + public TimeMap() |
| 11 | + { |
| 12 | + _timeMap = new Dictionary<(string key, string value), int>(); |
| 13 | + } |
| 14 | + |
| 15 | + public void Set(string key, string value, int timestamp) |
| 16 | + { |
| 17 | + _timeMap[(key, value)] = timestamp; |
| 18 | + } |
| 19 | + |
| 20 | + public string Get(string key, int timestamp) |
| 21 | + { |
| 22 | + foreach (var outerKeyValue in _timeMap) |
| 23 | + { |
| 24 | + if (key == outerKeyValue.Key.key) |
| 25 | + { |
| 26 | + if (timestamp == outerKeyValue.Value) |
| 27 | + return outerKeyValue.Key.value; |
| 28 | + else |
| 29 | + return _GetValueOfPreviousLargestTimestamp(key, timestamp); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return ""; |
| 34 | + } |
| 35 | + |
| 36 | + private string _GetValueOfPreviousLargestTimestamp(string key, int currentTimestamp) |
| 37 | + { |
| 38 | + int largestTimeStamp = _GetPreviousLargestTimestampOfKey(key, currentTimestamp); |
| 39 | + |
| 40 | + return _timeMap |
| 41 | + .Where(kvp => kvp.Key.key == key && kvp.Value == largestTimeStamp) |
| 42 | + .Select(kvp => kvp.Key.value) |
| 43 | + .FirstOrDefault() ?? ""; |
| 44 | + } |
| 45 | + |
| 46 | + private int _GetPreviousLargestTimestampOfKey(string key, int currentTimestamp) |
| 47 | + { |
| 48 | + return _timeMap |
| 49 | + .Where(kvp => kvp.Key.key == key && currentTimestamp >= kvp.Value) |
| 50 | + .Select(kvp => kvp.Value) // Select the timestamp values |
| 51 | + .DefaultIfEmpty(0) // Provide a default value to avoid an empty sequence |
| 52 | + .Max(); |
| 53 | + } |
| 54 | +} |
| 55 | +#endregion |
| 56 | + |
| 57 | +#region Approach 2 (Binary Search of Values) |
| 58 | +public class TimeMap2 |
| 59 | +{ |
| 60 | + private readonly Dictionary<string, List<(int timestamp, string value)>> _dict; |
| 61 | + |
| 62 | + public TimeMap2() |
| 63 | + { |
| 64 | + _dict = new Dictionary<string, List<(int, string)>>(); |
| 65 | + } |
| 66 | + |
| 67 | + public void Set(string key, string value, int timestamp) |
| 68 | + { |
| 69 | + var values = new List<(int, string)>(); |
| 70 | + if (!_dict.ContainsKey(key)) |
| 71 | + { |
| 72 | + _dict.Add(key, values); |
| 73 | + } |
| 74 | + _dict[key].Add((timestamp, value)); |
| 75 | + } |
| 76 | + |
| 77 | + public string Get(string key, int timestamp) |
| 78 | + { |
| 79 | + if (!_dict.ContainsKey(key)) |
| 80 | + { |
| 81 | + return ""; |
| 82 | + } |
| 83 | + |
| 84 | + return _GetValueUsingBinarySearch(key, timestamp); |
| 85 | + } |
| 86 | + |
| 87 | + private string _GetValueUsingBinarySearch(string key, int timestamp) |
| 88 | + { |
| 89 | + var value = _dict[key]; |
| 90 | + |
| 91 | + var left = 0; |
| 92 | + var right = value.Count; |
| 93 | + var result = ""; |
| 94 | + |
| 95 | + while (left < right) |
| 96 | + { |
| 97 | + var mid = left + (right - left) / 2; |
| 98 | + if (value[mid].timestamp == timestamp) |
| 99 | + { |
| 100 | + return value[mid].value; |
| 101 | + } |
| 102 | + else if (value[mid].timestamp < timestamp) |
| 103 | + { |
| 104 | + left = mid + 1; |
| 105 | + result = value[mid].value; |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + right = mid; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return result; |
| 114 | + } |
| 115 | +} |
| 116 | +#endregion |
| 117 | + |
| 118 | +public class Solution |
| 119 | +{ |
| 120 | + public static void Main(string[] args) |
| 121 | + { |
| 122 | + //TimeMap timeMap = new TimeMap(); |
| 123 | + //timeMap.Set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1. |
| 124 | + //Console.WriteLine(timeMap.Get("foo", 1)); // return "bar" |
| 125 | + //Console.WriteLine(timeMap.Get("foo", 3)); // return "bar", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar". |
| 126 | + //timeMap.Set("foo", "bar2", 4); // store the key "foo" and value "bar2" along with timestamp = 4. |
| 127 | + //Console.WriteLine(timeMap.Get("foo", 4)); // return "bar2" |
| 128 | + //Console.WriteLine(timeMap.Get("foo", 5)); // return "bar2" |
| 129 | + |
| 130 | + |
| 131 | + TimeMap2 timeMap = new TimeMap2(); |
| 132 | + timeMap.Set("love", "high", 10); // store the key "foo" and value "bar" along with timestamp = 1. |
| 133 | + timeMap.Set("love", "low", 20); // store the key "foo" and value "bar" along with timestamp = 1. |
| 134 | + Console.WriteLine(timeMap.Get("love", 5)); // return "" |
| 135 | + Console.WriteLine(timeMap.Get("love", 10)); // return "high", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar". |
| 136 | + Console.WriteLine(timeMap.Get("love", 15)); // return "high", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar". |
| 137 | + Console.WriteLine(timeMap.Get("love", 20)); // return "low", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar". |
| 138 | + Console.WriteLine(timeMap.Get("love", 25)); // return "low", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar". |
| 139 | + |
| 140 | + Console.ReadKey(); |
| 141 | + } |
| 142 | + |
| 143 | +} |
| 144 | + |
0 commit comments