Skip to content

Commit 9981d45

Browse files
authored
feat: add solutions to lc problem: No.2802 (doocs#1378)
No.2802.Find The K-th Lucky Number
1 parent 0a3db80 commit 9981d45

File tree

14 files changed

+537
-100
lines changed

14 files changed

+537
-100
lines changed

solution/2700-2799/2715.Execute Cancellable Function With Delay/README.md

-80
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [2715. Timeout Cancellation](https://leetcode.cn/problems/timeout-cancellation)
2+
3+
[English Version](/solution/2700-2799/2715.Timeout%20Cancellation/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Given a function <code>fn</code>, an array of&nbsp;arguments&nbsp;<code>args</code>, and a timeout&nbsp;<code>t</code>&nbsp;in milliseconds, return a cancel function <code>cancelFn</code>.</p>
10+
11+
<p>After a delay of&nbsp;<code>t</code>,&nbsp;<code>fn</code>&nbsp;should be called with <code>args</code> passed as parameters <strong>unless</strong> <code>cancelFn</code> was invoked before the delay of <code>t</code> milliseconds elapses, specifically at <code>cancelT</code>&nbsp;ms.&nbsp;In that case,&nbsp;<code>fn</code> should never be called.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> fn = (x) =&gt; x * 5, args = [2], t = 20, cancelT = 50
18+
<strong>Output:</strong> [{&quot;time&quot;: 20, &quot;returned&quot;: 10}]
19+
<strong>Explanation:</strong>
20+
const cancel = cancellable((x) =&gt; x * 5, [2], 20); // fn(2) called at t=20ms
21+
setTimeout(cancel, 50);
22+
23+
The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened after the execution of fn(2) at 20ms.
24+
</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> fn = (x) =&gt; x**2, args = [2], t = 100, cancelT = 50
30+
<strong>Output:</strong> []
31+
<strong>Explanation:</strong>
32+
const cancel = cancellable((x) =&gt; x**2, [2], 100); // fn(2) not called
33+
setTimeout(cancel, 50);
34+
35+
The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called.
36+
</pre>
37+
38+
<p><strong class="example">Example 3:</strong></p>
39+
40+
<pre>
41+
<strong>Input:</strong> fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30, cancelT = 100
42+
<strong>Output:</strong> [{&quot;time&quot;: 30, &quot;returned&quot;: 8}]
43+
<strong>Explanation:</strong>
44+
const cancel = cancellable((x1, x2) =&gt; x1 * x2, [2,4], 30); // fn(2,4) called at t=30ms
45+
setTimeout(cancel, 100);
46+
47+
The cancellation was scheduled to occur after a delay of cancelT (100ms), which happened after the execution of fn(2,4) at 30ms.
48+
</pre>
49+
50+
<p>&nbsp;</p>
51+
<p><strong>Constraints:</strong></p>
52+
53+
<ul>
54+
<li><code>fn is a function</code></li>
55+
<li><code>args is a valid JSON array</code></li>
56+
<li><code>1 &lt;= args.length &lt;= 10</code></li>
57+
<li><code><font face="monospace">20 &lt;= t &lt;= 1000</font></code></li>
58+
<li><code><font face="monospace">10 &lt;= cancelT &lt;= 1000</font></code></li>
59+
</ul>
60+
61+
## 解法
62+
63+
<!-- 这里可写通用的实现逻辑 -->
64+
65+
<!-- tabs:start -->
66+
67+
### **TypeScript**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```ts
72+
73+
```
74+
75+
<!-- tabs:end -->

solution/2700-2799/2715.Execute Cancellable Function With Delay/README_EN.md solution/2700-2799/2715.Timeout Cancellation/README_EN.md

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,48 @@
1-
# [2715. Execute Cancellable Function With Delay](https://leetcode.com/problems/execute-cancellable-function-with-delay)
1+
# [2715. Timeout Cancellation](https://leetcode.com/problems/timeout-cancellation)
22

3-
[中文文档](/solution/2700-2799/2715.Execute%20Cancellable%20Function%20With%20Delay/README.md)
3+
[中文文档](/solution/2700-2799/2715.Timeout%20Cancellation/README.md)
44

55
## Description
66

7-
<p>Given a function <code>fn</code>, an array or arguments&nbsp;<code>args</code>, and a timeout&nbsp;<code>t</code>&nbsp;in milliseconds, return a cancel function <code>cancelFn</code>.</p>
7+
<p>Given a function <code>fn</code>, an array of&nbsp;arguments&nbsp;<code>args</code>, and a timeout&nbsp;<code>t</code>&nbsp;in milliseconds, return a cancel function <code>cancelFn</code>.</p>
88

9-
<p>After a delay of&nbsp;<code>t</code>,&nbsp;<code>fn</code>&nbsp;should be called with <code>args</code> passed as parameters <strong>unless</strong> <code>cancelFn</code> was called first. In that case,&nbsp;<code>fn</code> should never be called.</p>
9+
<p>After a delay of&nbsp;<code>t</code>,&nbsp;<code>fn</code>&nbsp;should be called with <code>args</code> passed as parameters <strong>unless</strong> <code>cancelFn</code> was invoked before the delay of <code>t</code> milliseconds elapses, specifically at <code>cancelT</code>&nbsp;ms.&nbsp;In that case,&nbsp;<code>fn</code> should never be called.</p>
1010

1111
<p>&nbsp;</p>
1212
<p><strong class="example">Example 1:</strong></p>
1313

1414
<pre>
15-
<strong>Input:</strong> fn = (x) =&gt; x * 5, args = [2], t = 20
15+
<strong>Input:</strong> fn = (x) =&gt; x * 5, args = [2], t = 20, cancelT = 50
1616
<strong>Output:</strong> [{&quot;time&quot;: 20, &quot;returned&quot;: 10}]
1717
<strong>Explanation:</strong>
18-
const cancelTime = 50
1918
const cancel = cancellable((x) =&gt; x * 5, [2], 20); // fn(2) called at t=20ms
20-
setTimeout(cancel, cancelTime);
19+
setTimeout(cancel, 50);
2120

22-
The cancellation was scheduled to occur after a delay of cancelTime (50ms), which happened after the execution of fn(2) at 20ms.
21+
The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened after the execution of fn(2) at 20ms.
2322
</pre>
2423

2524
<p><strong class="example">Example 2:</strong></p>
2625

2726
<pre>
28-
<strong>Input:</strong> fn = (x) =&gt; x**2, args = [2], t = 100
27+
<strong>Input:</strong> fn = (x) =&gt; x**2, args = [2], t = 100, cancelT = 50
2928
<strong>Output:</strong> []
3029
<strong>Explanation:</strong>
31-
const cancelTime = 50
3230
const cancel = cancellable((x) =&gt; x**2, [2], 100); // fn(2) not called
33-
setTimeout(cancel, cancelTime);
31+
setTimeout(cancel, 50);
3432

35-
The cancellation was scheduled to occur after a delay of cancelTime (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called.
33+
The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called.
3634
</pre>
3735

3836
<p><strong class="example">Example 3:</strong></p>
3937

4038
<pre>
41-
<strong>Input:</strong> fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30
39+
<strong>Input:</strong> fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30, cancelT = 100
4240
<strong>Output:</strong> [{&quot;time&quot;: 30, &quot;returned&quot;: 8}]
4341
<strong>Explanation:</strong>
44-
const cancelTime = 100
4542
const cancel = cancellable((x1, x2) =&gt; x1 * x2, [2,4], 30); // fn(2,4) called at t=30ms
46-
setTimeout(cancel, cancelTime);
43+
setTimeout(cancel, 100);
4744

48-
The cancellation was scheduled to occur after a delay of cancelTime (100ms), which happened after the execution of fn(2,4) at 30ms.
45+
The cancellation was scheduled to occur after a delay of cancelT (100ms), which happened after the execution of fn(2,4) at 30ms.
4946
</pre>
5047

5148
<p>&nbsp;</p>

0 commit comments

Comments
 (0)