Skip to content

Commit d35f99f

Browse files
committed
feat: add solutions to lc problem: No.2636
No.2636.Promise Pool
1 parent 2c81d40 commit d35f99f

File tree

6 files changed

+88
-2
lines changed

6 files changed

+88
-2
lines changed

solution/2600-2699/2636.Promise Pool/README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,19 @@ n = 1
9191
<!-- 这里可写当前语言的特殊实现逻辑 -->
9292

9393
```ts
94-
94+
type F = () => Promise<any>;
95+
96+
function promisePool(functions: F[], n: number): Promise<any> {
97+
const wrappers = functions.map(fn => async () => {
98+
await fn();
99+
const nxt = waiting.shift();
100+
nxt && (await nxt());
101+
});
102+
103+
const running = wrappers.slice(0, n);
104+
const waiting = wrappers.slice(n);
105+
return Promise.all(running.map(fn => fn()));
106+
}
95107
```
96108

97109
### **...**

solution/2600-2699/2636.Promise Pool/README_EN.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,19 @@ At t=900, the 3rd function resolves. Pool size is 0 so the returned promise reso
8383
### **TypeScript**
8484

8585
```ts
86-
86+
type F = () => Promise<any>;
87+
88+
function promisePool(functions: F[], n: number): Promise<any> {
89+
const wrappers = functions.map(fn => async () => {
90+
await fn();
91+
const nxt = waiting.shift();
92+
nxt && (await nxt());
93+
});
94+
95+
const running = wrappers.slice(0, n);
96+
const waiting = wrappers.slice(n);
97+
return Promise.all(running.map(fn => fn()));
98+
}
8799
```
88100

89101
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
type F = () => Promise<any>;
2+
3+
function promisePool(functions: F[], n: number): Promise<any> {
4+
const wrappers = functions.map(fn => async () => {
5+
await fn();
6+
const nxt = waiting.shift();
7+
nxt && (await nxt());
8+
});
9+
10+
const running = wrappers.slice(0, n);
11+
const waiting = wrappers.slice(n);
12+
return Promise.all(running.map(fn => fn()));
13+
}

solution/2600-2699/2637.Promise Time Limit/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,23 @@ t = 1000
9191
<!-- 这里可写当前语言的特殊实现逻辑 -->
9292

9393
```ts
94+
type Fn = (...params: any[]) => Promise<any>;
95+
96+
function timeLimit(fn: Fn, t: number): Fn {
97+
return async function (...args) {
98+
return Promise.race([
99+
fn(...args),
100+
new Promise((_, reject) =>
101+
setTimeout(() => reject('Time Limit Exceeded'), t),
102+
),
103+
]);
104+
};
105+
}
94106

107+
/**
108+
* const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100);
109+
* limited(150).catch(console.log) // "Time Limit Exceeded" at t=100ms
110+
*/
95111
```
96112

97113
### **...**

solution/2600-2699/2637.Promise Time Limit/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,23 @@ The function immediately throws an error.</pre>
8383
### **TypeScript**
8484

8585
```ts
86+
type Fn = (...params: any[]) => Promise<any>;
87+
88+
function timeLimit(fn: Fn, t: number): Fn {
89+
return async function (...args) {
90+
return Promise.race([
91+
fn(...args),
92+
new Promise((_, reject) =>
93+
setTimeout(() => reject('Time Limit Exceeded'), t),
94+
),
95+
]);
96+
};
97+
}
8698

99+
/**
100+
* const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100);
101+
* limited(150).catch(console.log) // "Time Limit Exceeded" at t=100ms
102+
*/
87103
```
88104

89105
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
type Fn = (...params: any[]) => Promise<any>;
2+
3+
function timeLimit(fn: Fn, t: number): Fn {
4+
return async function (...args) {
5+
return Promise.race([
6+
fn(...args),
7+
new Promise((_, reject) =>
8+
setTimeout(() => reject('Time Limit Exceeded'), t),
9+
),
10+
]);
11+
};
12+
}
13+
14+
/**
15+
* const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100);
16+
* limited(150).catch(console.log) // "Time Limit Exceeded" at t=100ms
17+
*/

0 commit comments

Comments
 (0)