mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
63 lines
8.0 KiB
JSON
63 lines
8.0 KiB
JSON
{
|
|
"data": {
|
|
"question": {
|
|
"questionId": "2749",
|
|
"questionFrontendId": "2637",
|
|
"boundTopicId": null,
|
|
"title": "Promise Time Limit",
|
|
"titleSlug": "promise-time-limit",
|
|
"content": "<p>Given an asyncronous function <code>fn</code> and a time <code>t</code> in milliseconds, return a new <strong>time limited</strong> version of the input function.</p>\n\n<p>A <strong>time limited</strong> function is a function that is identical to the original unless it takes longer than <code>t</code> milliseconds to fullfill. In that case, it will reject with <code>"Time Limit Exceeded"</code>. Note that it should reject with a string, not an <code>Error</code>.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = async (n) => { \n await new Promise(res => setTimeout(res, 100)); \n return n * n; \n}\ninputs = [5]\nt = 50\n<strong>Output:</strong> {"rejected":"Time Limit Exceeded","time":50}\n<strong>Explanation:</strong>\nThe provided function is set to resolve after 100ms. However, the time limit is set to 50ms. It rejects at t=50ms because the time limit was reached.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = async (n) => { \n await new Promise(res => setTimeout(res, 100)); \n return n * n; \n}\ninputs = [5]\nt = 150\n<strong>Output:</strong> {"resolved":25,"time":100}\n<strong>Explanation:</strong>\nThe function resolved 5 * 5 = 25 at t=100ms. The time limit is never reached.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = async (a, b) => { \n await new Promise(res => setTimeout(res, 120)); \n return a + b; \n}\ninputs = [5,10]\nt = 150\n<strong>Output:</strong> {"resolved":15,"time":120}\n<strong>Explanation:</strong>\nThe function resolved 5 + 10 = 15 at t=120ms. The time limit is never reached.\n</pre>\n\n<p><strong class=\"example\">Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = async () => { \n throw "Error";\n}\ninputs = []\nt = 1000\n<strong>Output:</strong> {"rejected":"Error","time":0}\n<strong>Explanation:</strong>\nThe function immediately throws an error.</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= inputs.length <= 10</code></li>\n\t<li><code>0 <= t <= 1000</code></li>\n\t<li><code>fn returns a promise</code></li>\n</ul>\n",
|
|
"translatedTitle": null,
|
|
"translatedContent": null,
|
|
"isPaidOnly": false,
|
|
"difficulty": "Easy",
|
|
"likes": 24,
|
|
"dislikes": 3,
|
|
"isLiked": null,
|
|
"similarQuestions": "[{\"title\": \"Sleep\", \"titleSlug\": \"sleep\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Debounce\", \"titleSlug\": \"debounce\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Promise Pool\", \"titleSlug\": \"promise-pool\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Cache With Time Limit\", \"titleSlug\": \"cache-with-time-limit\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
|
"exampleTestcases": "async (n) => { await new Promise(res => setTimeout(res, 100)); return n * n; }\n[5]\n50\nasync (n) => { await new Promise(res => setTimeout(res, 100)); return n * n; }\n[5]\n150\nasync (a, b) => { await new Promise(res => setTimeout(res, 120)); return a + b; }\n[5,10]\n150\nasync () => { throw \"Error\"; }\n[]\n1000",
|
|
"categoryTitle": "JavaScript",
|
|
"contributors": [],
|
|
"topicTags": [],
|
|
"companyTagStats": null,
|
|
"codeSnippets": [
|
|
{
|
|
"lang": "JavaScript",
|
|
"langSlug": "javascript",
|
|
"code": "/**\n * @param {Function} fn\n * @param {number} t\n * @return {Function}\n */\nvar timeLimit = function(fn, t) {\n\treturn async function(...args) {\n \n }\n};\n\n/**\n * const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100);\n * limited(150).catch(console.log) // \"Time Limit Exceeded\" at t=100ms\n */",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "TypeScript",
|
|
"langSlug": "typescript",
|
|
"code": "type Fn = (...params: any[]) => Promise<any>;\n\nfunction timeLimit(fn: Fn, t: number): Fn {\n\treturn async function(...args) {\n \n }\n};\n\n/**\n * const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100);\n * limited(150).catch(console.log) // \"Time Limit Exceeded\" at t=100ms\n */",
|
|
"__typename": "CodeSnippetNode"
|
|
}
|
|
],
|
|
"stats": "{\"totalAccepted\": \"494\", \"totalSubmission\": \"593\", \"totalAcceptedRaw\": 494, \"totalSubmissionRaw\": 593, \"acRate\": \"83.3%\"}",
|
|
"hints": [
|
|
"You can return a copy of a function with: \r\n\r\nfunction outerFunction(fn) { \r\n return function innerFunction(...params) {\r\n return fn(...params);\r\n };\r\n}",
|
|
"Inside the inner function, you will need to return a new Promise.",
|
|
"You can create a new promise like: new Promise((resolve, reject) => {}).",
|
|
"You can execute code with a delay with \"setTimeout(fn, delay)\"",
|
|
"To reject a promise after a delay, \"setTimeout(() => reject('err'), delay)\"",
|
|
"You can resolve and reject when the passed promise resolves or rejects with: \"fn(...params).then(resolve).catch(reject)\""
|
|
],
|
|
"solution": null,
|
|
"status": null,
|
|
"sampleTestCase": "async (n) => { await new Promise(res => setTimeout(res, 100)); return n * n; }\n[5]\n50",
|
|
"metaData": "{\n \"name\": \"timeLimit\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"inputs\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"t\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\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"
|
|
}
|
|
}
|
|
} |