|
| 1 | +var TimeMap = function() { |
| 2 | + |
| 3 | +}; |
| 4 | + |
| 5 | +/** |
| 6 | + * @param {string} key |
| 7 | + * @param {string} value |
| 8 | + * @param {number} timestamp |
| 9 | + * @return {void} |
| 10 | + */ |
| 11 | +TimeMap.prototype.set = function(key, value, timestamp) { |
| 12 | + |
| 13 | +}; |
| 14 | + |
| 15 | +/** |
| 16 | + * @param {string} key |
| 17 | + * @param {number} timestamp |
| 18 | + * @return {string} |
| 19 | + */ |
| 20 | +TimeMap.prototype.get = function(key, timestamp) { |
| 21 | + |
| 22 | +}; |
| 23 | + |
| 24 | +/** |
| 25 | + * Your TimeMap object will be instantiated and called as such: |
| 26 | + * var obj = new TimeMap() |
| 27 | + * obj.set(key,value,timestamp) |
| 28 | + * var param_2 = obj.get(key,timestamp) |
| 29 | + */ |
| 30 | + |
| 31 | +// Input |
| 32 | +// ["TimeMap", "set", "get", "get", "set", "get", "get"] |
| 33 | +// [[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]] |
| 34 | +// Output |
| 35 | +// [null, null, "bar", "bar", null, "bar2", "bar2"] |
| 36 | + |
| 37 | + |
| 38 | +const timeMap = new TimeMap(); |
| 39 | +timeMap.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1. |
| 40 | +timeMap.get("foo", 1); // return "bar" |
| 41 | +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". |
| 42 | +timeMap.set("foo", "bar2", 4); // store the key "foo" and value "bar2" along with timestamp = 4. |
| 43 | +timeMap.get("foo", 4); // return "bar2" |
| 44 | +timeMap.get("foo", 5); // return "bar2" |
| 45 | + |
| 46 | +// var TimeMap = function() { |
| 47 | +// this.keyStore = new Map(); |
| 48 | +// }; |
| 49 | + |
| 50 | +// /** |
| 51 | +// * @param {string} key |
| 52 | +// * @param {string} value |
| 53 | +// * @param {number} timestamp |
| 54 | +// * @return {void} |
| 55 | +// */ |
| 56 | +// TimeMap.prototype.set = function(key, value, timestamp) { |
| 57 | +// if (!this.keyStore.has(key)) { |
| 58 | +// this.keyStore.set(key, []); |
| 59 | +// } |
| 60 | +// this.keyStore.get(key).push([timestamp, value]); |
| 61 | +// }; |
| 62 | + |
| 63 | +// /** |
| 64 | +// * @param {string} key |
| 65 | +// * @param {number} timestamp |
| 66 | +// * @return {string} |
| 67 | +// */ |
| 68 | +// TimeMap.prototype.get = function(key, timestamp) { |
| 69 | +// const values = this.keyStore.get(key) || []; |
| 70 | +// let left = 0; |
| 71 | +// let right = values.length - 1; |
| 72 | +// let result = ''; |
| 73 | + |
| 74 | +// while (left <= right) { |
| 75 | +// const mid = Math.floor((left + right) / 2); |
| 76 | +// if (values[mid][0] <= timestamp) { |
| 77 | +// result = values[mid][1]; |
| 78 | +// left = mid + 1; |
| 79 | +// } else { |
| 80 | +// right = mid - 1; |
| 81 | +// } |
| 82 | +// } |
| 83 | + |
| 84 | +// return result; |
| 85 | +// }; |
0 commit comments