mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 23:41:41 +08:00
存量题库数据更新
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
<p>Given an asyncronous function <code>fn</code> and a time <code>t</code> in milliseconds, return a new <strong>time limited</strong> version of the input function.</p>
|
||||
<p>Given an asynchronous function <code>fn</code> and a time <code>t</code> in milliseconds, return a new <strong>time limited</strong> version of the input function. <code>fn</code> takes arguments provided to the <strong>time limited </strong>function.</p>
|
||||
|
||||
<p>A <strong>time limited</strong> function is a function that is identical to the original unless it takes longer than <code>t</code> milliseconds to fullfill. In that case, it will reject with <code>"Time Limit Exceeded"</code>. Note that it should reject with a string, not an <code>Error</code>.</p>
|
||||
<p>The <strong>time limited</strong> function should follow these rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>If the <code>fn</code> completes within the time limit of <code>t</code> milliseconds, the <strong>time limited</strong> function should resolve with the result.</li>
|
||||
<li>If the execution of the <code>fn</code> exceeds the time limit, the <strong>time limited</strong> function should reject with the string <code>"Time Limit Exceeded"</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
@@ -15,6 +20,17 @@ inputs = [5]
|
||||
t = 50
|
||||
<strong>Output:</strong> {"rejected":"Time Limit Exceeded","time":50}
|
||||
<strong>Explanation:</strong>
|
||||
const limited = timeLimit(fn, t)
|
||||
const start = performance.now()
|
||||
let result;
|
||||
try {
|
||||
const res = await limited(...inputs)
|
||||
result = {"resolved": res, "time": Math.floor(performance.now() - start)};
|
||||
} catch (err) {
|
||||
result = {"rejected": err, "time": Math.floor(performance.now() - start)};
|
||||
}
|
||||
console.log(result) // Output
|
||||
|
||||
The provided function is set to resolve after 100ms. However, the time limit is set to 50ms. It rejects at t=50ms because the time limit was reached.
|
||||
</pre>
|
||||
|
||||
@@ -45,7 +61,7 @@ inputs = [5,10]
|
||||
t = 150
|
||||
<strong>Output:</strong> {"resolved":15,"time":120}
|
||||
<strong>Explanation:</strong>
|
||||
The function resolved 5 + 10 = 15 at t=120ms. The time limit is never reached.
|
||||
The function resolved 5 + 10 = 15 at t=120ms. The time limit is never reached.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
@@ -67,5 +83,5 @@ The function immediately throws an error.</pre>
|
||||
<ul>
|
||||
<li><code>0 <= inputs.length <= 10</code></li>
|
||||
<li><code>0 <= t <= 1000</code></li>
|
||||
<li><code>fn returns a promise</code></li>
|
||||
<li><code>fn</code> returns a promise</li>
|
||||
</ul>
|
||||
|
Reference in New Issue
Block a user