mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-04 23:11:41 +08:00
update
This commit is contained in:
@@ -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>
|
25
leetcode/problem/array-prototype-last.html
Normal file
25
leetcode/problem/array-prototype-last.html
Normal file
@@ -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>
|
62
leetcode/problem/array-reduce-transformation.html
Normal file
62
leetcode/problem/array-reduce-transformation.html
Normal file
@@ -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>
|
56
leetcode/problem/cache-with-time-limit.html
Normal file
56
leetcode/problem/cache-with-time-limit.html
Normal file
@@ -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>
|
40
leetcode/problem/check-if-object-instance-of-class.html
Normal file
40
leetcode/problem/check-if-object-instance-of-class.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>Write a function that checks if a given object is an instance of a given class or superclass.</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 = () => checkIfInstance(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 checkIfInstance(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 = () => checkIfInstance(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 = () => checkIfInstance(5, Number)
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>5 is a Number. Note that the "instanceof" keyword would return false.
|
||||
</pre>
|
48
leetcode/problem/convert-object-to-json-string.html
Normal file
48
leetcode/problem/convert-object-to-json-string.html
Normal file
@@ -0,0 +1,48 @@
|
||||
<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>
|
||||
</ul>
|
33
leetcode/problem/counter.html
Normal file
33
leetcode/problem/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>
|
65
leetcode/problem/curry.html
Normal file
65
leetcode/problem/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>
|
77
leetcode/problem/debounce.html
Normal file
77
leetcode/problem/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>
|
42
leetcode/problem/filter-elements-from-array.html
Normal file
42
leetcode/problem/filter-elements-from-array.html
Normal file
@@ -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>
|
55
leetcode/problem/flatten-deeply-nested-array.html
Normal file
55
leetcode/problem/flatten-deeply-nested-array.html
Normal file
@@ -0,0 +1,55 @@
|
||||
<p>Given a <strong>multi-dimensional</strong> array <code>arr</code> and a depth <code>n</code>, return a <strong>flattened</strong> version of that array.</p>
|
||||
|
||||
<p>A <strong>multi-dimensional</strong> array is a recursive data structure that contains integers or other <strong>multi-dimensional</strong> arrays.</p>
|
||||
|
||||
<p>A <strong>flattened</strong> array is a version of that array with some or all of the sub-arrays removed and replaced with the actual elements in that sub-array. This flattening operation should only be done if the current depth of nesting is greater than <code>n</code>. The depth of the elements in the first array are considered to be <code>0</code>.</p>
|
||||
|
||||
<p>Please solve it without the built-in <code>Array.flat</code> method.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 0
|
||||
<strong>Output</strong>
|
||||
[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
Passing a depth of n=0 will always result in the original array. This is because the smallest possible depth of a subarray (0) is not less than n=0. Thus, no subarray should be flattened. </pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 1
|
||||
<strong>Output</strong>
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
The subarrays starting with 4, 7, and 13 are all flattened. This is because their depth of 0 is less than 1. However [9, 10, 11] remains unflattened because its depth is 1.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
arr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 2
|
||||
<strong>Output</strong>
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
The maximum depth of any subarray is 1. Thus, all of them are flattened.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= count of numbers in arr <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= count of subarrays in arr <= 10<sup>5</sup></code></li>
|
||||
<li><code>maxDepth <= 1000</code></li>
|
||||
<li><code>-1000 <= each number <= 1000</code></li>
|
||||
<li><code><font face="monospace">0 <= n <= 1000</font></code></li>
|
||||
</ul>
|
50
leetcode/problem/function-composition.html
Normal file
50
leetcode/problem/function-composition.html
Normal file
@@ -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>
|
83
leetcode/problem/group-by.html
Normal file
83
leetcode/problem/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>
|
48
leetcode/problem/json-deep-equal.html
Normal file
48
leetcode/problem/json-deep-equal.html
Normal file
@@ -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>
|
56
leetcode/problem/memoize-ii.html
Normal file
56
leetcode/problem/memoize-ii.html
Normal file
@@ -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/problem/memoize.html
Normal file
80
leetcode/problem/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,32 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once amongst the <code>p</code> pairs.</p>
|
||||
|
||||
<p>Note that for a pair of elements at the index <code>i</code> and <code>j</code>, the difference of this pair is <code>|nums[i] - nums[j]|</code>, where <code>|x|</code> represents the <strong>absolute</strong> <strong>value</strong> of <code>x</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> <strong>maximum</strong> difference among all </em><code>p</code> <em>pairs.</em> We define the maximum of an empty set to be zero.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,1,2,7,1,3], p = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The first pair is formed from the indices 1 and 4, and the second pair is formed from the indices 2 and 5.
|
||||
The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1. Therefore, we return 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,2,1,2], p = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Let the indices 1 and 3 form a pair. The difference of that pair is |2 - 2| = 0, which is the minimum we can attain.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= p <= (nums.length)/2</code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>grid</code>. Your initial position is at the <strong>top-left</strong> cell <code>(0, 0)</code>.</p>
|
||||
|
||||
<p>Starting from the cell <code>(i, j)</code>, you can move to one of the following cells:</p>
|
||||
|
||||
<ul>
|
||||
<li>Cells <code>(i, k)</code> with <code>j < k <= grid[i][j] + j</code> (rightward movement), or</li>
|
||||
<li>Cells <code>(k, j)</code> with <code>i < k <= grid[i][j] + i</code> (downward movement).</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum number of cells you need to visit to reach the <strong>bottom-right</strong> cell</em> <code>(m - 1, n - 1)</code>. If there is no valid path, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex1.png" style="width: 271px; height: 171px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The image above shows one of the paths that visits exactly 4 cells.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex2.png" style="width: 271px; height: 171px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation: </strong>The image above shows one of the paths that visits exactly 3 cells.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/26/ex3.png" style="width: 181px; height: 81px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[2,1,0],[1,0,0]]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be proven that no path exists.
|
||||
</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 <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= grid[i][j] < m * n</code></li>
|
||||
<li><code>grid[m - 1][n - 1] == 0</code></li>
|
||||
</ul>
|
40
leetcode/problem/prime-in-diagonal.html
Normal file
40
leetcode/problem/prime-in-diagonal.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>You are given a 0-indexed two-dimensional integer array <code>nums</code>.</p>
|
||||
|
||||
<p>Return <em>the largest <strong>prime</strong> number that lies on at least one of the <b>diagonals</b> of </em><code>nums</code>. In case, no prime is present on any of the diagonals, return<em> 0.</em></p>
|
||||
|
||||
<p>Note that:</p>
|
||||
|
||||
<ul>
|
||||
<li>An integer is <strong>prime</strong> if it is greater than <code>1</code> and has no positive integer divisors other than <code>1</code> and itself.</li>
|
||||
<li>An integer <code>val</code> is on one of the <strong>diagonals</strong> of <code>nums</code> if there exists an integer <code>i</code> for which <code>nums[i][i] = val</code> or an <code>i</code> for which <code>nums[i][nums.length - i - 1] = val</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/03/06/screenshot-2023-03-06-at-45648-pm.png" style="width: 181px; height: 121px;" /></p>
|
||||
|
||||
<p>In the above diagram, one diagonal is <strong>[1,5,9]</strong> and another diagonal is<strong> [3,5,7]</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[1,2,3],[5,6,7],[9,10,11]]
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> The numbers 1, 3, 6, 9, and 11 are the only numbers present on at least one of the diagonals. Since 11 is the largest prime, we return 11.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[1,2,3],[5,17,7],[9,11,10]]
|
||||
<strong>Output:</strong> 17
|
||||
<strong>Explanation:</strong> The numbers 1, 3, 9, 10, and 17 are all present on at least one of the diagonals. 17 is the largest prime, so we return 17.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 300</code></li>
|
||||
<li><code>nums.length == nums<sub>i</sub>.length</code></li>
|
||||
<li><code>1 <= nums<span style="font-size: 10.8333px;">[i][j]</span> <= 4*10<sup>6</sup></code></li>
|
||||
</ul>
|
71
leetcode/problem/promise-pool.html
Normal file
71
leetcode/problem/promise-pool.html
Normal file
@@ -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>
|
71
leetcode/problem/promise-time-limit.html
Normal file
71
leetcode/problem/promise-time-limit.html
Normal file
@@ -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>
|
29
leetcode/problem/sleep.html
Normal file
29
leetcode/problem/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>
|
58
leetcode/problem/snail-traversal.html
Normal file
58
leetcode/problem/snail-traversal.html
Normal file
@@ -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 [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]<code> </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>
|
34
leetcode/problem/sum-of-distances.html
Normal file
34
leetcode/problem/sum-of-distances.html
Normal file
@@ -0,0 +1,34 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. There exists an array <code>arr</code> of length <code>nums.length</code>, where <code>arr[i]</code> is the sum of <code>|i - j|</code> over all <code>j</code> such that <code>nums[j] == nums[i]</code> and <code>j != i</code>. If there is no such <code>j</code>, set <code>arr[i]</code> to be <code>0</code>.</p>
|
||||
|
||||
<p>Return <em>the array </em><code>arr</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,1,1,2]
|
||||
<strong>Output:</strong> [5,0,3,4,0]
|
||||
<strong>Explanation:</strong>
|
||||
When i = 0, nums[0] == nums[2] and nums[0] == nums[3]. Therefore, arr[0] = |0 - 2| + |0 - 3| = 5.
|
||||
When i = 1, arr[1] = 0 because there is no other index with value 3.
|
||||
When i = 2, nums[2] == nums[0] and nums[2] == nums[3]. Therefore, arr[2] = |2 - 0| + |2 - 3| = 3.
|
||||
When i = 3, nums[3] == nums[0] and nums[3] == nums[2]. Therefore, arr[3] = |3 - 0| + |3 - 2| = 4.
|
||||
When i = 4, arr[4] = 0 because there is no other index with value 2.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,5,3]
|
||||
<strong>Output:</strong> [0,0,0]
|
||||
<strong>Explanation:</strong> Since each element in nums is distinct, arr[i] = 0 for all i.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user