mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-02 05:13:29 +08:00
update
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<p>Design a <code>Calculator</code> class. The class should provide the mathematical operations of addition, subtraction, multiplication, division, and exponentiation. It should also allow consecutive operations to be performed using method chaining. The <code>Calculator</code> class constructor should accept a number which serves as the initial value of <code>result</code>.</p>
|
||||
|
||||
<p>Your <font face="monospace"><code>Calculator</code> </font>class should have the following methods:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>add</code> - This method adds the given number <code>value</code> to the <code>result</code> and returns the updated <code>Calculator</code>.</li>
|
||||
<li><code>subtract</code> - This method subtracts the given number <code>value</code> from the <code>result</code> and returns the updated <code>Calculator</code>.</li>
|
||||
<li><code>multiply</code> - This method multiplies the <code>result</code> by the given number <code>value</code> and returns the updated <code>Calculator</code>.</li>
|
||||
<li><code>divide</code> - This method divides the <code>result</code> by the given number <code>value</code> and returns the updated <code>Calculator</code>. If the passed value is <code>0</code>, an error <code>"Division by zero is not allowed"</code> should be thrown.</li>
|
||||
<li><code>power</code> - This method raises the <code>result</code> to the power of the given number <code>value</code> and returns the updated <code>Calculator</code>.</li>
|
||||
<li><code>getResult</code> - This method returns the <code>result</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Solutions within <code>10<sup>-5</sup></code> of the actual result are considered correct.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> actions = ["Calculator", "add", "subtract", "getResult"], values = [10, 5, 7]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
new Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> actions = ["Calculator", "multiply", "power", "getResult"], values = [2, 5, 2]
|
||||
<strong>Output:</strong> 100
|
||||
<strong>Explanation:</strong>
|
||||
new Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> actions = ["Calculator", "divide", "getResult"], values = [20, 0]
|
||||
<strong>Output:</strong> "Division by zero is not allowed"
|
||||
<strong>Explanation:</strong>
|
||||
new Calculator(20).divide(0).getResult() // 20 / 0
|
||||
|
||||
The error should be thrown because we cannot divide by zero.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= actions.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= values.length <= 2 * 10<sup>4</sup></code><code> - 1</code></li>
|
||||
<li><code>actions[i] is one of "Calculator", "add", "subtract", "multiply", "divide", "power", and "getResult"</code></li>
|
||||
<li><code><font face="monospace">Last action is always "getResult"</font></code></li>
|
||||
<li><code><font face="monospace">values is a JSON array of numbers</font></code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>You are given an integer <code>n</code> that consists of exactly <code>3</code> digits.</p>
|
||||
|
||||
<p>We call the number <code>n</code> <strong>fascinating</strong> if, after the following modification, the resulting number contains all the digits from <code>1</code> to <code>9</code> <strong>exactly</strong> once and does not contain any <code>0</code>'s:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Concatenate</strong> <code>n</code> with the numbers <code>2 * n</code> and <code>3 * n</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>true</code><em> if </em><code>n</code><em> is fascinating, or </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p><strong>Concatenating</strong> two numbers means joining them together. For example, the concatenation of <code>121</code> and <code>371</code> is <code>121371</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 192
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We concatenate the numbers n = 192 and 2 * n = 384 and 3 * n = 576. The resulting number is 192384576. This number contains all the digits from 1 to 9 exactly once.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 100
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> We concatenate the numbers n = 100 and 2 * n = 200 and 3 * n = 300. The resulting number is 100200300. This number does not satisfy any of the conditions.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>100 <= n <= 999</code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>Given an object or an array, return if it is empty.</p>
|
||||
|
||||
<ul>
|
||||
<li>An empty object contains no key-value pairs.</li>
|
||||
<li>An empty array contains no elements.</li>
|
||||
</ul>
|
||||
|
||||
<p>You may assume the object or array is the output of <code>JSON.parse</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> obj = {"x": 5, "y": 42}
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The object has 2 key-value pairs so it is not empty.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> obj = {}
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The object doesn't have any key-value pairs so it is empty.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> obj = [null, false, 0]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The array has 3 elements so it is not empty.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li> <code>2 <= JSON.stringify(obj).length <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<strong>Can you solve it in O(1) time?</strong>
|
@@ -0,0 +1,52 @@
|
||||
<p>You are given a <strong>0-indexed</strong> permutation of <code>n</code> integers <code>nums</code>.</p>
|
||||
|
||||
<p>A permutation is called <strong>semi-ordered</strong> if the first number equals <code>1</code> and the last number equals <code>n</code>. You can perform the below operation as many times as you want until you make <code>nums</code> a <strong>semi-ordered</strong> permutation:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick two adjacent elements in <code>nums</code>, then swap them.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum number of operations to make </em><code>nums</code><em> a <strong>semi-ordered permutation</strong></em>.</p>
|
||||
|
||||
<p>A <strong>permutation</strong> is a sequence of integers from <code>1</code> to <code>n</code> of length <code>n</code> containing each number exactly once.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,4,3]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can make the permutation semi-ordered using these sequence of operations:
|
||||
1 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3].
|
||||
2 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4].
|
||||
It can be proved that there is no sequence of less than two operations that make nums a semi-ordered permutation.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,1,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can make the permutation semi-ordered using these sequence of operations:
|
||||
1 - swap i = 1 and j = 2. The permutation becomes [2,1,4,3].
|
||||
2 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3].
|
||||
3 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4].
|
||||
It can be proved that there is no sequence of less than three operations that make nums a semi-ordered permutation.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,4,2,5]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The permutation is already a semi-ordered permutation.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length == n <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
<li><code>nums is a permutation.</code></li>
|
||||
</ul>
|
@@ -0,0 +1,60 @@
|
||||
<p>Given an array of asynchronous functions <code>functions</code>, return a new promise <code>promise</code>. Each function in the array accepts no arguments and returns a promise.</p>
|
||||
|
||||
<p><code>promise</code> resolves:</p>
|
||||
|
||||
<ul>
|
||||
<li>When all the promises returned from <code>functions</code> were resolved successfully. The resolved value of <code>promise</code> should be an array of all the resolved values of promises in the same order as they were in the <code>functions.</code></li>
|
||||
</ul>
|
||||
|
||||
<p><code>promise</code> rejects:</p>
|
||||
|
||||
<ul>
|
||||
<li>When any of the promises returned from <code>functions</code> were rejected. <code>promise</code> should also reject with the reason of the first rejection.</li>
|
||||
</ul>
|
||||
|
||||
<p>Please solve it without using the built-in <code>Promise.all</code> function.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> functions = [
|
||||
() => new Promise(resolve => setTimeout(() => resolve(5), 200))
|
||||
]
|
||||
<strong>Output:</strong> {"t": 200, "resolved": [5]}
|
||||
<strong>Explanation:</strong>
|
||||
promiseAll(functions).then(console.log); // [5]
|
||||
|
||||
The single function was resolved at 200ms with a value of 5.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> functions = [
|
||||
() => new Promise(resolve => setTimeout(() => resolve(1), 200)),
|
||||
() => new Promise((resolve, reject) => setTimeout(() => reject("Error"), 100))
|
||||
]
|
||||
<strong>Output:</strong> {"t": 100, "rejected": "Error"}
|
||||
<strong>Explanation:</strong> Since one of the promises rejected, the returned promise also rejected with the same error at the same time.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> functions = [
|
||||
() => new Promise(resolve => setTimeout(() => resolve(4), 50)),
|
||||
() => new Promise(resolve => setTimeout(() => resolve(10), 150)),
|
||||
() => new Promise(resolve => setTimeout(() => resolve(16), 100))
|
||||
]
|
||||
<strong>Output:</strong> {"t": 150, "resolved": [4, 10, 16]}
|
||||
<strong>Explanation:</strong> All the promises resolved with a value. The returned promise resolved when the last promise resolved.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>functions is an array of functions that returns promises</code></li>
|
||||
<li><code>1 <= functions.length <= 10</code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>Given a function <code>fn</code>, an array or arguments <code>args</code>, and a timeout <code>t</code> in milliseconds, return a cancel function <code>cancelFn</code>.</p>
|
||||
|
||||
<p>After a delay of <code>t</code>, <code>fn</code> should be called with <code>args</code> passed as parameters <strong>unless</strong> <code>cancelFn</code> was called first. In that case, <code>fn</code> should never be called.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> fn = (x) => x * 5, args = [2], t = 20, cancelTime = 50
|
||||
<strong>Output:</strong> [{"time": 20, "returned": 10}]
|
||||
<strong>Explanation:</strong>
|
||||
const cancel = cancellable(fn, [2], 20); // fn(2) called at t=20ms
|
||||
setTimeout(cancel, 50);
|
||||
|
||||
the cancelTime (50ms) is after the delay time (20ms), so fn(2) should be called at t=20ms. The value returned from fn is 10.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> fn = (x) => x**2, args = [2], t = 100, cancelTime = 50
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> fn(2) was never called because cancelTime (50ms) is before the delay time (100ms).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> fn = (x1, x2) => x1 * x2, args = [2,4], t = 30, cancelTime = 100
|
||||
<strong>Output:</strong> [{"time": 30, "returned": 8}]
|
||||
<strong>Explanation:</strong> fn(2, 4) was called at t=30ms because cancelTime > t.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>fn is a function</code></li>
|
||||
<li><code>args is a valid JSON array</code></li>
|
||||
<li><code>1 <= args.length <= 10</code></li>
|
||||
<li><code><font face="monospace">20 <= t <= 1000</font></code></li>
|
||||
<li><code><font face="monospace">10 <= cancelT <= 1000</font></code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>You are given a string <code>s</code> consisting of only lowercase English letters. In one operation, you can do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Select any non-empty substring of <code>s</code>, possibly the entire string, then replace each one of its characters with the previous character of the English alphabet. For example, 'b' is converted to 'a', and 'a' is converted to 'z'.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>lexicographically smallest</strong> string you can obtain after performing the above operation <strong>exactly once</strong>.</em></p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters in a string.</p>
|
||||
A string <code>x</code> is <strong>lexicographically smaller</strong> than a string <code>y</code> of the same length if <code>x[i]</code> comes before <code>y[i]</code> in alphabetic order for the first position <code>i</code> such that <code>x[i] != y[i]</code>.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "cbabc"
|
||||
<strong>Output:</strong> "baabc"
|
||||
<strong>Explanation:</strong> We apply the operation on the substring starting at index 0, and ending at index 1 inclusive.
|
||||
It can be proven that the resulting string is the lexicographically smallest.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "acbbc"
|
||||
<strong>Output:</strong> "abaab"
|
||||
<strong>Explanation:</strong> We apply the operation on the substring starting at index 1, and ending at index 4 inclusive.
|
||||
It can be proven that the resulting string is the lexicographically smallest.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "leetcode"
|
||||
<strong>Output:</strong> "kddsbncd"
|
||||
<strong>Explanation:</strong> We apply the operation on the entire string.
|
||||
It can be proven that the resulting string is the lexicographically smallest.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase English letters</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>s</code> that consists of digits from <code>0</code> to <code>9</code>.</p>
|
||||
|
||||
<p>A string <code>t</code> is called a <strong>semi-repetitive</strong> if there is at most one consecutive pair of the same digits inside <code>t</code>. For example, <code>0010</code>, <code>002020</code>, <code>0123</code>, <code>2002</code>, and <code>54944</code> are semi-repetitive while <code>00101022</code>, and <code>1101234883</code> are not.</p>
|
||||
|
||||
<p>Return <em>the length of the longest semi-repetitive substring inside</em> <code>s</code>.</p>
|
||||
|
||||
<p>A <b>substring</b> is a contiguous <strong>non-empty</strong> sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "52233"
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The longest semi-repetitive substring is "5223", which starts at i = 0 and ends at j = 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "5494"
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> s is a semi-reptitive string, so the answer is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "1111111"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The longest semi-repetitive substring is "11", which starts at i = 0 and ends at j = 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 50</code></li>
|
||||
<li><code>'0' <= s[i] <= '9'</code></li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p>
|
||||
|
||||
<p>Let us call a <strong>non-empty</strong> subset of rows <strong>good</strong> if the sum of each column of the subset is at most half of the length of the subset.</p>
|
||||
|
||||
<p>More formally, if the length of the chosen subset of rows is <code>k</code>, then the sum of each column should be at most <code>floor(k / 2)</code>.</p>
|
||||
|
||||
<p>Return <em>an integer array that contains row indices of a good subset sorted in <strong>ascending</strong> order.</em></p>
|
||||
|
||||
<p>If there are multiple good subsets, you can return any of them. If there are no good subsets, return an empty array.</p>
|
||||
|
||||
<p>A <strong>subset</strong> of rows of the matrix <code>grid</code> is any matrix that can be obtained by deleting some (possibly none or all) rows from <code>grid</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,1,1,0],[0,0,0,1],[1,1,1,1]]
|
||||
<strong>Output:</strong> [0,1]
|
||||
<strong>Explanation:</strong> We can choose the 0<sup>th</sup> and 1<sup>st</sup> rows to create a good subset of rows.
|
||||
The length of the chosen subset is 2.
|
||||
- The sum of the 0<sup>th</sup> column is 0 + 0 = 0, which is at most half of the length of the subset.
|
||||
- The sum of the 1<sup>st</sup> column is 1 + 0 = 1, which is at most half of the length of the subset.
|
||||
- The sum of the 2<sup>nd</sup> column is 1 + 0 = 1, which is at most half of the length of the subset.
|
||||
- The sum of the 3<sup>rd</sup> column is 0 + 1 = 1, which is at most half of the length of the subset.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0]]
|
||||
<strong>Output:</strong> [0]
|
||||
<strong>Explanation:</strong> We can choose the 0<sup>th</sup> row to create a good subset of rows.
|
||||
The length of the chosen subset is 1.
|
||||
- The sum of the 0<sup>th</sup> column is 0, which is at most half of the length of the subset.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,1],[1,1,1]]
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> It is impossible to choose any subset of rows to create a good subset.
|
||||
</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 <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= n <= 5</code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
</ul>
|
37
leetcode-cn/problem (English)/排序方式(English) [sort-by].html
Normal file
37
leetcode-cn/problem (English)/排序方式(English) [sort-by].html
Normal file
@@ -0,0 +1,37 @@
|
||||
<p>Given an array <code>arr</code> and a function <code>fn</code>, return a sorted array <code>sortedArr</code>. You can assume <code>fn</code> only returns numbers and those numbers determine the sort order of <code>sortedArr</code>. <code>sortedArray</code> must be sorted in <strong>ascending order</strong> by <code>fn</code> output.</p>
|
||||
|
||||
<p>You may assume that <code>fn</code> will never duplicate numbers for a given array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [5, 4, 1, 2, 3], fn = (x) => x
|
||||
<strong>Output:</strong> [1, 2, 3, 4, 5]
|
||||
<strong>Explanation:</strong> fn simply returns the number passed to it so the array is sorted in ascending order.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [{"x": 1}, {"x": 0}, {"x": -1}], fn = (d) => d.x
|
||||
<strong>Output:</strong> [{"x": -1}, {"x": 0}, {"x": 1}]
|
||||
<strong>Explanation:</strong> fn returns the value for the "x" key. So the array is sorted based on that value.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [[3, 4], [5, 2], [10, 1]], fn = (x) => x[1]
|
||||
<strong>Output:</strong> [[10, 1], [5, 2], [3, 4]]
|
||||
<strong>Explanation:</strong> arr is sorted in ascending order by number at index=1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>arr is a valid JSON array</code></li>
|
||||
<li><code>fn is a function that returns a number</code></li>
|
||||
<li><code>1 <= arr.length <= 5 * 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code> representing the cost of collecting different chocolates. The cost of collecting the chocolate at the index <code>i</code> is <code>nums[i]</code>. Each chocolate is of a different type, and initially, the chocolate at the index <code>i</code> is of <code>i<sup>th</sup></code> type.</p>
|
||||
|
||||
<p>In one operation, you can do the following with an incurred <strong>cost</strong> of <code>x</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Simultaneously change the chocolate of <code>i<sup>th</sup></code> type to <code>((i + 1) mod n)<sup>th</sup></code> type for all chocolates.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum cost to collect chocolates of all types, given that you can perform as many operations as you would like.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [20,1,15], x = 5
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> Initially, the chocolate types are [0,1,2]. We will buy the 1<sup>st</sup> type of chocolate at a cost of 1.
|
||||
Now, we will perform the operation at a cost of 5, and the types of chocolates will become [1,2,0]. We will buy the 2<sup>nd</sup><sup> </sup>type of chocolate at a cost of 1.
|
||||
Now, we will again perform the operation at a cost of 5, and the chocolate types will become [2,0,1]. We will buy the 0<sup>th </sup>type of chocolate at a cost of 1.
|
||||
Thus, the total cost will become (1 + 5 + 1 + 5 + 1) = 13. We can prove that this is optimal.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3], x = 4
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> We will collect all three types of chocolates at their own price without performing any operations. Therefore, the total cost is 1 + 2 + 3 = 6.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= x <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>Given an integer array <code>nums</code> containing <strong>distinct</strong> <strong>positive</strong> integers, find and return <strong>any</strong> number from the array that is neither the <strong>minimum</strong> nor the <strong>maximum</strong> value in the array, or <strong><code>-1</code></strong> if there is no such number.</p>
|
||||
|
||||
<p>Return <em>the selected integer.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,1,4]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In this example, the minimum value is 1 and the maximum value is 4. Therefore, either 2 or 3 can be valid answers.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> Since there is no number in nums that is neither the maximum nor the minimum, we cannot select a number that satisfies the given condition. Therefore, there is no answer.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,3]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Since 2 is neither the maximum nor the minimum value in nums, it is the only valid answer.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
<li>All values in <code>nums</code> are distinct</li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, each of length <code>n</code>, and a <strong>1-indexed 2D array</strong> <code>queries</code> where <code>queries[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>For the <code>i<sup>th</sup></code> query, find the <strong>maximum value</strong> of <code>nums1[j] + nums2[j]</code> among all indices <code>j</code> <code>(0 <= j < n)</code>, where <code>nums1[j] >= x<sub>i</sub></code> and <code>nums2[j] >= y<sub>i</sub></code>, or <strong>-1</strong> if there is no <code>j</code> satisfying the constraints.</p>
|
||||
|
||||
<p>Return <em>an array </em><code>answer</code><em> where </em><code>answer[i]</code><em> is the answer to the </em><code>i<sup>th</sup></code><em> query.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [4,3,1,2], nums2 = [2,4,9,5], queries = [[4,1],[1,3],[2,5]]
|
||||
<strong>Output:</strong> [6,10,7]
|
||||
<strong>Explanation:</strong>
|
||||
For the 1st query <code node="[object Object]">x<sub>i</sub> = 4</code> and <code node="[object Object]">y<sub>i</sub> = 1</code>, we can select index <code node="[object Object]">j = 0</code> since <code node="[object Object]">nums1[j] >= 4</code> and <code node="[object Object]">nums2[j] >= 1</code>. The sum <code node="[object Object]">nums1[j] + nums2[j]</code> is 6, and we can show that 6 is the maximum we can obtain.
|
||||
|
||||
For the 2nd query <code node="[object Object]">x<sub>i</sub> = 1</code> and <code node="[object Object]">y<sub>i</sub> = 3</code>, we can select index <code node="[object Object]">j = 2</code> since <code node="[object Object]">nums1[j] >= 1</code> and <code node="[object Object]">nums2[j] >= 3</code>. The sum <code node="[object Object]">nums1[j] + nums2[j]</code> is 10, and we can show that 10 is the maximum we can obtain.
|
||||
|
||||
For the 3rd query <code node="[object Object]">x<sub>i</sub> = 2</code> and <code node="[object Object]">y<sub>i</sub> = 5</code>, we can select index <code node="[object Object]">j = 3</code> since <code node="[object Object]">nums1[j] >= 2</code> and <code node="[object Object]">nums2[j] >= 5</code>. The sum <code node="[object Object]">nums1[j] + nums2[j]</code> is 7, and we can show that 7 is the maximum we can obtain.
|
||||
|
||||
Therefore, we return <code node="[object Object]">[6,10,7]</code>.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,2,5], nums2 = [2,3,4], queries = [[4,4],[3,2],[1,1]]
|
||||
<strong>Output:</strong> [9,9,9]
|
||||
<strong>Explanation:</strong> For this example, we can use index <code node="[object Object]">j = 2</code> for all the queries since it satisfies the constraints for each query.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [2,1], nums2 = [2,3], queries = [[3,3]]
|
||||
<strong>Output:</strong> [-1]
|
||||
<strong>Explanation:</strong> There is one query in this example with <code node="[object Object]">x<sub>i</sub></code> = 3 and <code node="[object Object]">y<sub>i</sub></code> = 3. For every index, j, either nums1[j] < <code node="[object Object]">x<sub>i</sub></code> or nums2[j] < <code node="[object Object]">y<sub>i</sub></code>. Hence, there is no solution.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums1.length == nums2.length</code> </li>
|
||||
<li><code>n == nums1.length </code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums1[i], nums2[i] <= 10<sup>9</sup> </code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[i].length == 2</code></li>
|
||||
<li><code>x<sub>i</sub> == queries[i][1]</code></li>
|
||||
<li><code>y<sub>i</sub> == queries[i][2]</code></li>
|
||||
<li><code>1 <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>Given a <strong>0-indexed</strong> string <code>s</code>, repeatedly perform the following operation <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose an index <code>i</code> in the string, and let <code>c</code> be the character in position <code>i</code>. <strong>Delete</strong> the <strong>closest occurrence</strong> of <code>c</code> to the <strong>left</strong> of <code>i</code> (if any) and the <strong>closest occurrence</strong> of <code>c</code> to the <strong>right</strong> of <code>i</code> (if any).</li>
|
||||
</ul>
|
||||
|
||||
<p>Your task is to <strong>minimize</strong> the length of <code>s</code> by performing the above operation any number of times.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the length of the <strong>minimized</strong> string.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aaabc"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> In this example, s is "aaabc". We can start by selecting the character 'a' at index 1. We then remove the closest 'a' to the left of index 1, which is at index 0, and the closest 'a' to the right of index 1, which is at index 2. After this operation, the string becomes "abc". Any further operation we perform on the string will leave it unchanged. Therefore, the length of the minimized string is 3.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "cbbd"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> For this we can start with character 'b' at index 1. There is no occurrence of 'b' to the left of index 1, but there is one to the right at index 2, so we delete the 'b' at index 2. The string becomes "cbd" and further operations will leave it unchanged. Hence, the minimized length is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "dddaaa"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> For this, we can start with the character 'd' at index 1. The closest occurrence of a 'd' to its left is at index 0, and the closest occurrence of a 'd' to its right is at index 2. We delete both index 0 and 2, so the string becomes "daaa". In the new string, we can select the character 'a' at index 2. The closest occurrence of an 'a' to its left is at index 1, and the closest occurrence of an 'a' to its right is at index 3. We delete both of them, and the string becomes "da". We cannot minimize this further, so the minimized length is 2.
|
||||
</pre>
|
||||
|
||||
<div class="notranslate" style="all: initial;"> </div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> contains only lowercase English letters</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given an integer <code>n</code> and a <strong>0-indexed</strong> <strong>2D array</strong> <code>queries</code> where <code>queries[i] = [type<sub>i</sub>, index<sub>i</sub>, val<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>Initially, there is a <strong>0-indexed</strong> <code>n x n</code> matrix filled with <code>0</code>'s. For each query, you must apply one of the following changes:</p>
|
||||
|
||||
<ul>
|
||||
<li>if <code>type<sub>i</sub> == 0</code>, set the values in the row with <code>index<sub>i</sub></code> to <code>val<sub>i</sub></code>, overwriting any previous values.</li>
|
||||
<li>if <code>type<sub>i</sub> == 1</code>, set the values in the column with <code>index<sub>i</sub></code> to <code>val<sub>i</sub></code>, overwriting any previous values.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the sum of integers in the matrix after all queries are applied</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/11/exm1.png" style="width: 681px; height: 161px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, queries = [[0,0,1],[1,2,2],[0,2,3],[1,0,4]]
|
||||
<strong>Output:</strong> 23
|
||||
<strong>Explanation:</strong> The image above describes the matrix after each query. The sum of the matrix after all queries are applied is 23.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/11/exm2.png" style="width: 681px; height: 331px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, queries = [[0,0,4],[0,1,2],[1,0,1],[0,2,3],[1,2,1]]
|
||||
<strong>Output:</strong> 17
|
||||
<strong>Explanation:</strong> The image above describes the matrix after each query. The sum of the matrix after all queries are applied is 17.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>queries[i].length == 3</code></li>
|
||||
<li><code>0 <= type<sub>i</sub> <= 1</code></li>
|
||||
<li><code>0 <= index<sub>i</sub> < n</code></li>
|
||||
<li><code>0 <= val<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,77 @@
|
||||
<p>Given two arrays <code>arr1</code> and <code>arr2</code>, return a new array <code>joinedArray</code>. All the objects in each of the two inputs arrays will contain an <code>id</code> field that has an integer value. <code>joinedArray</code> is an array formed by merging <code>arr1</code> and <code>arr2</code> based on their <code>id</code> key. The length of <code>joinedArray</code> should be the length of unique values of <code>id</code>. The returned array should be sorted in <strong>ascending</strong> order based on the <code>id</code> key.</p>
|
||||
|
||||
<p>If a given <code>id</code> exists in one array but not the other, the single object with that <code>id</code> should be included in the result array without modification.</p>
|
||||
|
||||
<p>If two objects share an <code>id</code>, their properties should be merged into a single object:</p>
|
||||
|
||||
<ul>
|
||||
<li>If a key only exists in one object, that single key-value pair should be included in the object.</li>
|
||||
<li>If a key is included in both objects, the value in the object from <code>arr2</code> should override the value from <code>arr1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
arr1 = [
|
||||
{"id": 1, "x": 1},
|
||||
{"id": 2, "x": 9}
|
||||
],
|
||||
arr2 = [
|
||||
{"id": 3, "x": 5}
|
||||
]
|
||||
<strong>Output:</strong>
|
||||
[
|
||||
{"id": 1, "x": 1},
|
||||
{"id": 2, "x": 9},
|
||||
{"id": 3, "x": 5}
|
||||
]
|
||||
<strong>Explanation:</strong> There are no duplicate ids so arr1 is simply concatenated with arr2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
arr1 = [
|
||||
{"id": 1, "x": 2, "y": 3},
|
||||
{"id": 2, "x": 3, "y": 6}
|
||||
],
|
||||
arr2 = [
|
||||
{"id": 2, "x": 10, "y": 20},
|
||||
{"id": 3, "x": 0, "y": 0}
|
||||
]
|
||||
<strong>Output:</strong>
|
||||
[
|
||||
{"id": 1, "x": 2, "y": 3},
|
||||
{"id": 2, "x": 10, "y": 20},
|
||||
{"id": 3, "x": 0, "y": 0}
|
||||
]
|
||||
<strong>Explanation:</strong> The two objects with id=1 and id=3 are included in the result array without modifiction. The two objects with id=2 are merged together. The keys from arr2 override the values in arr1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
arr1 = [
|
||||
{"id": 1, "b": {"b": 94},"v": [4, 3], "y": 48}
|
||||
]
|
||||
arr2 = [
|
||||
{"id": 1, "b": {"c": 84}, "v": [1, 3]}
|
||||
]
|
||||
<strong>Output:</strong> [
|
||||
{"id": 1, "b": {"c": 84}, "v": [1, 3], "y": 48}
|
||||
]
|
||||
<strong>Explanation:</strong> The two objects with id=1 are merged together. For the keys "b" and "v" the values from arr2 are used. Since the key "y" only exists in arr1, that value is taken form arr1.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>arr1 and arr2 are valid JSON arrays</code></li>
|
||||
<li><code>Each object in arr1 and arr2 has a unique integer id key</code></li>
|
||||
<li><code>2 <= JSON.stringify(arr1).length <= 10<sup>6</sup></code></li>
|
||||
<li><code>2 <= JSON.stringify(arr2).length <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
Given two promises <code>promise1</code> and <code>promise2</code>, return a new promise. <code>promise1</code> and <code>promise2</code> will both resolve with a number. The returned promise should resolve with the sum of the two numbers.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
promise1 = new Promise(resolve => setTimeout(() => resolve(2), 20)),
|
||||
promise2 = new Promise(resolve => setTimeout(() => resolve(5), 60))
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> The two input promises resolve with the values of 2 and 5 respectively. The returned promise should resolve with a value of 2 + 5 = 7. The time the returned promise resolves is not judged for this problem.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
promise1 = new Promise(resolve => setTimeout(() => resolve(10), 50)),
|
||||
promise2 = new Promise(resolve => setTimeout(() => resolve(-12), 30))
|
||||
<strong>Output:</strong> -2
|
||||
<strong>Explanation:</strong> The two input promises resolve with the values of 10 and -12 respectively. The returned promise should resolve with a value of 10 + -12 = -2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>promise1 and promise2 are promises that resolve with a number</code></li>
|
||||
</ul>
|
@@ -0,0 +1,59 @@
|
||||
<p>Some robots are standing on an infinite number line with their initial coordinates given by a <strong>0-indexed</strong> integer array <code>nums</code> and will start moving once given the command to move. The robots will move a unit distance each second.</p>
|
||||
|
||||
<p>You are given a string <code>s</code> denoting the direction in which robots will move on command. <code>'L'</code> means the robot will move towards the left side or negative side of the number line, whereas <code>'R'</code> means the robot will move towards the right side or positive side of the number line.</p>
|
||||
|
||||
<p>If two robots collide, they will start moving in opposite directions.</p>
|
||||
|
||||
<p>Return <em>the sum of distances between all the pairs of robots </em><code>d</code> <em>seconds after the command. </em>Since the sum can be very large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p><b>Note: </b></p>
|
||||
|
||||
<ul>
|
||||
<li>For two robots at the index <code>i</code> and <code>j</code>, pair <code>(i,j)</code> and pair <code>(j,i)</code> are considered the same pair.</li>
|
||||
<li>When robots collide, they <strong>instantly change</strong> their directions without wasting any time.</li>
|
||||
<li>Collision happens when two robots share the same place in a moment.
|
||||
<ul>
|
||||
<li>For example, if a robot is positioned in 0 going to the right and another is positioned in 2 going to the left, the next second they'll be both in 1 and they will change direction and the next second the first one will be in 0, heading left, and another will be in 2, heading right.</li>
|
||||
<li>For example, if a robot is positioned in 0 going to the right and another is positioned in 1 going to the left, the next second the first one will be in 0, heading left, and another will be in 1, heading right.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-2,0,2], s = "RLL", d = 3
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
After 1 second, the positions are [-1,-1,1]. Now, the robot at index 0 will move left, and the robot at index 1 will move right.
|
||||
After 2 seconds, the positions are [-2,0,0]. Now, the robot at index 1 will move left, and the robot at index 2 will move right.
|
||||
After 3 seconds, the positions are [-3,-1,1].
|
||||
The distance between the robot at index 0 and 1 is abs(-3 - (-1)) = 2.
|
||||
The distance between the robot at index 0 and 2 is abs(-3 - 1) = 4.
|
||||
The distance between the robot at index 0 and 1 is abs(-1 - 1) = 2.
|
||||
The sum of the pairs of all distances = 2 + 4 + 2 = 8.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,0], s = "RL", d = 2
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
After 1 second, the positions are [2,-1].
|
||||
After 2 seconds, the positions are [3,-2].
|
||||
The distance between the two robots is abs(-2 - 3) = 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-2 * 10<sup>9</sup> <= nums[i] <= 2 * 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= d <= 10<sup>9</sup></code></li>
|
||||
<li><code>nums.length == s.length </code></li>
|
||||
<li><code>s</code> consists of 'L' and 'R' only</li>
|
||||
<li><code>nums[i]</code> will be unique.</li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>You are given two numeric strings <code>num1</code> and <code>num2</code> and two integers <code>max_sum</code> and <code>min_sum</code>. We denote an integer <code>x</code> to be <em>good</em> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>num1 <= x <= num2</code></li>
|
||||
<li><code>min_sum <= digit_sum(x) <= max_sum</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of good integers</em>. Since the answer may be large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>Note that <code>digit_sum(x)</code> denotes the sum of the digits of <code>x</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = "1", num2 = "12", <code>min_sum</code> = 1, max_sum = 8
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> There are 11 integers whose sum of digits lies between 1 and 8 are 1,2,3,4,5,6,7,8,10,11, and 12. Thus, we return 11.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = "1", num2 = "5", <code>min_sum</code> = 1, max_sum = 5
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The 5 integers whose sum of digits lies between 1 and 5 are 1,2,3,4, and 5. Thus, we return 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num1 <= num2 <= 10<sup>22</sup></code></li>
|
||||
<li><code>1 <= min_sum <= max_sum <= 400</code></li>
|
||||
</ul>
|
@@ -0,0 +1,80 @@
|
||||
Given a function <code>fn,</code> an array of arguments <code>args</code>, and an interval time <code>t</code>, return a cancel function <code>cancelFn</code>. The function <code>fn</code> should be called with <code>args</code> immediately and then called again every <code>t</code> milliseconds until <code>cancelFn</code> is called.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> fn = (x) => x * 2, args = [4], t = 20, cancelT = 110
|
||||
<strong>Output:</strong>
|
||||
[
|
||||
{"time": 0, "returned": 8},
|
||||
{"time": 20, "returned": 8},
|
||||
{"time": 40, "returned": 8},
|
||||
{"time": 60, "returned": 8},
|
||||
{"time": 80, "returned": 8},
|
||||
{"time": 100, "returned": 8}
|
||||
]
|
||||
<strong>Explanation:</strong> Every 20ms, fn(4) is called. Until t=110ms, then it is cancelled.
|
||||
|
||||
const cancel = cancellable(x => x * 2, [4], 20);
|
||||
setTimeout(cancel, 110);
|
||||
|
||||
1st fn call is at 0ms. fn(4) returns 8.
|
||||
2nd fn call is at 20ms. fn(4) returns 8.
|
||||
3rd fn call is at 40ms. fn(4) returns 8.
|
||||
4th fn call is at 60ms. fn(4) returns 8.
|
||||
5th fn call is at 80ms. fn(4) returns 8.
|
||||
6th fn call is at 100ms. fn(4) returns 8.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> fn = (x1, x2) => (x1 * x2), args = [2, 5], t = 25, cancelT = 140
|
||||
<strong>Output:</strong>
|
||||
[
|
||||
{"time": 0, "returned": 10},
|
||||
{"time": 25, "returned": 10},
|
||||
{"time": 50, "returned": 10},
|
||||
{"time": 75, "returned": 10},
|
||||
{"time": 100, "returned": 10},
|
||||
{"time": 125, "returned": 10}
|
||||
]
|
||||
<strong>Explanation:</strong> Every 25ms, fn(2, 5) is called. Until t=140ms, then it is cancelled.
|
||||
1st fn call is at 0ms
|
||||
2nd fn call is at 25ms
|
||||
3rd fn call is at 50ms
|
||||
4th fn call is at 75ms
|
||||
5th fn call is at 100ms
|
||||
6th fn call is at 125ms
|
||||
Cancelled at 140ms
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> fn = (x1, x2, x3) => (x1 + x2 + x3), args = [5, 1, 3], t = 50, cancelT = 180
|
||||
<strong>Output:</strong>
|
||||
[
|
||||
{"time": 0, "returned": 9},
|
||||
{"time": 50, "returned": 9},
|
||||
{"time": 100, "returned": 9},
|
||||
{"time": 150, "returned": 9}
|
||||
]
|
||||
<strong>Explanation:</strong> Every 50ms, fn(5, 1, 3) is called. Until t=180ms, then it is cancelled.
|
||||
1st fn call is at 0ms
|
||||
2nd fn call is at 50ms
|
||||
3rd fn call is at 100ms
|
||||
4th fn call is at 150ms
|
||||
Cancelled at 180ms
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>fn is a function</code></li>
|
||||
<li><code>args is a valid JSON array</code></li>
|
||||
<li><code>1 <= args.length <= 10</code></li>
|
||||
<li><code><font face="monospace">20 <= t <= 1000</font></code></li>
|
||||
<li><code><font face="monospace">10 <= cancelT <= 1000</font></code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user