1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/originData/interval-cancellation.json

59 lines
14 KiB
JSON
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"data": {
"question": {
"questionId": "2862",
"questionFrontendId": "2725",
"categoryTitle": "JavaScript",
"boundTopicId": 2300584,
"title": "Interval Cancellation",
"titleSlug": "interval-cancellation",
"content": "<p>Given a function <code>fn</code>, an array of arguments&nbsp;<code>args</code>, and&nbsp;an interval time <code>t</code>, return a cancel function <code>cancelFn</code>.</p>\n\n<p>The function <code>fn</code> should be called with <code>args</code> immediately and then called again every&nbsp;<code>t</code> milliseconds&nbsp;until&nbsp;<code>cancelFn</code>&nbsp;is called at <code>cancelTimeMs</code> ms.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x) =&gt; x * 2, args = [4], t = 35\n<strong>Output:</strong> \n[\n {&quot;time&quot;: 0, &quot;returned&quot;: 8},\n {&quot;time&quot;: 35, &quot;returned&quot;: 8},\n {&quot;time&quot;: 70, &quot;returned&quot;: 8},\n {&quot;time&quot;: 105, &quot;returned&quot;: 8},\n {&quot;time&quot;: 140, &quot;returned&quot;: 8},\n {&quot;time&quot;: 175, &quot;returned&quot;: 8}\n]\n<strong>Explanation:</strong> \nconst result = [];\nconst fn = (x) =&gt; x * 2;\nconst cancelTimeMs = 190;\n\nconst start = performance.now();\n\nconst log = (...argsArr) =&gt; {\n const diff = Math.floor(performance.now() - start);\n result.push({&quot;time&quot;: diff, &quot;returned&quot;: fn(...argsArr)});\n}\n\nconst cancel = cancellable(log, [4], 35);\nsetTimeout(cancel, cancelTimeMs);\n\nsetTimeout(() =&gt; {\n console.log(result); // Output\n }, cancelTimeMs + 50) \n\nEvery 35ms, fn(4) is called. Until t=190ms, then it is cancelled.\n1st fn call is at 0ms. fn(4) returns 8.\n2nd fn call is at 35ms. fn(4) returns 8.\n3rd fn call is at 70ms. fn(4) returns 8.\n4th fn call is at&nbsp;105ms. fn(4) returns 8.\n5th fn call is at 140ms. fn(4) returns 8.\n6th fn call is at 175ms. fn(4) returns 8.\nCancelled at 190ms\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x1, x2) =&gt; (x1 * x2), args = [2, 5], t = 30\n<strong>Output:</strong> \n[\n {&quot;time&quot;: 0, &quot;returned&quot;: 10},\n {&quot;time&quot;: 30, &quot;returned&quot;: 10},\n {&quot;time&quot;: 60, &quot;returned&quot;: 10},\n {&quot;time&quot;: 90, &quot;returned&quot;: 10},\n {&quot;time&quot;: 120, &quot;returned&quot;: 10},\n {&quot;time&quot;: 150, &quot;returned&quot;: 10}\n]\n<strong>Explanation:</strong> \nconst cancelTimeMs = 165;\n\nEvery 30ms, fn(2, 5) is called. Until t=165ms, then it is cancelled.\n1st fn call is at 0ms&nbsp;\n2nd fn call is at 30ms&nbsp;\n3rd fn call is at 60ms&nbsp;\n4th fn call is at&nbsp;90ms&nbsp;\n5th fn call is at 120ms&nbsp;\n6th fn call is at 150ms\nCancelled at 165ms\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x1, x2, x3) =&gt; (x1 + x2 + x3), args = [5, 1, 3], t = 50\n<strong>Output:</strong> \n[\n {&quot;time&quot;: 0, &quot;returned&quot;: 9},\n {&quot;time&quot;: 50, &quot;returned&quot;: 9},\n {&quot;time&quot;: 100, &quot;returned&quot;: 9},\n {&quot;time&quot;: 150, &quot;returned&quot;: 9}\n]\n<strong>Explanation:</strong> \nconst cancelTimeMs = 180;\n\nEvery 50ms, fn(5, 1, 3) is called. Until t=180ms, then it is cancelled. \n1st fn call is at 0ms\n2nd fn call is at 50ms\n3rd fn call is at 100ms\n4th fn call is at&nbsp;150ms\nCancelled at 180ms\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>fn</code> is a function</li>\n\t<li><code>args</code> is a valid JSON array</li>\n\t<li><code>1 &lt;= args.length &lt;= 10</code></li>\n\t<li><code><font face=\"monospace\">30 &lt;= t &lt;= 100</font></code></li>\n\t<li><code><font face=\"monospace\">10 &lt;= </font>cancelTimeMs<font face=\"monospace\"> &lt;= 500</font></code></li>\n</ul>\n",
"translatedTitle": "间隔取消",
"translatedContent": "<p>现给定一个函数 <code>fn</code>,一个参数数组 <code>args</code> 和一个时间间隔 <code>t</code>,返回一个取消函数 <code>cancelFn</code>。</p>\n\n<p>函数 <code>fn</code> 应该立即使用 <code>args</code> 调用,并且在每个 <code>t</code> 毫秒内再次调用,直到调用 <code>cancelFn</code>。</p>\n\n<p>&nbsp;</p>\n\n<p><strong class=\"example\">示例 1</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x) =&gt; x * 2, args = [4], t = 35, cancelT = 190\n<b>输出:</b>\n[\n {\"time\": 0, \"returned\": 8},\n {\"time\": 35, \"returned\": 8},\n {\"time\": 70, \"returned\": 8},\n {\"time\": 105, \"returned\": 8},\n {\"time\": 140, \"returned\": 8},\n {\"time\": 175, \"returned\": 8}\n]\n<strong>解释:</strong> \nconst result = []\nconst fn = (x) =&gt; x * 2\nconst args = [4], t = 35, cancelT = 190\n\nconst start = performance.now()\n\nconst log = (...argsArr) =&gt; {\n const diff = Math.floor(performance.now() - start)\n result.push({\"time\": diff, \"returned\": fn(...argsArr)})\n}\n\nconst cancel = cancellable(log, [4], 35);\nsetTimeout(cancel, 190);\n\nsetTimeout(() =&gt; {\n console.log(result) // Output\n }, cancelT + t + 15) \n\n每隔 35ms调用 fn(4)。直到 t=190ms然后取消。\n第一次调用 fn 是在 0ms。fn(4) 返回 8。\n第二次调用 fn 是在 35ms。fn(4) 返回 8。\n第三次调用 fn 是在 70ms。fn(4) 返回 8。\n第四次调用 fn 是在&nbsp;105ms。fn(4) 返回 8。\n第五次调用 fn 是在 140ms。fn(4) 返回 8。\n第六次调用 fn 是在 175ms。fn(4) 返回 8。\n在 t=190ms 时取消\n</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x1, x2) =&gt; (x1 * x2), args = [2, 5], t = 30, cancelT = 165\n<strong>输出:</strong> \n[\n {\"time\": 0, \"returned\": 10},\n {\"time\": 30, \"returned\": 10},\n {\"time\": 60, \"returned\": 10},\n {\"time\": 90, \"returned\": 10},\n {\"time\": 120, \"returned\": 10},\n {\"time\": 150, \"returned\": 10}\n]\n<strong>解释:</strong>\nconst cancel = cancellable((x1, x2) =&gt; (x1 * x2), [2, 5], 30); \nsetTimeout(cancel, 165);\n\n每隔 30ms调用 fn(2, 5)。直到 t=165ms然后取消。\n第一次调用 fn 是在 0ms\n第二次调用 fn 是在 30ms\n第三次调用 fn 是在 60ms\n第四次调用 fn 是在&nbsp;90ms\n第五次调用 fn 是在 120ms\n第六次调用 fn 是在 150ms\n在 165ms 取消\n</pre>\n\n<p><strong class=\"example\">示例 3</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x1, x2, x3) =&gt; (x1 + x2 + x3), args = [5, 1, 3], t = 50, cancelT = 180\n<b>输出:</b>\n[\n {\"time\": 0, \"returned\": 9},\n {\"time\": 50, \"returned\": 9},\n {\"time\": 100, \"returned\": 9},\n {\"time\": 150, \"returned\": 9}\n]\n<b>解释:</b>\nconst cancel = cancellable((x1, x2, x3) =&gt; (x1 + x2 + x3), [5, 1, 3], 50);\nsetTimeout(cancel, cancelT);\n\n每隔 50ms调用 fn(5, 1, 3)。直到 t=180ms然后取消。\n第一次调用 fn 是在 0ms\n第二次调用 fn 是在 50ms\n第三次调用 fn 是在 100ms\n第四次调用 fn 是在&nbsp;150ms\n在 180ms 取消\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>fn</code> 是一个函数</li>\n\t<li><code>args</code> 是一个有效的 JSON 数组</li>\n\t<li><code>1 &lt;= args.length &lt;= 10</code></li>\n\t<li><code><font face=\"monospace\">30 &lt;= t &lt;= 100</font></code></li>\n\t<li><code><font face=\"monospace\">10 &lt;= cancelT &lt;= 500</font></code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 2,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}",
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Function} fn\n * @param {Array} args\n * @param {number} t\n * @return {Function}\n */\nvar cancellable = function(fn, args, t) {\n \n};\n\n/**\n * const result = [];\n *\n * const fn = (x) => x * 2;\n * const args = [4], t = 35, cancelTimeMs = 190;\n *\n * const start = performance.now();\n *\n * const log = (...argsArr) => {\n * const diff = Math.floor(performance.now() - start);\n * result.push({\"time\": diff, \"returned\": fn(...argsArr)});\n * }\n * \n * const cancel = cancellable(log, args, t);\n *\n * setTimeout(cancel, cancelTimeMs);\n * \n * setTimeout(() => {\n * console.log(result); // [\n * // {\"time\":0,\"returned\":8},\n * // {\"time\":35,\"returned\":8},\n * // {\"time\":70,\"returned\":8}, \n * // {\"time\":105,\"returned\":8},\n * // {\"time\":140,\"returned\":8},\n * // {\"time\":175,\"returned\":8}\n * // ]\n * }, cancelTimeMs + t + 15) \n */",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };\ntype Fn = (...args: JSONValue[]) => void\n\nfunction cancellable(fn: Fn, args: JSONValue[], t: number): Function {\n\t\n};\n\n/**\n * const result = [];\n *\n * const fn = (x) => x * 2;\n * const args = [4], t = 35, cancelTimeMs = 190;\n *\n * const start = performance.now();\n *\n * const log = (...argsArr) => {\n * const diff = Math.floor(performance.now() - start);\n * result.push({\"time\": diff, \"returned\": fn(...argsArr)});\n * }\n * \n * const cancel = cancellable(log, args, t);\n *\n * setTimeout(cancel, cancelTimeMs);\n * \n * setTimeout(() => {\n * console.log(result); // [\n * // {\"time\":0,\"returned\":8},\n * // {\"time\":35,\"returned\":8},\n * // {\"time\":70,\"returned\":8}, \n * // {\"time\":105,\"returned\":8},\n * // {\"time\":140,\"returned\":8},\n * // {\"time\":175,\"returned\":8}\n * // ]\n * }, cancelTimeMs + t + 15) \n */",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"2.5K\", \"totalSubmission\": \"3.6K\", \"totalAcceptedRaw\": 2479, \"totalSubmissionRaw\": 3591, \"acRate\": \"69.0%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "(x) => x * 2\n[4]\n35\n190",
"metaData": "{\n \"name\": \"cancellable\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"args\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"t\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"cancelTimeMs\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 5.1.6<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "(x) => x * 2\n[4]\n35\n190\n(x1, x2) => (x1 * x2)\n[2,5]\n30\n165\n(x1, x2, x3) => (x1 + x2 + x3)\n[5,1,3]\n50\n180",
"__typename": "QuestionNode"
}
}
}