mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
63 lines
9.0 KiB
JSON
63 lines
9.0 KiB
JSON
{
|
|
"data": {
|
|
"question": {
|
|
"questionId": "2762",
|
|
"questionFrontendId": "2622",
|
|
"boundTopicId": null,
|
|
"title": "Cache With Time Limit",
|
|
"titleSlug": "cache-with-time-limit",
|
|
"content": "<p>Write a class that allows getting and setting key-value pairs, however a <strong>time until expiration</strong> is associated with each key.</p>\n\n<p>The class has three public methods:</p>\n\n<p><code>set(key, value, duration)</code>: accepts an integer <code>key</code>, an integer <code>value</code>, and a <code>duration</code> in milliseconds. Once the <code>duration</code> has elapsed, the key should be inaccessible. The method should return <code>true</code> if the same un-expired key already exists and <code>false</code> otherwise. Both the value and duration should be overwritten if the key already exists.</p>\n\n<p><code>get(key)</code>: if an un-expired key exists, it should return the associated value. Otherwise it should return <code>-1</code>.</p>\n\n<p><code>count()</code>: returns the count of un-expired keys.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \n["TimeLimitedCache", "set", "get", "count", "get"]\n[[], [1, 42, 100], [1], [], [1]]\n[0, 0, 50, 50, 150]\n<strong>Output:</strong> [null, false, 42, 1, -1]\n<strong>Explanation:</strong>\nAt t=0, the cache is constructed.\nAt t=0, a key-value pair (1: 42) is added with a time limit of 100ms. The value doesn't exist so false is returned.\nAt t=50, key=1 is requested and the value of 42 is returned.\nAt t=50, count() is called and there is one active key in the cache.\nAt t=100, key=1 expires.\nAt t=150, get(1) is called but -1 is returned because the cache is empty.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \n["TimeLimitedCache", "set", "set", "get", "get", "get", "count"]\n[[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []]\n[0, 0, 40, 50, 120, 200, 250]\n<strong>Output:</strong> [null, false, true, 50, 50, -1]\n<strong>Explanation:</strong>\nAt t=0, the cache is constructed.\nAt t=0, a key-value pair (1: 42) is added with a time limit of 50ms. The value doesn't exist so false is returned.\nAt t=40, a key-value pair (1: 50) is added with a time limit of 100ms. A non-expired value already existed so true is returned and the old value was overwritten.\nAt t=50, get(1) is called which returned 50.\nAt t=120, get(1) is called which returned 50.\nAt t=140, key=1 expires.\nAt t=200, get(1) is called but the cache is empty so -1 is returned.\nAt t=250, count() returns 0 because the cache is empty.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= key <= 10<sup>9</sup></code></li>\n\t<li><code>0 <= value <= 10<sup>9</sup></code></li>\n\t<li><code>0 <= duration <= 1000</code></li>\n\t<li><code>total method calls will not exceed 100</code></li>\n</ul>\n",
|
|
"translatedTitle": null,
|
|
"translatedContent": null,
|
|
"isPaidOnly": false,
|
|
"difficulty": "Medium",
|
|
"likes": 13,
|
|
"dislikes": 2,
|
|
"isLiked": null,
|
|
"similarQuestions": "[{\"title\": \"Debounce\", \"titleSlug\": \"debounce\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Promise Time Limit\", \"titleSlug\": \"promise-time-limit\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Promise Pool\", \"titleSlug\": \"promise-pool\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
|
"exampleTestcases": "[\"TimeLimitedCache\", \"set\", \"get\", \"count\", \"get\"]\n[[], [1, 42, 100], [1], [], [1]]\n[0, 0, 50, 50, 150]\n[\"TimeLimitedCache\", \"set\", \"set\", \"get\", \"get\", \"get\", \"count\"]\n[[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []]\n[0, 0, 40, 50, 120, 200, 250]",
|
|
"categoryTitle": "JavaScript",
|
|
"contributors": [],
|
|
"topicTags": [],
|
|
"companyTagStats": null,
|
|
"codeSnippets": [
|
|
{
|
|
"lang": "JavaScript",
|
|
"langSlug": "javascript",
|
|
"code": "\nvar TimeLimitedCache = function() {\n \n};\n\n/** \n * @param {number} key\n * @param {number} value\n * @param {number} time until expiration in ms\n * @return {boolean} if un-expired key already existed\n */\nTimeLimitedCache.prototype.set = function(key, value, duration) {\n \n};\n\n/** \n * @param {number} key\n * @return {number} value associated with key\n */\nTimeLimitedCache.prototype.get = function(key) {\n \n};\n\n/** \n * @return {number} count of non-expired keys\n */\nTimeLimitedCache.prototype.count = function() {\n \n};\n\n/**\n * Your TimeLimitedCache object will be instantiated and called as such:\n * var obj = new TimeLimitedCache()\n * obj.set(1, 42, 1000); // false\n * obj.get(1) // 42\n * obj.count() // 1\n */",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "TypeScript",
|
|
"langSlug": "typescript",
|
|
"code": "class TimeLimitedCache {\n constructor() {\n\n }\n\n set(key: number, value: number, duration: number): boolean {\n\n }\n\n get(key: number): number {\n\n }\n\n\tcount(): number {\n \n }\n}\n\n/**\n * Your TimeLimitedCache object will be instantiated and called as such:\n * var obj = new TimeLimitedCache()\n * obj.set(1, 42, 1000); // false\n * obj.get(1) // 42\n * obj.count() // 1\n */",
|
|
"__typename": "CodeSnippetNode"
|
|
}
|
|
],
|
|
"stats": "{\"totalAccepted\": \"375\", \"totalSubmission\": \"506\", \"totalAcceptedRaw\": 375, \"totalSubmissionRaw\": 506, \"acRate\": \"74.1%\"}",
|
|
"hints": [
|
|
"You can delay execution of code with \"ref = setTimeout(fn, delay)\". You can abort the execution with \"clearTimeout(ref)\"",
|
|
"When storing the values in the cache, also store a reference to the timeout. The timeout should clear the key from the cache after the expiration has elapsed.",
|
|
"When you set a key that already exists, clear the existing timeout.",
|
|
"You can delay execution of code with \"ref = setTimeout(fn, delay)\". You can abort the execution with \"clearTimeout(ref)\"",
|
|
"When storing the values in the cache, also store a reference to the timeout. The timeout should clear the key from the cache after the expiration has elapsed.",
|
|
"When you set a key that already exists, clear the existing timeout."
|
|
],
|
|
"solution": null,
|
|
"status": null,
|
|
"sampleTestCase": "[\"TimeLimitedCache\", \"set\", \"get\", \"count\", \"get\"]\n[[], [1, 42, 100], [1], [], [1]]\n[0, 0, 50, 50, 150]",
|
|
"metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"methods\",\n \"type\": \"string[]\"\n },\n {\n \"type\": \"integer[]\",\n \"name\": \"inputs\"\n },\n {\n \"type\": \"integer[]\",\n \"name\": \"ts\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"typescript\",\n \"javascript\"\n ],\n \"manual\": true\n}",
|
|
"judgerAvailable": true,
|
|
"judgeType": "large",
|
|
"mysqlSchemas": [],
|
|
"enableRunCode": true,
|
|
"enableTestMode": false,
|
|
"enableDebugger": false,
|
|
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
|
"libraryUrl": null,
|
|
"adminUrl": null,
|
|
"challengeQuestion": null,
|
|
"__typename": "QuestionNode"
|
|
}
|
|
}
|
|
} |