mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			72 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>Given an array of asyncronous functions <code>functions</code> and a <strong>pool limit</strong> <code>n</code>, return an asyncronous function <code>promisePool</code>. It should return a promise that resolves when all the input functions resolve.</p>
 | 
						|
 | 
						|
<p><b>Pool limit</b> is defined as the maximum number promises that can be pending at once. <code>promisePool</code> should begin execution of as many functions as possible and continue executing new functions when old promises resolve. <code>promisePool</code> should execute <code>functions[i]</code> then <code>functions[i + 1]</code> then <code>functions[i + 2]</code>, etc. When the last promise resolves, <code>promisePool</code> should also resolve.</p>
 | 
						|
 | 
						|
<p>For example, if <code>n = 1</code>, <code>promisePool</code> will execute one function at a time in series. However, if <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>
 | 
						|
 | 
						|
<p>You can assume all <code>functions</code> never reject. It is acceptable for <code>promisePool</code> to return a promise that resolves any value.</p>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong class="example">Example 1:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> 
 | 
						|
functions = [
 | 
						|
  () => new Promise(res => setTimeout(res, 300)),
 | 
						|
  () => new Promise(res => setTimeout(res, 400)),
 | 
						|
  () => new Promise(res => setTimeout(res, 200))
 | 
						|
]
 | 
						|
n = 2
 | 
						|
<strong>Output:</strong> [[300,400,500],500]
 | 
						|
<strong>Explanation:</strong>
 | 
						|
Three functions are passed in. They sleep for 300ms, 400ms, and 200ms respectively.
 | 
						|
At t=0, the first 2 functions are executed. The pool size limit of 2 is reached.
 | 
						|
At t=300, the 1st function resolves, and the 3rd function is executed. Pool size is 2.
 | 
						|
At t=400, the 2nd function resolves. There is nothing left to execute. Pool size is 1.
 | 
						|
At t=500, the 3rd function resolves. Pool size is zero so the returned promise also resolves.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong class="example">Example 2:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:
 | 
						|
</strong>functions = [
 | 
						|
  () => new Promise(res => setTimeout(res, 300)),
 | 
						|
  () => new Promise(res => setTimeout(res, 400)),
 | 
						|
  () => new Promise(res => setTimeout(res, 200))
 | 
						|
]
 | 
						|
n = 5
 | 
						|
<strong>Output:</strong> [[300,400,200],400]
 | 
						|
<strong>Explanation:</strong>
 | 
						|
At t=0, all 3 functions are executed. The pool limit of 5 is never met.
 | 
						|
At t=200, the 3rd function resolves. Pool size is 2.
 | 
						|
At t=300, the 1st function resolved. Pool size is 1.
 | 
						|
At t=400, the 2nd function resolves. Pool size is 0, so the returned promise also resolves.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong class="example">Example 3:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong>
 | 
						|
functions = [
 | 
						|
  () => new Promise(res => setTimeout(res, 300)),
 | 
						|
  () => new Promise(res => setTimeout(res, 400)),
 | 
						|
  () => new Promise(res => setTimeout(res, 200))
 | 
						|
]
 | 
						|
n = 1
 | 
						|
<strong>Output:</strong> [[300,700,900],900]
 | 
						|
<strong>Explanation:</strong>
 | 
						|
At t=0, the 1st function is executed. Pool size is 1.
 | 
						|
At t=300, the 1st function resolves and the 2nd function is executed. Pool size is 1.
 | 
						|
At t=700, the 2nd function resolves and the 3rd function is executed. Pool size is 1.
 | 
						|
At t=900, the 3rd function resolves. Pool size is 0 so the returned promise resolves.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>0 <= functions.length <= 10</code></li>
 | 
						|
	<li><code><font face="monospace">1 <= n <= 10</font></code></li>
 | 
						|
</ul>
 |