mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-18 11:36:48 +08:00
update
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<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>
|
@@ -0,0 +1,45 @@
|
||||
<p>We define the <strong>conversion array</strong> <code>conver</code> of an array <code>arr</code> as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>conver[i] = arr[i] + max(arr[0..i])</code> where <code>max(arr[0..i])</code> is the maximum value of <code>arr[j]</code> over <code>0 <= j <= i</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>We also define the <strong>score</strong> of an array <code>arr</code> as the sum of the values of the conversion array of <code>arr</code>.</p>
|
||||
|
||||
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>, return <em>an array </em><code>ans</code><em> of length </em><code>n</code><em> where </em><code>ans[i]</code><em> is the score of the prefix</em> <code>nums[0..i]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,7,5,10]
|
||||
<strong>Output:</strong> [4,10,24,36,56]
|
||||
<strong>Explanation:</strong>
|
||||
For the prefix [2], the conversion array is [4] hence the score is 4
|
||||
For the prefix [2, 3], the conversion array is [4, 6] hence the score is 10
|
||||
For the prefix [2, 3, 7], the conversion array is [4, 6, 14] hence the score is 24
|
||||
For the prefix [2, 3, 7, 5], the conversion array is [4, 6, 14, 12] hence the score is 36
|
||||
For the prefix [2, 3, 7, 5, 10], the conversion array is [4, 6, 14, 12, 20] hence the score is 56
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,2,4,8,16]
|
||||
<strong>Output:</strong> [2,4,8,16,32,64]
|
||||
<strong>Explanation:</strong>
|
||||
For the prefix [1], the conversion array is [2] hence the score is 2
|
||||
For the prefix [1, 1], the conversion array is [2, 2] hence the score is 4
|
||||
For the prefix [1, 1, 2], the conversion array is [2, 2, 4] hence the score is 8
|
||||
For the prefix [1, 1, 2, 4], the conversion array is [2, 2, 4, 8] hence the score is 16
|
||||
For the prefix [1, 1, 2, 4, 8], the conversion array is [2, 2, 4, 8, 16] hence the score is 32
|
||||
For the prefix [1, 1, 2, 4, 8, 16], the conversion array is [2, 2, 4, 8, 16, 32] hence the score is 64
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>Given a <code>m x n</code> binary matrix <code>mat</code>, find the <strong>0-indexed</strong> position of the row that contains the <strong>maximum</strong> count of <strong>ones,</strong> and the number of ones in that row.</p>
|
||||
|
||||
<p>In case there are multiple rows that have the maximum count of ones, the row with the <strong>smallest row number</strong> should be selected.</p>
|
||||
|
||||
<p>Return<em> an array containing the index of the row, and the number of ones in it.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,1],[1,0]]
|
||||
<strong>Output:</strong> [0,1]
|
||||
<strong>Explanation:</strong> Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1<code>)</code>. So, the answer is [0,1].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,0,0],[0,1,1]]
|
||||
<strong>Output:</strong> [1,2]
|
||||
<strong>Explanation:</strong> The row indexed 1 has the maximum count of ones <code>(2)</code>. So we return its index, <code>1</code>, and the count. So, the answer is [1,2].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,0],[1,1],[0,0]]
|
||||
<strong>Output:</strong> [1,2]
|
||||
<strong>Explanation:</strong> The row indexed 1 has the maximum count of ones (2). So the answer is [1,2].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == mat.length</code> </li>
|
||||
<li><code>n == mat[i].length</code> </li>
|
||||
<li><code>1 <= m, n <= 100</code> </li>
|
||||
<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>Given the <code>root</code> of a binary tree, replace the value of each node in the tree with the <strong>sum of all its cousins' values</strong>.</p>
|
||||
|
||||
<p>Two nodes of a binary tree are <strong>cousins</strong> if they have the same depth with different parents.</p>
|
||||
|
||||
<p>Return <em>the </em><code>root</code><em> of the modified tree</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the depth of a node is the number of edges in the path from the root node to it.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/11/example11.png" style="width: 571px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,4,9,1,10,null,7]
|
||||
<strong>Output:</strong> [0,0,0,7,7,null,11]
|
||||
<strong>Explanation:</strong> The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
|
||||
- Node with value 5 does not have any cousins so its sum is 0.
|
||||
- Node with value 4 does not have any cousins so its sum is 0.
|
||||
- Node with value 9 does not have any cousins so its sum is 0.
|
||||
- Node with value 1 has a cousin with value 7 so its sum is 7.
|
||||
- Node with value 10 has a cousin with value 7 so its sum is 7.
|
||||
- Node with value 7 has cousins with values 1 and 10 so its sum is 11.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/11/diagram33.png" style="width: 481px; height: 91px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [3,1,2]
|
||||
<strong>Output:</strong> [0,0,0]
|
||||
<strong>Explanation:</strong> The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
|
||||
- Node with value 3 does not have any cousins so its sum is 0.
|
||||
- Node with value 1 does not have any cousins so its sum is 0.
|
||||
- Node with value 2 does not have any cousins so its sum is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisiting of <strong>positive</strong> integers. You can do the following operation on the array <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Select an index <code>i</code> such that <code>0 <= i < n - 1</code> and replace either of <code>nums[i]</code> or <code>nums[i+1]</code> with their gcd value.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations to make all elements of </em><code>nums</code><em> equal to </em><code>1</code>. If it is impossible, return <code>-1</code>.</p>
|
||||
|
||||
<p>The gcd of two integers is the greatest common divisor of the two integers.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,6,3,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We can do the following operations:
|
||||
- Choose index i = 2 and replace nums[2] with gcd(3,4) = 1. Now we have nums = [2,6,1,4].
|
||||
- Choose index i = 1 and replace nums[1] with gcd(6,1) = 1. Now we have nums = [2,1,1,4].
|
||||
- Choose index i = 0 and replace nums[0] with gcd(2,1) = 1. Now we have nums = [1,1,1,4].
|
||||
- Choose index i = 2 and replace nums[3] with gcd(1,4) = 1. Now we have nums = [1,1,1,1].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,10,6,14]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that it is impossible to make all the elements equal to 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>Given a positive integer <code>n</code>, find the sum of all integers in the range <code>[1, n]</code> <strong>inclusive</strong> that are divisible by <code>3</code>, <code>5</code>, or <code>7</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the sum of all numbers in the given range satisfying the constraint.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7
|
||||
<strong>Output:</strong> 21
|
||||
<strong>Explanation:</strong> Numbers in the range <code>[1, 7]</code> that are divisible by <code>3</code>, <code>5,</code> or <code>7 </code>are <code>3, 5, 6, 7</code>. The sum of these numbers is <code>21</code>.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 10
|
||||
<strong>Output:</strong> 40
|
||||
<strong>Explanation:</strong> Numbers in the range <code>[1, 10] that are</code> divisible by <code>3</code>, <code>5,</code> or <code>7</code> are <code>3, 5, 6, 7, 9, 10</code>. The sum of these numbers is 40.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 9
|
||||
<strong>Output:</strong> 30
|
||||
<strong>Explanation:</strong> Numbers in the range <code>[1, 9]</code> that are divisible by <code>3</code>, <code>5</code>, or <code>7</code> are <code>3, 5, 6, 7, 9</code>. The sum of these numbers is <code>30</code>.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>3</sup></code></li>
|
||||
</ul>
|
77
leetcode-cn/problem (English)/函数防抖(English) [debounce].html
Normal file
77
leetcode-cn/problem (English)/函数防抖(English) [debounce].html
Normal file
@@ -0,0 +1,77 @@
|
||||
<p>Given a function <code>fn</code> and a time in milliseconds <code>t</code>, return a <strong>debounced</strong> version of that function.</p>
|
||||
|
||||
<p>A <strong>debounced</strong> function is a function whose execution is delayed by <code>t</code> milliseconds and whose execution is cancelled if it is called again within that window of time. The debounced function should also recieve the passed parameters.</p>
|
||||
|
||||
<p>For example, let's say <code>t = 50ms</code>, and the function was called at <code>30ms</code>, <code>60ms</code>, and <code>100ms</code>. The first 2 function calls would be cancelled, and the 3rd function call would be executed at <code>150ms</code>. If instead <code>t = 35ms</code>, The 1st call would be cancelled, the 2nd would be executed at <code>95ms</code>, and the 3rd would be executed at <code>135ms</code>.</p>
|
||||
|
||||
<p><img alt="Debounce Schematic" src="https://assets.leetcode.com/uploads/2023/04/08/screen-shot-2023-04-08-at-11048-pm.png" style="width: 800px; height: 242px;" /></p>
|
||||
|
||||
<p>The above diagram shows how debounce will transform events. Each rectangle represents 100ms and the debounce time is 400ms. Each color represents a different set of inputs.</p>
|
||||
|
||||
<p>Please solve it without using lodash's <code>_.debounce()</code> function.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
t = 50
|
||||
calls = [
|
||||
{"t": 50, inputs: [1]},
|
||||
{"t": 75, inputs: [2]}
|
||||
]
|
||||
<strong>Output:</strong> [{"t": 125, inputs: [2]}]
|
||||
<strong>Explanation:</strong>
|
||||
let start = Date.now();
|
||||
function log(...inputs) {
|
||||
console.log([Date.now() - start, inputs ])
|
||||
}
|
||||
const dlog = debounce(log, 50);
|
||||
setTimeout(() => dlog(1), 50);
|
||||
setTimeout(() => dlog(2), 75);
|
||||
|
||||
The 1st call is cancelled by the 2nd call because the 2nd call occurred before 100ms
|
||||
The 2nd call is delayed by 50ms and executed at 125ms. The inputs were (2).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
t = 20
|
||||
calls = [
|
||||
{"t": 50, inputs: [1]},
|
||||
{"t": 100, inputs: [2]}
|
||||
]
|
||||
<strong>Output:</strong> [{"t": 70, inputs: [1]}, {"t": 120, inputs: [2]}]
|
||||
<strong>Explanation:</strong>
|
||||
The 1st call is delayed until 70ms. The inputs were (1).
|
||||
The 2nd call is delayed until 120ms. The inputs were (2).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
t = 150
|
||||
calls = [
|
||||
{"t": 50, inputs: [1, 2]},
|
||||
{"t": 300, inputs: [3, 4]},
|
||||
{"t": 300, inputs: [5, 6]}
|
||||
]
|
||||
<strong>Output:</strong> [{"t": 200, inputs: [1,2]}, {"t": 450, inputs: [5, 6]}]
|
||||
<strong>Explanation:</strong>
|
||||
The 1st call is delayed by 150ms and ran at 200ms. The inputs were (1, 2).
|
||||
The 2nd call is cancelled by the 3rd call
|
||||
The 3rd call is delayed by 150ms and ran at 450ms. The inputs were (5, 6).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= t <= 1000</code></li>
|
||||
<li><code>1 <= calls.length <= 10</code></li>
|
||||
<li><code>0 <= calls[i].t <= 1000</code></li>
|
||||
<li><code>0 <= calls[i].inputs.length <= 10</code></li>
|
||||
</ul>
|
83
leetcode-cn/problem (English)/分组(English) [group-by].html
Normal file
83
leetcode-cn/problem (English)/分组(English) [group-by].html
Normal file
@@ -0,0 +1,83 @@
|
||||
<p>Write code that enhances all arrays such that you can call the <code>array.groupBy(fn)</code> method on any array and it will return a <strong>grouped</strong> version of the array.</p>
|
||||
|
||||
<p>A <strong>grouped</strong> array is an object where each key is the output of <code>fn(arr[i])</code> and each value is an array containing all items in the original array with that key.</p>
|
||||
|
||||
<p>The provided callback <code>fn</code> will accept an item in the array and return a string key.</p>
|
||||
|
||||
<p>The order of each value list should be the order the items appear in the array. Any order of keys is acceptable.</p>
|
||||
|
||||
<p>Please solve it without lodash's <code>_.groupBy</code> function.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
array = [
|
||||
{"id":"1"},
|
||||
{"id":"1"},
|
||||
{"id":"2"}
|
||||
],
|
||||
fn = function (item) {
|
||||
return item.id;
|
||||
}
|
||||
<strong>Output:</strong>
|
||||
{
|
||||
"1": [{"id": "1"}, {"id": "1"}],
|
||||
"2": [{"id": "2"}]
|
||||
}
|
||||
<strong>Explanation:</strong>
|
||||
Output is from array.groupBy(fn).
|
||||
The selector function gets the "id" out of each item in the array.
|
||||
There are two objects with an "id" of 1. Both of those objects are put in the first array.
|
||||
There is one object with an "id" of 2. That object is put in the second array.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
array = [
|
||||
[1, 2, 3],
|
||||
[1, 3, 5],
|
||||
[1, 5, 9]
|
||||
]
|
||||
fn = function (list) {
|
||||
return String(list[0]);
|
||||
}
|
||||
<strong>Output:</strong>
|
||||
{
|
||||
"1": [[1, 2, 3], [1, 3, 5], [1, 5, 9]]
|
||||
}
|
||||
<strong>Explanation:</strong>
|
||||
The array can be of any type. In this case, the selector function defines the key as being the first element in the array.
|
||||
All the arrays have 1 as their first element so they are grouped together.
|
||||
{
|
||||
"1": [[1, 2, 3], [1, 3, 5], [1, 5, 9]]
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
fn = function (n) {
|
||||
return String(n > 5);
|
||||
}
|
||||
<strong>Output:</strong>
|
||||
{
|
||||
"true": [6, 7, 8, 9, 10],
|
||||
"false": [1, 2, 3, 4, 5]
|
||||
}
|
||||
<strong>Explanation:</strong>
|
||||
The selector function splits the array by whether each number is greater than 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= array.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>fn returns a string</code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>Given an array of functions <code>[f<span style="font-size: 10.8333px;">1</span>, f<sub>2</sub>, f<sub>3</sub>, ..., f<sub>n</sub>]</code>, return a new function <code>fn</code> that is the <strong>function composition</strong> of the array of functions.</p>
|
||||
|
||||
<p>The <strong>function composition</strong> of <code>[f(x), g(x), h(x)]</code> is <code>fn(x) = f(g(h(x)))</code>.</p>
|
||||
|
||||
<p>The <strong>function composition</strong> of an empty list of functions is the <strong>identity function</strong> <code>f(x) = x</code>.</p>
|
||||
|
||||
<p>You may assume each function in the array accepts one integer as input and returns one integer as output.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> functions = [x => x + 1, x => x * x, x => 2 * x], x = 4
|
||||
<strong>Output:</strong> 65
|
||||
<strong>Explanation:</strong>
|
||||
Evaluating from right to left ...
|
||||
Starting with x = 4.
|
||||
2 * (4) = 8
|
||||
(8) * (8) = 64
|
||||
(64) + 1 = 65
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> functions = [x => 10 * x, x => 10 * x, x => 10 * x], x = 1
|
||||
<strong>Output:</strong> 1000
|
||||
<strong>Explanation:</strong>
|
||||
Evaluating from right to left ...
|
||||
10 * (1) = 10
|
||||
10 * (10) = 100
|
||||
10 * (100) = 1000
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> functions = [], x = 42
|
||||
<strong>Output:</strong> 42
|
||||
<strong>Explanation:</strong>
|
||||
The composition of zero functions is the identity function</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code><font face="monospace">-1000 <= x <= 1000</font></code></li>
|
||||
<li><code><font face="monospace">0 <= functions.length <= 1000</font></code></li>
|
||||
<li><font face="monospace"><code>all functions accept and return a single integer</code></font></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>Given two objects <code>o1</code> and <code>o2</code>, check if they are <strong>deeply equal</strong>.</p>
|
||||
|
||||
<p>For two objects to be <strong>deeply equal</strong>, they must contain the same keys, and the associated values must also be <strong>deeply equal</strong>. Two objects are also considered <strong>deeply equal</strong> if they pass the <code>===</code> equality check.</p>
|
||||
|
||||
<p>You may assume both objects are the output of <code>JSON.parse</code>. In other words, they are valid JSON.</p>
|
||||
|
||||
<p>Please solve it without using lodash's <code>_.isEqual()</code> function.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> o1 = {"x":1,"y":2}, o2 = {"x":1,"y":2}
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The keys and values match exactly.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> o1 = {"y":2,"x":1}, o2 = {"x":1,"y":2}
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Although the keys are in a different order, they still match exactly.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> o1 = {"x":null,"L":[1,2,3]}, o2 = {"x":null,"L":["1","2","3"]}
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The array of numbers is different from the array of strings.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> o1 = true, o2 = false
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> true !== false</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= JSON.stringify(o1).length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= JSON.stringify(o2).length <= 10<sup>5</sup></code></li>
|
||||
<li><code>maxNestingDepth <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>Given an object, return a valid JSON string of that object. You may assume the object only inludes strings, integers, arrays, objects, booleans, and null. The returned string should not include extra spaces. The order of keys should be the same as the order returned by <code>Object.keys()</code>.</p>
|
||||
|
||||
<p>Please solve it without using the built-in <code>JSON.stringify</code> method.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> object = {"y":1,"x":2}
|
||||
<strong>Output:</strong> {"y":1,"x":2}
|
||||
<strong>Explanation:</strong>
|
||||
Return the JSON representation.
|
||||
Note that the order of keys should be the same as the order returned by Object.keys().</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> object = {"a":"str","b":-12,"c":true,"d":null}
|
||||
<strong>Output:</strong> {"a":"str","b":-12,"c":true,"d":null}
|
||||
<strong>Explanation:</strong>
|
||||
The primitives of JSON are strings, numbers, booleans, and null.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> object = {"key":{"a":1,"b":[{},null,"Hello"]}}
|
||||
<strong>Output:</strong> {"key":{"a":1,"b":[{},null,"Hello"]}}
|
||||
<strong>Explanation:</strong>
|
||||
Objects and arrays can include other objects and arrays.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> object = true
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong>
|
||||
Primitive types are valid inputs.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>object includes strings, integers, booleans, arrays, objects, and null</code></li>
|
||||
<li><code>1 <= JSON.stringify(object).length <= 10<sup>5</sup></code></li>
|
||||
<li><code>maxNestingLevel <= 1000</code></li>
|
||||
<li><code>all strings will only contain alphanumeric characters</code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>Given a <strong>multi-dimensional array</strong> of integers, return a generator object which yields integers in the same order as <strong>inorder traversal</strong>.</p>
|
||||
|
||||
<p>A <strong>multi-dimensional array</strong> is a recursive data structure that contains both integers and other <strong>multi-dimensional arrays</strong>.</p>
|
||||
|
||||
<p><strong>inorder traversal</strong> iterates over each array from left to right, yielding any integers it encounters or applying <strong>inorder traversal</strong> to any arrays it encounters.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [[[6]],[1,3],[]]
|
||||
<strong>Output:</strong> [6,1,3]
|
||||
<strong>Explanation:</strong>
|
||||
const generator = inorderTraversal(arr);
|
||||
generator.next().value; // 6
|
||||
generator.next().value; // 1
|
||||
generator.next().value; // 3
|
||||
generator.next().done; // true
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = []
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> There are no integers so the generator doesn't yield anything.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= arr.flat().length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= arr.flat()[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>maxNestingDepth <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums</code> and <code>divisors</code>.</p>
|
||||
|
||||
<p>The <strong>divisibility score</strong> of <code>divisors[i]</code> is the number of indices <code>j</code> such that <code>nums[j]</code> is divisible by <code>divisors[i]</code>.</p>
|
||||
|
||||
<p>Return <em>the integer</em> <code>divisors[i]</code> <em>with the maximum divisibility score</em>. If there is more than one integer with the maximum score, return the minimum of them.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,7,9,3,9], divisors = [5,2,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The divisibility score for every element in divisors is:
|
||||
The divisibility score of divisors[0] is 0 since no number in nums is divisible by 5.
|
||||
The divisibility score of divisors[1] is 1 since nums[0] is divisible by 2.
|
||||
The divisibility score of divisors[2] is 3 since nums[2], nums[3], and nums[4] are divisible by 3.
|
||||
Since divisors[2] has the maximum divisibility score, we return it.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [20,14,21,10], divisors = [5,7,5]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The divisibility score for every element in divisors is:
|
||||
The divisibility score of divisors[0] is 2 since nums[0] and nums[3] are divisible by 5.
|
||||
The divisibility score of divisors[1] is 2 since nums[1] and nums[2] are divisible by 7.
|
||||
The divisibility score of divisors[2] is 2 since nums[0] and nums[3] are divisible by 5.
|
||||
Since divisors[0], divisors[1], and divisors[2] all have the maximum divisibility score, we return the minimum of them (i.e., divisors[2]).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [12], divisors = [10,16]
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> The divisibility score for every element in divisors is:
|
||||
The divisibility score of divisors[0] is 0 since no number in nums is divisible by 10.
|
||||
The divisibility score of divisors[1] is 0 since no number in nums is divisible by 16.
|
||||
Since divisors[0] and divisors[1] both have the maximum divisibility score, we return the minimum of them (i.e., divisors[0]).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length, divisors.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i], divisors[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,25 @@
|
||||
Write code that enhances all arrays such that you can call the <code>array.last()</code> method on any array and it will return the last element. If there are no elements in the array, it should return <code>-1</code>.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Calling nums.last() should return the last element: 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = []
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> Because there are no elements, return -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= arr.length <= 1000</code></li>
|
||||
<li><code>0 <= arr[i] <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,62 @@
|
||||
<p>Given an integer array <code>nums</code>, a reducer function <code>fn</code>, and an intial value <code>init</code>, return a <strong>reduced</strong> array.</p>
|
||||
|
||||
<p>A <strong>reduced</strong> array is created by applying the following operation: <code>val = fn(init, nums[0])</code>, <code>val = fn(val, nums[1])</code>, <code>val = fn(val, arr[2])</code>, <code>...</code> until every element in the array has been processed. The final value of <code>val</code> is returned.</p>
|
||||
|
||||
<p>If the length of the array is 0, it should return <code>init</code>.</p>
|
||||
|
||||
<p>Please solve it without using the built-in <code>Array.reduce</code> method.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
nums = [1,2,3,4]
|
||||
fn = function sum(accum, curr) { return accum + curr; }
|
||||
init = 0
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong>
|
||||
initially, the value is init=0.
|
||||
(0) + nums[0] = 1
|
||||
(1) + nums[1] = 3
|
||||
(3) + nums[2] = 6
|
||||
(6) + nums[3] = 10
|
||||
The final answer is 10.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
nums = [1,2,3,4]
|
||||
fn = function sum(accum, curr) { return accum + curr * curr; }
|
||||
init = 100
|
||||
<strong>Output:</strong> 130
|
||||
<strong>Explanation:</strong>
|
||||
initially, the value is init=100.
|
||||
(100) + nums[0]^2 = 101
|
||||
(101) + nums[1]^2 = 105
|
||||
(105) + nums[2]^2 = 114
|
||||
(114) + nums[3]^2 = 130
|
||||
The final answer is 130.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
nums = []
|
||||
fn = function sum(accum, curr) { return 0; }
|
||||
init = 25
|
||||
<strong>Output:</strong> 25
|
||||
<strong>Explanation:</strong> For empty arrays, the answer is always init.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= nums.length <= 1000</code></li>
|
||||
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||
<li><code>0 <= init <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>There exists an undirected and unrooted tree with <code>n</code> nodes indexed from <code>0</code> to <code>n - 1</code>. You are given the integer <code>n</code> and a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>
|
||||
|
||||
<p>Each node has an associated price. You are given an integer array <code>price</code>, where <code>price[i]</code> is the price of the <code>i<sup>th</sup></code> node.</p>
|
||||
|
||||
<p>The <strong>price sum</strong> of a given path is the sum of the prices of all nodes lying on that path.</p>
|
||||
|
||||
<p>Additionally, you are given a 2D integer array <code>trips</code>, where <code>trips[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> indicates that you start the <code>i<sup>th</sup></code> trip from the node <code>start<sub>i</sub></code> and travel to the node <code>end<sub>i</sub></code> by any path you like.</p>
|
||||
|
||||
<p>Before performing your first trip, you can choose some <strong>non-adjacent</strong> nodes and halve the prices.</p>
|
||||
|
||||
<p>Return <em>the minimum total price sum to perform all the given trips</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/03/16/diagram2.png" style="width: 541px; height: 181px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, edges = [[0,1],[1,2],[1,3]], price = [2,2,10,6], trips = [[0,3],[2,1],[2,3]]
|
||||
<strong>Output:</strong> 23
|
||||
<strong>Explanation:</strong> The diagram above denotes the tree after rooting it at node 2. The first part shows the initial tree and the second part shows the tree after choosing nodes 0, 2, and 3, and making their price half.
|
||||
For the 1<sup>st</sup> trip, we choose path [0,1,3]. The price sum of that path is 1 + 2 + 3 = 6.
|
||||
For the 2<sup>nd</sup> trip, we choose path [2,1]. The price sum of that path is 2 + 5 = 7.
|
||||
For the 3<sup>rd</sup> trip, we choose path [2,1,3]. The price sum of that path is 5 + 2 + 3 = 10.
|
||||
The total price sum of all trips is 6 + 7 + 10 = 23.
|
||||
It can be proven, that 23 is the minimum answer that we can achieve.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/03/16/diagram3.png" style="width: 456px; height: 111px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, edges = [[0,1]], price = [2,2], trips = [[0,0]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The diagram above denotes the tree after rooting it at node 0. The first part shows the initial tree and the second part shows the tree after choosing node 0, and making its price half.
|
||||
For the 1<sup>st</sup> trip, we choose path [0]. The price sum of that path is 1.
|
||||
The total price sum of all trips is 1. It can be proven, that 1 is the minimum answer that we can achieve.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
<li><code>price.length == n</code></li>
|
||||
<li><code>price[i]</code> is an even integer.</li>
|
||||
<li><code>1 <= price[i] <= 1000</code></li>
|
||||
<li><code>1 <= trips.length <= 100</code></li>
|
||||
<li><code>0 <= start<sub>i</sub>, end<sub>i</sub> <= n - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,71 @@
|
||||
<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>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> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = async (n) => {
|
||||
await new Promise(res => setTimeout(res, 100));
|
||||
return n * n;
|
||||
}
|
||||
inputs = [5]
|
||||
t = 50
|
||||
<strong>Output:</strong> {"rejected":"Time Limit Exceeded","time":50}
|
||||
<strong>Explanation:</strong>
|
||||
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>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = async (n) => {
|
||||
await new Promise(res => setTimeout(res, 100));
|
||||
return n * n;
|
||||
}
|
||||
inputs = [5]
|
||||
t = 150
|
||||
<strong>Output:</strong> {"resolved":25,"time":100}
|
||||
<strong>Explanation:</strong>
|
||||
The function resolved 5 * 5 = 25 at t=100ms. The time limit is never reached.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = async (a, b) => {
|
||||
await new Promise(res => setTimeout(res, 120));
|
||||
return a + b;
|
||||
}
|
||||
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.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = async () => {
|
||||
throw "Error";
|
||||
}
|
||||
inputs = []
|
||||
t = 1000
|
||||
<strong>Output:</strong> {"rejected":"Error","time":0}
|
||||
<strong>Explanation:</strong>
|
||||
The function immediately throws an error.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= inputs.length <= 10</code></li>
|
||||
<li><code>0 <= t <= 1000</code></li>
|
||||
<li><code>fn returns a promise</code></li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>Write a class that allows getting and setting key-value pairs, however a <strong>time until expiration</strong> is associated with each key.</p>
|
||||
|
||||
<p>The class has three public methods:</p>
|
||||
|
||||
<p><code>set(key, value, duration)</code>: accepts an integer <code>key</code>, an integer <code>value</code>, and a <code>duration</code> in milliseconds. Once the <code>duration</code> has elapsed, the key should be inaccessible. The method should return <code>true</code> if the same un-expired key already exists and <code>false</code> otherwise. Both the value and duration should be overwritten if the key already exists.</p>
|
||||
|
||||
<p><code>get(key)</code>: if an un-expired key exists, it should return the associated value. Otherwise it should return <code>-1</code>.</p>
|
||||
|
||||
<p><code>count()</code>: returns the count of un-expired keys.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
["TimeLimitedCache", "set", "get", "count", "get"]
|
||||
[[], [1, 42, 100], [1], [], [1]]
|
||||
[0, 0, 50, 50, 150]
|
||||
<strong>Output:</strong> [null, false, 42, 1, -1]
|
||||
<strong>Explanation:</strong>
|
||||
At t=0, the cache is constructed.
|
||||
At t=0, a key-value pair (1: 42) is added with a time limit of 100ms. The value doesn't exist so false is returned.
|
||||
At t=50, key=1 is requested and the value of 42 is returned.
|
||||
At t=50, count() is called and there is one active key in the cache.
|
||||
At t=100, key=1 expires.
|
||||
At t=150, get(1) is called but -1 is returned because the cache is empty.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
["TimeLimitedCache", "set", "set", "get", "get", "get", "count"]
|
||||
[[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []]
|
||||
[0, 0, 40, 50, 120, 200, 250]
|
||||
<strong>Output:</strong> [null, false, true, 50, 50, -1]
|
||||
<strong>Explanation:</strong>
|
||||
At t=0, the cache is constructed.
|
||||
At t=0, a key-value pair (1: 42) is added with a time limit of 50ms. The value doesn't exist so false is returned.
|
||||
At t=40, a key-value pair (1: 50) is added with a time limit of 100ms. A non-expired value already existed so true is returned and the old value was overwritten.
|
||||
At t=50, get(1) is called which returned 50.
|
||||
At t=120, get(1) is called which returned 50.
|
||||
At t=140, key=1 expires.
|
||||
At t=200, get(1) is called but the cache is empty so -1 is returned.
|
||||
At t=250, count() returns 0 because the cache is empty.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= key <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= value <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= duration <= 1000</code></li>
|
||||
<li><code>total method calls will not exceed 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>Given a string <code>word</code> to which you can insert letters "a", "b" or "c" anywhere and any number of times, return <em>the minimum number of letters that must be inserted so that <code>word</code> becomes <strong>valid</strong>.</em></p>
|
||||
|
||||
<p>A string is called <strong>valid </strong>if it can be formed by concatenating the string "abc" several times.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "b"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Insert the letter "a" right before "b", and the letter "c" right next to "a" to obtain the valid string "<strong>a</strong>b<strong>c</strong>".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "aaa"
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> Insert letters "b" and "c" next to each "a" to obtain the valid string "a<strong>bc</strong>a<strong>bc</strong>a<strong>bc</strong>".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abc"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> word is already valid. No modifications are needed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 50</code></li>
|
||||
<li><code>word</code> consists of letters "a", "b" and "c" only. </li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>grid</code>. The width of a column is the maximum <strong>length </strong>of its integers.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>grid = [[-10], [3], [12]]</code>, the width of the only column is <code>3</code> since <code>-10</code> is of length <code>3</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer array</em> <code>ans</code> <em>of size</em> <code>n</code> <em>where</em> <code>ans[i]</code> <em>is the width of the</em> <code>i<sup>th</sup></code> <em>column</em>.</p>
|
||||
|
||||
<p>The <strong>length</strong> of an integer <code>x</code> with <code>len</code> digits is equal to <code>len</code> if <code>x</code> is non-negative, and <code>len + 1</code> otherwise.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1],[22],[333]]
|
||||
<strong>Output:</strong> [3]
|
||||
<strong>Explanation:</strong> In the 0<sup>th</sup> column, 333 is of length 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[-15,1,3],[15,7,12],[5,6,-2]]
|
||||
<strong>Output:</strong> [3,1,2]
|
||||
<strong>Explanation:</strong>
|
||||
In the 0<sup>th</sup> column, only -15 is of length 3.
|
||||
In the 1<sup>st</sup> column, all integers are of length 1.
|
||||
In the 2<sup>nd</sup> column, both 12 and -2 are of length 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 100 </code></li>
|
||||
<li><code>-10<sup>9</sup> <= grid[r][c] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
65
leetcode-cn/problem (English)/柯里化(English) [curry].html
Normal file
65
leetcode-cn/problem (English)/柯里化(English) [curry].html
Normal file
@@ -0,0 +1,65 @@
|
||||
<p>Given a function <code>fn</code>, return a <strong>curried</strong> version of that function.</p>
|
||||
|
||||
<p>A <strong>curried</strong> function is a function that accepts fewer or an equal number of parameters as the original function and returns either another <strong>curried</strong> function or the same value the original function would have returned.</p>
|
||||
|
||||
<p>In practical terms, if you called the original function like <code>sum(1,2,3)</code>, you would call the <strong>curried</strong> version like <code>csum(1)(2)(3)<font face="sans-serif, Arial, Verdana, Trebuchet MS">, </font></code><code>csum(1)(2,3)</code>, <code>csum(1,2)(3)</code>, or <code>csum(1,2,3)</code>. All these methods of calling the <strong>curried</strong> function should return the same value as the original.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = function sum(a, b, c) { return a + b + c; }
|
||||
inputs = [[1],[2],[3]]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
The code being executed is:
|
||||
const curriedSum = curry(fn);
|
||||
curriedSum(1)(2)(3) === 6;
|
||||
curriedSum(1)(2)(3) should return the same value as sum(1, 2, 3).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = function sum(a, b, c) { return a + b + c; }
|
||||
inputs = [[1,2],[3]]]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
curriedSum(1, 2)(3) should return the same value as sum(1, 2, 3).</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = function sum(a, b, c) { return a + b + c; }
|
||||
inputs = [[],[],[1,2,3]]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
You should be able to pass the parameters in any way, including all at once or none at all.
|
||||
curriedSum()()(1, 2, 3) should return the same value as sum(1, 2, 3).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = function life() { return 42; }
|
||||
inputs = [[]]
|
||||
<strong>Output:</strong> 42
|
||||
<strong>Explanation:</strong>
|
||||
currying a function that accepts zero parameters should effectively do nothing.
|
||||
curriedLife() === 42
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= inputs.length <= 1000</code></li>
|
||||
<li><code>0 <= inputs[i][j] <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= fn.length <= 1000</code></li>
|
||||
<li><code>inputs.flat().length == fn.length</code></li>
|
||||
<li><code>function parameters explicitly defined</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>Write a function that checks if a given object is an instance of a given class or superclass. For this problem, an object is considered an instance of a given class if that object has access to that class's methods.</p>
|
||||
|
||||
<p>There are no constraints on the data types that can be passed to the function.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => checkIfInstanceOf(new Date(), Date)
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>The object returned by the Date constructor is, by definition, an instance of Date.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstanceOf(new Dog(), Animal); }
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong>
|
||||
class Animal {};
|
||||
class Dog extends Animal {};
|
||||
checkIfInstance(new Dog(), Animal); // true
|
||||
|
||||
Dog is a subclass of Animal. Therefore, a Dog object is an instance of both Dog and Animal.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => checkIfInstanceOf(Date, Date)
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation: </strong>A date constructor cannot logically be an instance of itself.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => checkIfInstanceOf(5, Number)
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>5 is a Number. Note that the "instanceof" keyword would return false. However, it is still considered an instance of Number because it accesses the Number methods. For example "toFixed()".
|
||||
</pre>
|
@@ -0,0 +1,56 @@
|
||||
<p>Given an integer array <code>nums</code> containing <code>n</code> integers, find the <strong>beauty</strong> of each subarray of size <code>k</code>.</p>
|
||||
|
||||
<p>The <strong>beauty</strong> of a subarray is the <code>x<sup>th</sup></code><strong> smallest integer </strong>in the subarray if it is <strong>negative</strong>, or <code>0</code> if there are fewer than <code>x</code> negative integers.</p>
|
||||
|
||||
<p>Return <em>an integer array containing </em><code>n - k + 1</code> <em>integers, which denote the </em><strong>beauty</strong><em> of the subarrays <strong>in order</strong> from the first index in the array.</em></p>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,-1,-3,-2,3], k = 3, x = 2
|
||||
<strong>Output:</strong> [-1,-2,-2]
|
||||
<strong>Explanation:</strong> There are 3 subarrays with size k = 3.
|
||||
The first subarray is <code>[1, -1, -3]</code> and the 2<sup>nd</sup> smallest negative integer is -1.
|
||||
The second subarray is <code>[-1, -3, -2]</code> and the 2<sup>nd</sup> smallest negative integer is -2.
|
||||
The third subarray is <code>[-3, -2, 3] </code>and the 2<sup>nd</sup> smallest negative integer is -2.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,-2,-3,-4,-5], k = 2, x = 2
|
||||
<strong>Output:</strong> [-1,-2,-3,-4]
|
||||
<strong>Explanation:</strong> There are 4 subarrays with size k = 2.
|
||||
For <code>[-1, -2]</code>, the 2<sup>nd</sup> smallest negative integer is -1.
|
||||
For <code>[-2, -3]</code>, the 2<sup>nd</sup> smallest negative integer is -2.
|
||||
For <code>[-3, -4]</code>, the 2<sup>nd</sup> smallest negative integer is -3.
|
||||
For <code>[-4, -5]</code>, the 2<sup>nd</sup> smallest negative integer is -4. </pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-3,1,2,-3,0,-3], k = 2, x = 1
|
||||
<strong>Output:</strong> [-3,0,-3,-3,-3]
|
||||
<strong>Explanation:</strong> There are 5 subarrays with size k = 2<strong>.</strong>
|
||||
For <code>[-3, 1]</code>, the 1<sup>st</sup> smallest negative integer is -3.
|
||||
For <code>[1, 2]</code>, there is no negative integer so the beauty is 0.
|
||||
For <code>[2, -3]</code>, the 1<sup>st</sup> smallest negative integer is -3.
|
||||
For <code>[-3, 0]</code>, the 1<sup>st</sup> smallest negative integer is -3.
|
||||
For <code>[0, -3]</code>, the 1<sup>st</sup> smallest negative integer is -3.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length </code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
<li><code>1 <= x <= k </code></li>
|
||||
<li><code>-50 <= nums[i] <= 50 </code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>Write a generator function that returns a generator object which yields the <strong>fibonacci sequence</strong>.</p>
|
||||
|
||||
<p>The <strong>fibonacci sequence</strong> is defined by the relation <code>X<sub>n</sub> = X<sub>n-1</sub> + X<sub>n-2</sub></code>.</p>
|
||||
|
||||
<p>The first few numbers of the series are <code>0, 1, 1, 2, 3, 5, 8, 13</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> callCount = 5
|
||||
<strong>Output:</strong> [0,1,1,2,3]
|
||||
<strong>Explanation:</strong>
|
||||
const gen = fibGenerator();
|
||||
gen.next().value; // 0
|
||||
gen.next().value; // 1
|
||||
gen.next().value; // 1
|
||||
gen.next().value; // 2
|
||||
gen.next().value; // 3
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> callCount = 0
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> gen.next() is never called so nothing is outputted
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= callCount <= 50</code></li>
|
||||
</ul>
|
29
leetcode-cn/problem (English)/睡眠函数(English) [sleep].html
Normal file
29
leetcode-cn/problem (English)/睡眠函数(English) [sleep].html
Normal file
@@ -0,0 +1,29 @@
|
||||
<p>Given a positive integer <code>millis</code>, write an asyncronous function that sleeps for <code>millis</code> milliseconds. It can resolve any value.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> millis = 100
|
||||
<strong>Output:</strong> 100
|
||||
<strong>Explanation:</strong> It should return a promise that resolves after 100ms.
|
||||
let t = Date.now();
|
||||
sleep(100).then(() => {
|
||||
console.log(Date.now() - t); // 100
|
||||
});
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> millis = 200
|
||||
<strong>Output:</strong> 200
|
||||
<strong>Explanation:</strong> It should return a promise that resolves after 200ms.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= millis <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>Write code that enhances all arrays such that you can call the <code>snail(rowsCount, colsCount)</code> method that transforms the 1D array into a 2D array organised in the pattern known as <strong>snail traversal order</strong>. Invalid input values should output an empty array. If <code>rowsCount * colsCount !== nums.length</code>, the input is considered invalid.</p>
|
||||
|
||||
<p><strong>Snail traversal order</strong><em> </em>starts at the top left cell with the first value of the current array. It then moves through the entire first column from top to bottom, followed by moving to the next column on the right and traversing it from bottom to top. This pattern continues, alternating the direction of traversal with each column, until the entire current array is covered. For example, when given the input array <code>[19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]</code> with <code>rowsCount = 5</code> and <code>colsCount = 4</code>, the desired output matrix is shown below. Note that iterating the matrix following the arrows corresponds to the order of numbers in the original array.</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><img alt="Traversal Diagram" src="https://assets.leetcode.com/uploads/2023/04/10/screen-shot-2023-04-10-at-100006-pm.png" style="width: 275px; height: 343px;" /></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
nums = [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]
|
||||
rowsCount = 5
|
||||
colsCount = 4
|
||||
<strong>Output:</strong>
|
||||
[
|
||||
[19,17,16,15],
|
||||
[10,1,14,4],
|
||||
[3,2,12,20],
|
||||
[7,5,18,11],
|
||||
[9,8,6,13]
|
||||
]
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
nums = [1,2,3,4]
|
||||
rowsCount = 1
|
||||
colsCount = 4
|
||||
<strong>Output:</strong> [[1, 2, 3, 4]]
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
nums = [1,3]
|
||||
rowsCount = 2
|
||||
colsCount = 2
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> 2 multiplied by 2 is 4, and the original array [1,3] has a length of 2; therefore, the input is invalid.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= nums.length <= 250</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
<li><code>1 <= rowsCount <= 250</code></li>
|
||||
<li><code>1 <= colsCount <= 250</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
33
leetcode-cn/problem (English)/计数器(English) [counter].html
Normal file
33
leetcode-cn/problem (English)/计数器(English) [counter].html
Normal file
@@ -0,0 +1,33 @@
|
||||
<p>Given an integer <code>n</code>, return a <code>counter</code> function. This <code>counter</code> function initially returns <code>n</code> and then returns 1 more than the previous value every subsequent time it is called (<code>n</code>, <code>n + 1</code>, <code>n + 2</code>, etc).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
n = 10
|
||||
["call","call","call"]
|
||||
<strong>Output:</strong> [10,11,12]
|
||||
<strong>Explanation:
|
||||
</strong>counter() = 10 // The first time counter() is called, it returns n.
|
||||
counter() = 11 // Returns 1 more than the previous time.
|
||||
counter() = 12 // Returns 1 more than the previous time.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
n = -2
|
||||
["call","call","call","call","call"]
|
||||
<strong>Output:</strong> [-2,-1,0,1,2]
|
||||
<strong>Explanation:</strong> counter() initially returns -2. Then increases after each sebsequent call.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>-1000<sup> </sup><= n <= 1000</code></li>
|
||||
<li><code>At most 1000 calls to counter() will be made</code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>You are given a positive integer <code>arrivalTime</code> denoting the arrival time of a train in hours, and another positive integer <code>delayedTime</code> denoting the amount of delay in hours.</p>
|
||||
|
||||
<p>Return <em>the time when the train will arrive at the station.</em></p>
|
||||
|
||||
<p>Note that the time in this problem is in 24-hours format.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arrivalTime = 15, delayedTime = 5
|
||||
<strong>Output:</strong> 20
|
||||
<strong>Explanation:</strong> Arrival time of the train was 15:00 hours. It is delayed by 5 hours. Now it will reach at 15+5 = 20 (20:00 hours).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arrivalTime = 13, delayedTime = 11
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Arrival time of the train was 13:00 hours. It is delayed by 11 hours. Now it will reach at 13+11=24 (Which is denoted by 00:00 in 24 hours format so return 0).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arrivaltime < 24</code></li>
|
||||
<li><code>1 <= delayedTime <= 24</code></li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>Given a function <code>fn</code>, return a <strong>memoized</strong> version of that function.</p>
|
||||
|
||||
<p>A <strong>memoized </strong>function is a function that will never be called twice with the same inputs. Instead it will return a cached value.</p>
|
||||
|
||||
<p><code>fn</code> can be any function and there are no constraints on what type of values it accepts. Inputs are considered identical if they are <code>===</code> to each other.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
getInputs = () => [[2,2],[2,2],[1,2]]
|
||||
fn = function (a, b) { return a + b; }
|
||||
<strong>Output:</strong> [{"val":4,"calls":1},{"val":4,"calls":1},{"val":3,"calls":2}]
|
||||
<strong>Explanation:</strong>
|
||||
const inputs = getInputs();
|
||||
const memoized = memoize(fn);
|
||||
for (const arr of inputs) {
|
||||
memoized(...arr);
|
||||
}
|
||||
|
||||
For the inputs of (2, 2): 2 + 2 = 4, and it required a call to fn().
|
||||
For the inputs of (2, 2): 2 + 2 = 4, but those inputs were seen before so no call to fn() was required.
|
||||
For the inputs of (1, 2): 1 + 2 = 3, and it required another call to fn() for a total of 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
getInputs = () => [[{},{}],[{},{}],[{},{}]]
|
||||
fn = function (a, b) { return ({...a, ...b}); }
|
||||
<strong>Output:</strong> [{"val":{},"calls":1},{"val":{},"calls":2},{"val":{},"calls":3}]
|
||||
<strong>Explanation:</strong>
|
||||
Merging two empty objects will always result in an empty object. It may seem like there should only be 1 call to fn() because of cache-hits, however none of those objects are === to each other.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
getInputs = () => { const o = {}; return [[o,o],[o,o],[o,o]]; }
|
||||
fn = function (a, b) { return ({...a, ...b}); }
|
||||
<strong>Output:</strong> [{"val":{},"calls":1},{"val":{},"calls":1},{"val":{},"calls":1}]
|
||||
<strong>Explanation:</strong>
|
||||
Merging two empty objects will always result in an empty object. The 2nd and 3rd third function calls result in a cache-hit. This is because every object passed in is identical.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= inputs.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= inputs.flat().length <= 10<sup>5</sup></code></li>
|
||||
<li><code>inputs[i][j] != NaN</code></li>
|
||||
</ul>
|
80
leetcode-cn/problem (English)/记忆函数(English) [memoize].html
Normal file
80
leetcode-cn/problem (English)/记忆函数(English) [memoize].html
Normal file
@@ -0,0 +1,80 @@
|
||||
<p>Given a function <code>fn</code>, return a <strong>memoized</strong> version of that function.</p>
|
||||
|
||||
<p>A <strong>memoized </strong>function is a function that will never be called twice with the same inputs. Instead it will returned a cached value.</p>
|
||||
|
||||
<p>You can assume there are <strong>3 </strong>possible input functions: <code>sum</code><strong>, </strong><code>fib</code><strong>, </strong>and <code>factorial</code><strong>.</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>sum</code><strong> </strong>accepts two integers <code>a</code> and <code>b</code> and returns <code>a + b</code>.</li>
|
||||
<li><code>fib</code><strong> </strong>accepts a single integer <code>n</code> and returns <code>1</code> if <font face="monospace"><code>n <= 1</code> </font>or<font face="monospace"> <code>fib(n - 1) + fib(n - 2)</code> </font>otherwise.</li>
|
||||
<li><code>factorial</code> accepts a single integer <code>n</code> and returns <code>1</code> if <code>n <= 1</code> or <code>factorial(n - 1) * n</code> otherwise.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
"sum"
|
||||
["call","call","getCallCount","call","getCallCount"]
|
||||
[[2,2],[2,2],[],[1,2],[]]
|
||||
<strong>Output</strong>
|
||||
[4,4,1,3,2]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
const sum = (a, b) => a + b;
|
||||
const memoizedSum = memoize(sum);
|
||||
memoizedSum(2, 2); // Returns 4. sum() was called as (2, 2) was not seen before.
|
||||
memoizedSum(2, 2); // Returns 4. However sum() was not called because the same inputs were seen before.
|
||||
// Total call count: 1
|
||||
memoizedSum(1, 2); // Returns 3. sum() was called as (1, 2) was not seen before.
|
||||
// Total call count: 2
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input
|
||||
</strong>"factorial"
|
||||
["call","call","call","getCallCount","call","getCallCount"]
|
||||
[[2],[3],[2],[],[3],[]]
|
||||
<strong>Output</strong>
|
||||
[2,6,2,2,6,2]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
const factorial = (n) => (n <= 1) ? 1 : (n * factorial(n - 1));
|
||||
const memoFactorial = memoize(factorial);
|
||||
memoFactorial(2); // Returns 2.
|
||||
memoFactorial(3); // Returns 6.
|
||||
memoFactorial(2); // Returns 2. However factorial was not called because 2 was seen before.
|
||||
// Total call count: 2
|
||||
memoFactorial(3); // Returns 6. However factorial was not called because 3 was seen before.
|
||||
// Total call count: 2
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input
|
||||
</strong>"fib"
|
||||
["call","getCallCount"]
|
||||
[[5],[]]
|
||||
<strong>Output</strong>
|
||||
[8,1]
|
||||
|
||||
<strong>Explanation
|
||||
</strong>fib(5) = 8
|
||||
// Total call count: 1
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= a, b <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= n <= 10</code></li>
|
||||
<li><code>at most 10<sup>5</sup> function calls</code></li>
|
||||
<li><code>at most 10<sup>5</sup> attempts to access callCount</code></li>
|
||||
<li><code>input function is sum, fib, or factorial</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>There is a <strong>directed weighted</strong> graph that consists of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The edges of the graph are initially represented by the given array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, edgeCost<sub>i</sub>]</code> meaning that there is an edge from <code>from<sub>i</sub></code> to <code>to<sub>i</sub></code> with the cost <code>edgeCost<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Implement the <code>Graph</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>Graph(int n, int[][] edges)</code> initializes the object with <code>n</code> nodes and the given edges.</li>
|
||||
<li><code>addEdge(int[] edge)</code> adds an edge to the list of edges where <code>edge = [from, to, edgeCost]</code>. It is guaranteed that there is no edge between the two nodes before adding this one.</li>
|
||||
<li><code>int shortestPath(int node1, int node2)</code> returns the <strong>minimum</strong> cost of a path from <code>node1</code> to <code>node2</code>. If no path exists, return <code>-1</code>. The cost of a path is the sum of the costs of the edges in the path.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/11/graph3drawio-2.png" style="width: 621px; height: 191px;" />
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["Graph", "shortestPath", "shortestPath", "addEdge", "shortestPath"]
|
||||
[[4, [[0, 2, 5], [0, 1, 2], [1, 2, 1], [3, 0, 3]]], [3, 2], [0, 3], [[1, 3, 4]], [0, 3]]
|
||||
<strong>Output</strong>
|
||||
[null, 6, -1, null, 6]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
Graph g = new Graph(4, [[0, 2, 5], [0, 1, 2], [1, 2, 1], [3, 0, 3]]);
|
||||
g.shortestPath(3, 2); // return 6. The shortest path from 3 to 2 in the first diagram above is 3 -> 0 -> 1 -> 2 with a total cost of 3 + 2 + 1 = 6.
|
||||
g.shortestPath(0, 3); // return -1. There is no path from 0 to 3.
|
||||
g.addEdge([1, 3, 4]); // We add an edge from node 1 to node 3, and we get the second diagram above.
|
||||
g.shortestPath(0, 3); // return 6. The shortest path from 0 to 3 now is 0 -> 1 -> 3 with a total cost of 2 + 4 = 6.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>0 <= edges.length <= n * (n - 1)</code></li>
|
||||
<li><code>edges[i].length == edge.length == 3</code></li>
|
||||
<li><code>0 <= from<sub>i</sub>, to<sub>i</sub>, from, to, node1, node2 <= n - 1</code></li>
|
||||
<li><code>1 <= edgeCost<sub>i</sub>, edgeCost <= 10<sup>6</sup></code></li>
|
||||
<li>There are no repeated edges and no self-loops in the graph at any point.</li>
|
||||
<li>At most <code>100</code> calls will be made for <code>addEdge</code>.</li>
|
||||
<li>At most <code>100</code> calls will be made for <code>shortestPath</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,135 @@
|
||||
<p>Sometimes you have a long running task, and you may wish to cancel it before it completes. To help with this goal, write a function <code>cancellable</code> that accepts a generator object and returns an array of two values: a <strong>cancel function</strong> and a <strong>promise</strong>.</p>
|
||||
|
||||
<p>You may assume the generator function will only yield promises. It is your function's responsibility to pass the values resolved by the promise back to the generator. If the promise rejects, your function should throw that error back to the generator.</p>
|
||||
|
||||
<p>If the cancel callback is called before the generator is done, your function should throw an error back to the generator. That error should be the string <code>"Cancelled"</code> (Not an <code>Error</code> object). If the error was caught, the returned promise should resolve with the next value that was yielded or returned. Otherwise, the promise should reject with the thrown error. No more code should be executed.</p>
|
||||
|
||||
<p>When the generator is done, the promise your function returned should resolve the value the generator returned. If, however, the generator throws an error, the returned promise should reject with the error.</p>
|
||||
|
||||
<p>An example of how your code would be used:</p>
|
||||
|
||||
<pre>
|
||||
function* tasks() {
|
||||
const val = yield new Promise(resolve => resolve(2 + 2));
|
||||
yield new Promise(resolve => setTimeout(resolve, 100));
|
||||
return val + 1; // calculation shouldn't be done.
|
||||
}
|
||||
const [cancel, promise] = cancellable(tasks());
|
||||
setTimeout(cancel, 50);
|
||||
promise.catch(console.log); // logs "Cancelled" at t=50ms
|
||||
</pre>
|
||||
|
||||
<p>If instead <code>cancel()</code> was not called or was called after <code>t=100ms</code>, the promise would have resolved <code>5</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
generatorFunction = function*() {
|
||||
return 42;
|
||||
}
|
||||
cancelledAt = 100
|
||||
<strong>Output:</strong> {"resolved": 42}
|
||||
<strong>Explanation:</strong>
|
||||
const generator = generatorFunction();
|
||||
const [cancel, promise] = cancellable(generator);
|
||||
setTimeout(cancel, 100);
|
||||
promise.then(console.log); // resolves 42 at t=0ms
|
||||
|
||||
The generator immediately yields 42 and finishes. Because of that, the returned promise immediately resolves 42. Note that cancelling a finished generator does nothing.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
generatorFunction = function*() {
|
||||
const msg = yield new Promise(res => res("Hello"));
|
||||
throw `Error: ${msg}`;
|
||||
}
|
||||
cancelledAt = null
|
||||
<strong>Output:</strong> {"rejected": "Error: Hello"}
|
||||
<strong>Explanation:</strong>
|
||||
A promise is yielded. The function handles this by waiting for it to resolve and then passes the resolved value back to the generator. Then an error is thrown which has the effect of causing the promise to reject with the same thrown error.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
generatorFunction = function*() {
|
||||
yield new Promise(res => setTimeout(res, 200));
|
||||
return "Success";
|
||||
}
|
||||
cancelledAt = 100
|
||||
<strong>Output:</strong> {"rejected": "Cancelled"}
|
||||
<strong>Explanation:</strong>
|
||||
While the function is waiting for the yielded promise to resolve, cancel() is called. This causes an error message to be sent back to the generator. Since this error is uncaught, the returned promise rejected with this error.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
generatorFunction = function*() {
|
||||
let result = 0;
|
||||
yield new Promise(res => setTimeout(res, 100));
|
||||
result += yield new Promise(res => res(1));
|
||||
yield new Promise(res => setTimeout(res, 100));
|
||||
result += yield new Promise(res => res(1));
|
||||
return result;
|
||||
}
|
||||
cancelledAt = null
|
||||
<strong>Output:</strong> {"resolved": 2}
|
||||
<strong>Explanation:</strong>
|
||||
4 promises are yielded. Two of those promises have their values added to the result. After 200ms, the generator finishes with a value of 2, and that value is resolved by the returned promise.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
generatorFunction = function*() {
|
||||
let result = 0;
|
||||
try {
|
||||
yield new Promise(res => setTimeout(res, 100));
|
||||
result += yield new Promise(res => res(1));
|
||||
yield new Promise(res => setTimeout(res, 100));
|
||||
result += yield new Promise(res => res(1));
|
||||
} catch(e) {
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
cancelledAt = 150
|
||||
<strong>Output:</strong> {"resolved": 1}
|
||||
<strong>Explanation:</strong>
|
||||
The first two yielded promises resolve and cause the result to increment. However, at t=150ms, the generator is cancelled. The error sent to the generator is caught and the result is returned and finally resolved by the returned promise.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 6:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
generatorFunction = function*() {
|
||||
try {
|
||||
yield new Promise((resolve, reject) => reject("Promise Rejected"));
|
||||
} catch(e) {
|
||||
let a = yield new Promise(resolve => resolve(2));
|
||||
let b = yield new Promise(resolve => resolve(2));
|
||||
return a + b;
|
||||
};
|
||||
}
|
||||
cancelledAt = null
|
||||
<strong>Output:</strong> {"resolved": 4}
|
||||
<strong>Explanation:</strong>
|
||||
The first yielded promise immediately rejects. This error is caught. Because the generator hasn't been cancelled, execution continues as usual. It ends up resolving 2 + 2 = 4.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>cancelledAt == null or 0 <= cancelledAt <= 1000</code></li>
|
||||
<li><code>generatorFunction returns a generator object</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>Given an integer array <code>arr</code> and a mapping function <code>fn</code>, return a new array with a transformation applied to each element.</p>
|
||||
|
||||
<p>The returned array should be created such that <code>returnedArray[i] = fn(arr[i], i)</code>.</p>
|
||||
|
||||
<p>Please solve it without the built-in <code>Array.map</code> method.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [1,2,3], fn = function plusone(n) { return n + 1; }
|
||||
<strong>Output:</strong> [2,3,4]
|
||||
<strong>Explanation:</strong>
|
||||
const newArray = map(arr, plusone); // [2,3,4]
|
||||
The function increases each value in the array by one.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [1,2,3], fn = function plusI(n, i) { return n + i; }
|
||||
<strong>Output:</strong> [1,3,5]
|
||||
<strong>Explanation:</strong> The function increases each value by the index it resides in.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [10,20,30], fn = function constant() { return 42; }
|
||||
<strong>Output:</strong> [42,42,42]
|
||||
<strong>Explanation:</strong> The function always returns 42.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= arr.length <= 1000</code></li>
|
||||
<li><code><font face="monospace">-10<sup>9</sup> <= arr[i] <= 10<sup>9</sup></font></code></li>
|
||||
<li><font face="monospace"><code>fn returns a number</code></font></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>Given an integer array <code>arr</code> and a filtering function <code>fn</code>, return a new array with a fewer or equal number of elements.</p>
|
||||
|
||||
<p>The returned array should only contain elements where <code>fn(arr[i], i)</code> evaluated to a truthy value.</p>
|
||||
|
||||
<p>Please solve it without the built-in <code>Array.filter</code> method.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [0,10,20,30], fn = function greaterThan10(n) { return n > 10; }
|
||||
<strong>Output:</strong> [20,30]
|
||||
<strong>Explanation:</strong>
|
||||
const newArray = filter(arr, fn); // [20, 30]
|
||||
The function filters out values that are not greater than 10</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [1,2,3], fn = function firstIndex(n, i) { return i === 0; }
|
||||
<strong>Output:</strong> [1]
|
||||
<strong>Explanation:</strong>
|
||||
fn can also accept the index of each element
|
||||
In this case, the function removes elements not at index 0
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [-2,-1,0,1,2], fn = function plusOne(n) { return n + 1 }
|
||||
<strong>Output:</strong> [-2,0,1,2]
|
||||
<strong>Explanation:</strong>
|
||||
Falsey values such as 0 should be filtered out
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= arr.length <= 1000</code></li>
|
||||
<li><code><font face="monospace">-10<sup>9</sup> <= arr[i] <= 10<sup>9</sup></font></code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user