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/promise-pool.json
2023-04-23 22:41:08 +08:00

62 lines
13 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": "2750",
"questionFrontendId": "2636",
"categoryTitle": "JavaScript",
"boundTopicId": 2222268,
"title": "Promise Pool",
"titleSlug": "promise-pool",
"content": "<p>Given an array&nbsp;of asyncronous functions&nbsp;<code>functions</code>&nbsp;and a <strong>pool limit</strong>&nbsp;<code>n</code>, return an asyncronous function&nbsp;<code>promisePool</code>. It should return&nbsp;a promise that resolves when all the input&nbsp;functions resolve.</p>\n\n<p><b>Pool limit</b> is defined as the maximum number promises that can be pending at once.&nbsp;<code>promisePool</code>&nbsp;should begin execution of as many functions as possible and continue executing new functions when old promises&nbsp;resolve.&nbsp;<code>promisePool</code>&nbsp;should execute <code>functions[i]</code>&nbsp;then <code>functions[i + 1]</code>&nbsp;then <code>functions[i + 2]</code>, etc. When the last promise resolves,&nbsp;<code>promisePool</code>&nbsp;should also resolve.</p>\n\n<p>For example, if&nbsp;<code>n = 1</code>, <code>promisePool</code>&nbsp;will execute one function at&nbsp;a time in&nbsp;series. However, if&nbsp;<code>n = 2</code>, it first executes two functions. When either of the two functions resolve, a 3rd function should be executed (if available), and so on until there are no functions left to execute.</p>\n\n<p>You can assume all&nbsp;<code>functions</code>&nbsp;never reject. It is acceptable for&nbsp;<code>promisePool</code>&nbsp;to return a promise that resolves any value.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfunctions = [\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 300)),\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 400)),\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 200))\n]\nn = 2\n<strong>Output:</strong> [[300,400,500],500]\n<strong>Explanation:</strong>\nThree functions are passed in. They sleep for 300ms, 400ms, and 200ms respectively.\nAt t=0, the first 2 functions are executed. The pool size limit of 2 is reached.\nAt t=300, the 1st function resolves, and the 3rd function is executed. Pool size is 2.\nAt t=400, the 2nd function resolves. There is nothing left to execute. Pool size is 1.\nAt t=500, the 3rd function resolves. Pool size is zero so the returned promise also resolves.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:\n</strong>functions = [\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 300)),\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 400)),\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 200))\n]\nn = 5\n<strong>Output:</strong> [[300,400,200],400]\n<strong>Explanation:</strong>\nAt t=0, all 3 functions are executed. The pool limit of 5 is never met.\nAt t=200, the 3rd function resolves. Pool size is 2.\nAt t=300, the 1st function resolved. Pool size is 1.\nAt t=400, the 2nd function resolves. Pool size is 0, so the returned promise also resolves.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfunctions = [\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 300)),\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 400)),\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 200))\n]\nn = 1\n<strong>Output:</strong> [[300,700,900],900]\n<strong>Explanation:</strong>\nAt t=0, the 1st function is executed. Pool size is 1.\nAt t=300, the 1st function resolves and the 2nd function is executed. Pool size is 1.\nAt t=700, the 2nd function resolves and the 3rd function is executed. Pool size is 1.\nAt t=900, the 3rd function resolves. Pool size is 0 so the returned promise resolves.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 &lt;= functions.length &lt;= 10</code></li>\n\t<li><code><font face=\"monospace\">1 &lt;= n &lt;= 10</font></code></li>\n</ul>\n",
"translatedTitle": "Promice 对象池",
"translatedContent": "<p>请你编写一个异步函数 <code>promisePool</code> ,它接收一个异步函数数组 <code>functions</code> 和 <strong>池限制</strong> <code>n</code>。它应该返回一个 promise 对象当所有输入函数都执行完毕后promise 对象就执行完毕。</p>\n\n<p><strong>池限制</strong> 定义是一次可以挂起的最多 promise 对象的数量。<code>promisePool</code> 应该开始执行尽可能多的函数,并在旧的 promise 执行完毕后继续执行新函数。<code>promisePool</code> 应该先执行 <code>functions[i]</code>,再执行 <code>functions[i + 1]</code>,然后执行&nbsp;<code>functions[i + 2]</code>,等等。当最后一个 promise 执行完毕时,<code>promisePool</code> 也应该执行完毕。</p>\n\n<p>例如,如果 <code>n = 1</code> , <code>promisePool</code>&nbsp;在序列中每次执行一个函数。然而,如果 <code>n = 2</code> ,它首先执行两个函数。当两个函数中的任何一个执行完毕后,再执行第三个函数(如果它是可用的),依此类推,直到没有函数要执行为止。</p>\n\n<p>你可以假设所有的 <code>functions</code> 都不会被拒绝。对于 <code>promisePool</code> 来说,返回一个可以解析任何值的 promise 都是可以接受的。</p>\n\n<p>&nbsp;</p>\n\n<p><strong class=\"example\">示例 1</strong></p>\n\n<pre>\n<b>输入:</b>\nfunctions = [\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 300)),\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 400)),\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 200))\n]\nn = 2\n<b>输出:</b>[[300,400,500],500]\n<strong>解释</strong>\n传递了三个函数。它们的睡眠时间分别为 300ms、 400ms 和 200ms。\n在 t=0 时,执行前两个函数。池大小限制达到 2。\n当 t=300 时第一个函数执行完毕后执行第3个函数。池大小为 2。\n在 t=400 时,第二个函数执行完毕后。没有什么可执行的了。池大小为 1。\n在 t=500 时,第三个函数执行完毕后。池大小为 0因此返回的 promise 也执行完成。\n</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<strong>输入:\n</strong>functions = [\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 300)),\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 400)),\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 200))\n]\nn = 5\n<b>输出:</b>[[300,400,200],400]\n<strong>解释:</strong>\n在 t=0 时所有3个函数都被执行。池的限制大小 5 永远不会满足。\n在 t=200 时,第三个函数执行完毕后。池大小为 2。\n在 t=300 时,第一个函数执行完毕后。池大小为 1。\n在 t=400 时,第二个函数执行完毕后。池大小为 0因此返回的 promise 也执行完成。\n</pre>\n\n<p><strong class=\"example\">示例 3</strong></p>\n\n<pre>\n<strong>输入:</strong>\nfunctions = [\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 300)),\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 400)),\n&nbsp; () =&gt; new Promise(res =&gt; setTimeout(res, 200))\n]\nn = 1\n<b>输出:</b>[[300,700,900],900]\n<strong>解释:</strong>\n在 t=0 时执行第一个函数。池大小为1。\n当 t=300 时,第一个函数执行完毕后,执行第二个函数。池大小为 1。\n当 t=700 时,第二个函数执行完毕后,执行第三个函数。池大小为 1。\n在 t=900 时,第三个函数执行完毕后。池大小为 0因此返回的 Promise 也执行完成。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>0 &lt;= functions.length &lt;= 10</code></li>\n\t<li><code><font face=\"monospace\">1 &lt;= n &lt;= 10</font></code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Medium",
"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, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Function[]} functions\n * @param {number} n\n * @return {Function}\n */\nvar promisePool = async function(functions, n) {\n\n};\n\n/**\n * const sleep = (t) => new Promise(res => setTimeout(res, t));\n * promisePool([() => sleep(500), () => sleep(400)], 1)\n * .then(console.log) // After 900ms\n */",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "type F = () => Promise<any>;\n\nfunction promisePool(functions: F[], n: number): Promise<any> {\n\n};\n\n/**\n * const sleep = (t) => new Promise(res => setTimeout(res, t));\n * promisePool([() => sleep(500), () => sleep(400)], 1)\n * .then(console.log) // After 900ms\n */",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"112\", \"totalSubmission\": \"154\", \"totalAcceptedRaw\": 112, \"totalSubmissionRaw\": 154, \"acRate\": \"72.7%\"}",
"hints": [
"Initially execute all the functions until the queue fills up.",
"Every time a function resolves, add a new promise to the queue if possible."
],
"solution": null,
"status": null,
"sampleTestCase": "[() => new Promise(res => setTimeout(res, 300)), () => new Promise(res => setTimeout(res, 400)), () => new Promise(res => setTimeout(res, 200))]\n2",
"metaData": "{\n \"name\": \"promisePool\",\n \"params\": [\n {\n \"name\": \"getFunctions\",\n \"type\": \"string\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"n\"\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,
"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 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/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": "[() => new Promise(res => setTimeout(res, 300)), () => new Promise(res => setTimeout(res, 400)), () => new Promise(res => setTimeout(res, 200))]\n2\n[() => new Promise(res => setTimeout(res, 300)), () => new Promise(res => setTimeout(res, 400)), () => new Promise(res => setTimeout(res, 200))]\n5\n[() => new Promise(res => setTimeout(res, 300)), () => new Promise(res => setTimeout(res, 400)), () => new Promise(res => setTimeout(res, 200))]\n1",
"__typename": "QuestionNode"
}
}
}