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,46 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A subarray of <code>nums</code> is called <strong>continuous</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li>Let <code>i</code>, <code>i + 1</code>, ..., <code>j</code><sub> </sub>be the indices in the subarray. Then, for each pair of indices <code>i <= i<sub>1</sub>, i<sub>2</sub> <= j</code>, <code><font face="monospace">0 <=</font> |nums[i<sub>1</sub>] - nums[i<sub>2</sub>]| <= 2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the total number of <strong>continuous</strong> subarrays.</em></p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,4,2,4]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
Continuous subarray of size 1: [5], [4], [2], [4].
|
||||
Continuous subarray of size 2: [5,4], [4,2], [2,4].
|
||||
Continuous subarray of size 3: [4,2,4].
|
||||
Thereare no subarrys of size 4.
|
||||
Total continuous subarrays = 4 + 3 + 1 = 8.
|
||||
It can be shown that there are no more continuous subarrays.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
Continuous subarray of size 1: [1], [2], [3].
|
||||
Continuous subarray of size 2: [1,2], [2,3].
|
||||
Continuous subarray of size 3: [1,2,3].
|
||||
Total continuous subarrays = 3 + 2 + 1 = 6.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>You are given an integer <code>n</code>. We say that two integers <code>x</code> and <code>y</code> form a prime number pair if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= x <= y <= n</code></li>
|
||||
<li><code>x + y == n</code></li>
|
||||
<li><code>x</code> and <code>y</code> are prime numbers</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the 2D sorted list of prime number pairs</em> <code>[x<sub>i</sub>, y<sub>i</sub>]</code>. The list should be sorted in <strong>increasing</strong> order of <code>x<sub>i</sub></code>. If there are no prime number pairs at all, return <em>an empty array</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong> A prime number is a natural number greater than <code>1</code> with only two factors, itself and <code>1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 10
|
||||
<strong>Output:</strong> [[3,7],[5,5]]
|
||||
<strong>Explanation:</strong> In this example, there are two prime pairs that satisfy the criteria.
|
||||
These pairs are [3,7] and [5,5], and we return them in the sorted order as described in the problem statement.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> We can show that there is no prime number pair that gives a sum of 2, so we return an empty array.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,61 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>words</code> containing <code>n</code> strings.</p>
|
||||
|
||||
<p>Let's define a <strong>join</strong> operation <code>join(x, y)</code> between two strings <code>x</code> and <code>y</code> as concatenating them into <code>xy</code>. However, if the last character of <code>x</code> is equal to the first character of <code>y</code>, one of them is <strong>deleted</strong>.</p>
|
||||
|
||||
<p>For example <code>join("ab", "ba") = "aba"</code> and <code>join("ab", "cde") = "abcde"</code>.</p>
|
||||
|
||||
<p>You are to perform <code>n - 1</code> <strong>join</strong> operations. Let <code>str<sub>0</sub> = words[0]</code>. Starting from <code>i = 1</code> up to <code>i = n - 1</code>, for the <code>i<sup>th</sup></code> operation, you can do one of the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Make <code>str<sub>i</sub> = join(str<sub>i - 1</sub>, words[i])</code></li>
|
||||
<li>Make <code>str<sub>i</sub> = join(words[i], str<sub>i - 1</sub>)</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Your task is to <strong>minimize</strong> the length of <code>str<sub>n - 1</sub></code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the minimum possible length of</em> <code>str<sub>n - 1</sub></code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["aa","ab","bc"]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation: </strong>In this example, we can perform join operations in the following order to minimize the length of str<sub>2</sub>:
|
||||
str<sub>0</sub> = "aa"
|
||||
str<sub>1</sub> = join(str<sub>0</sub>, "ab") = "aab"
|
||||
str<sub>2</sub> = join(str<sub>1</sub>, "bc") = "aabc"
|
||||
It can be shown that the minimum possible length of str<sub>2</sub> is 4.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["ab","b"]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In this example, str<sub>0</sub> = "ab", there are two ways to get str<sub>1</sub>:
|
||||
join(str<sub>0</sub>, "b") = "ab" or join("b", str<sub>0</sub>) = "bab".
|
||||
The first string, "ab", has the minimum length. Hence, the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["aaa","c","aba"]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> In this example, we can perform join operations in the following order to minimize the length of str<sub>2</sub>:
|
||||
str<sub>0</sub> = "aaa"
|
||||
str<sub>1</sub> = join(str<sub>0</sub>, "c") = "aaac"
|
||||
str<sub>2</sub> = join("aba", str<sub>1</sub>) = "abaaac"
|
||||
It can be shown that the minimum possible length of str<sub>2</sub> is 6.
|
||||
</pre>
|
||||
|
||||
<div class="notranslate" style="all: initial;"> </div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 1000</code></li>
|
||||
<li><code>1 <= words[i].length <= 50</code></li>
|
||||
<li>Each character in <code>words[i]</code> is an English lowercase letter</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>You are given a binary array <code>nums</code>.</p>
|
||||
|
||||
<p>A subarray of an array is <strong>good</strong> if it contains <strong>exactly</strong> <strong>one</strong> element with the value <code>1</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the number of ways to split the array </em><code>nums</code><em> into <strong>good</strong> subarrays</em>. As the number may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1,0,0,1]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 ways to split nums into good subarrays:
|
||||
- [0,1] [0,0,1]
|
||||
- [0,1,0] [0,1]
|
||||
- [0,1,0,0] [1]
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1,0]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> There is 1 way to split nums into good subarrays:
|
||||
- [0,1,0]
|
||||
</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] <= 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>You are given two integers <code>num1</code> and <code>num2</code>.</p>
|
||||
|
||||
<p>In one operation, you can choose integer <code>i</code> in the range <code>[0, 60]</code> and subtract <code>2<sup>i</sup> + num2</code> from <code>num1</code>.</p>
|
||||
|
||||
<p>Return <em>the integer denoting the <strong>minimum</strong> number of operations needed to make</em> <code>num1</code> <em>equal to</em> <code>0</code>.</p>
|
||||
|
||||
<p>If it is impossible to make <code>num1</code> equal to <code>0</code>, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = 3, num2 = -2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can make 3 equal to 0 with the following operations:
|
||||
- We choose i = 2 and substract 2<sup>2</sup> + (-2) from 3, 3 - (4 + (-2)) = 1.
|
||||
- We choose i = 2 and substract 2<sup>2</sup> + (-2) from 1, 1 - (4 + (-2)) = -1.
|
||||
- We choose i = 0 and substract 2<sup>0</sup> + (-2) from -1, (-1) - (1 + (-2)) = 0.
|
||||
It can be proven, that 3 is the minimum number of operations that we need to perform.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = 5, num2 = 7
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be proven, that it is impossible to make 5 equal to 0 with the given operation.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num1 <= 10<sup>9</sup></code></li>
|
||||
<li><code><font face="monospace">-10<sup>9</sup> <= num2 <= 10<sup>9</sup></font></code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>The <strong>imbalance number</strong> of a <strong>0-indexed</strong> integer array <code>arr</code> of length <code>n</code> is defined as the number of indices in <code>sarr = sorted(arr)</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < n - 1</code>, and</li>
|
||||
<li><code>sarr[i+1] - sarr[i] > 1</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Here, <code>sorted(arr)</code> is the function that returns the sorted version of <code>arr</code>.</p>
|
||||
|
||||
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code>, return <em>the <strong>sum of imbalance numbers</strong> of all its <strong>subarrays</strong></em>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,1,4]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 subarrays with non-zero<strong> </strong>imbalance numbers:
|
||||
- Subarray [3, 1] with an imbalance number of 1.
|
||||
- Subarray [3, 1, 4] with an imbalance number of 1.
|
||||
- Subarray [1, 4] with an imbalance number of 1.
|
||||
The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,3,3,5]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> There are 7 subarrays with non-zero imbalance numbers:
|
||||
- Subarray [1, 3] with an imbalance number of 1.
|
||||
- Subarray [1, 3, 3] with an imbalance number of 1.
|
||||
- Subarray [1, 3, 3, 3] with an imbalance number of 1.
|
||||
- Subarray [1, 3, 3, 3, 5] with an imbalance number of 2.
|
||||
- Subarray [3, 3, 3, 5] with an imbalance number of 1.
|
||||
- Subarray [3, 3, 5] with an imbalance number of 1.
|
||||
- Subarray [3, 5] with an imbalance number of 1.
|
||||
The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 8. </pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= nums.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>words</code> consisting of <strong>distinct</strong> strings.</p>
|
||||
|
||||
<p>The string <code>words[i]</code> can be paired with the string <code>words[j]</code> if:</p>
|
||||
|
||||
<ul>
|
||||
<li>The string <code>words[i]</code> is equal to the reversed string of <code>words[j]</code>.</li>
|
||||
<li><code>0 <= i < j < words.length</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of pairs that can be formed from the array </em><code>words</code><em>.</em></p>
|
||||
|
||||
<p>Note that each string can belong in <strong>at most one</strong> pair.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["cd","ac","dc","ca","zz"]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In this example, we can form 2 pair of strings in the following way:
|
||||
- We pair the 0<sup>th</sup> string with the 2<sup>nd</sup> string, as the reversed string of word[0] is "dc" and is equal to words[2].
|
||||
- We pair the 1<sup>st</sup> string with the 3<sup>rd</sup> string, as the reversed string of word[1] is "ca" and is equal to words[3].
|
||||
It can be proven that 2 is the maximum number of pairs that can be formed.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["ab","ba","cc"]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> In this example, we can form 1 pair of strings in the following way:
|
||||
- We pair the 0<sup>th</sup> string with the 1<sup>st</sup> string, as the reversed string of words[1] is "ab" and is equal to words[0].
|
||||
It can be proven that 1 is the maximum number of pairs that can be formed.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["aa","ab"]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> In this example, we are unable to form any pair of strings.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 50</code></li>
|
||||
<li><code>words[i].length == 2</code></li>
|
||||
<li><code>words</code> consists of distinct strings.</li>
|
||||
<li><code>words[i]</code> contains only lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>threshold</code>.</p>
|
||||
|
||||
<p>Find the length of the <strong>longest subarray</strong> of <code>nums</code> starting at index <code>l</code> and ending at index <code>r</code> <code>(0 <= l <= r < nums.length)</code> that satisfies the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums[l] % 2 == 0</code></li>
|
||||
<li>For all indices <code>i</code> in the range <code>[l, r - 1]</code>, <code>nums[i] % 2 != nums[i + 1] % 2</code></li>
|
||||
<li>For all indices <code>i</code> in the range <code>[l, r]</code>, <code>nums[i] <= threshold</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting the length of the longest such subarray.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,5,4], threshold = 5
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> In this example, we can select the subarray that starts at l = 1 and ends at r = 3 => [2,5,4]. This subarray satisfies the conditions.
|
||||
Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2], threshold = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2].
|
||||
It satisfies all the conditions and we can show that 1 is the maximum possible achievable length.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,4,5], threshold = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4].
|
||||
It satisfies all the conditions.
|
||||
Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
|
||||
</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><code>1 <= threshold <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p>
|
||||
|
||||
<p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>'L'</strong> for <strong>left</strong> or <strong>'R'</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p>
|
||||
|
||||
<p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p>
|
||||
|
||||
<p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p>
|
||||
|
||||
<p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final heath of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p>
|
||||
|
||||
<p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> The positions may be unsorted.</p>
|
||||
|
||||
<div class="notranslate" style="all: initial;"> </div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = "RRRRR"
|
||||
<strong>Output:</strong> [2,17,9,15,10]
|
||||
<strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = "RLRL"
|
||||
<strong>Output:</strong> [14]
|
||||
<strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4's health is smaller, it gets removed, and robot 3's health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = "RLRL"
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
|
||||
<li>All values in <code>positions</code> are distinct</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>You are given three integers <code>x</code>, <code>y</code>, and <code>z</code>.</p>
|
||||
|
||||
<p>You have <code>x</code> strings equal to <code>"AA"</code>, <code>y</code> strings equal to <code>"BB"</code>, and <code>z</code> strings equal to <code>"AB"</code>. You want to choose some (possibly all or none) of these strings and concactenate them in some order to form a new string. This new string must not contain <code>"AAA"</code> or <code>"BBB"</code> as a substring.</p>
|
||||
|
||||
<p>Return <em>the maximum possible length of the new string</em>.</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> x = 2, y = 5, z = 1
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation: </strong>We can concactenate the strings "BB", "AA", "BB", "AA", "BB", and "AB" in that order. Then, our new string is "BBAABBAABBAB".
|
||||
That string has length 12, and we can show that it is impossible to construct a string of longer length.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> x = 3, y = 2, z = 2
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong> We can concactenate the strings "AB", "AB", "AA", "BB", "AA", "BB", and "AA" in that order. Then, our new string is "ABABAABBAABBAA".
|
||||
That string has length 14, and we can show that it is impossible to construct a string of longer length.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= x, y, z <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>You are given an integer <code>n</code> denoting the total number of servers and a <strong>2D</strong> <strong>0-indexed </strong>integer array <code>logs</code>, where <code>logs[i] = [server_id, time]</code> denotes that the server with id <code>server_id</code> received a request at time <code>time</code>.</p>
|
||||
|
||||
<p>You are also given an integer <code>x</code> and a <strong>0-indexed</strong> integer array <code>queries</code>.</p>
|
||||
|
||||
<p>Return <em>a <strong>0-indexed</strong> integer array</em> <code>arr</code> <em>of length</em> <code>queries.length</code> <em>where</em> <code>arr[i]</code> <em>represents the number of servers that <strong>did not receive</strong> any requests during the time interval</em> <code>[queries[i] - x, queries[i]]</code>.</p>
|
||||
|
||||
<p>Note that the time intervals are inclusive.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, logs = [[1,3],[2,6],[1,5]], x = 5, queries = [10,11]
|
||||
<strong>Output:</strong> [1,2]
|
||||
<strong>Explanation:</strong>
|
||||
For queries[0]: The servers with ids 1 and 2 get requests in the duration of [5, 10]. Hence, only server 3 gets zero requests.
|
||||
For queries[1]: Only the server with id 2 gets a request in duration of [6,11]. Hence, the servers with ids 1 and 3 are the only servers that do not receive any requests during that time period.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, logs = [[2,4],[2,1],[1,2],[3,1]], x = 2, queries = [3,4]
|
||||
<strong>Output:</strong> [0,1]
|
||||
<strong>Explanation:</strong>
|
||||
For queries[0]: All servers get at least one request in the duration of [1, 3].
|
||||
For queries[1]: Only server with id 3 gets no request in the duration [2,4].
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= logs.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code><font face="monospace">logs[i].length == 2</font></code></li>
|
||||
<li><code>1 <= logs[i][0] <= n</code></li>
|
||||
<li><code>1 <= logs[i][1] <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= x <= 10<sup>5</sup></code></li>
|
||||
<li><code>x < queries[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed </strong>integer array <code>nums</code>. A pair of indices <code>i</code>, <code>j</code> where <code>0 <= i < j < nums.length</code> is called beautiful if the <strong>first digit</strong> of <code>nums[i]</code> and the <strong>last digit</strong> of <code>nums[j]</code> are <strong>coprime</strong>.</p>
|
||||
|
||||
<p>Return <em>the total number of beautiful pairs in </em><code>nums</code>.</p>
|
||||
|
||||
<p>Two integers <code>x</code> and <code>y</code> are <strong>coprime</strong> if there is no integer greater than 1 that divides both of them. In other words, <code>x</code> and <code>y</code> are coprime if <code>gcd(x, y) == 1</code>, where <code>gcd(x, y)</code> is the <strong>greatest common divisor</strong> of <code>x</code> and <code>y</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,5,1,4]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> There are 5 beautiful pairs in nums:
|
||||
When i = 0 and j = 1: the first digit of nums[0] is 2, and the last digit of nums[1] is 5. We can confirm that 2 and 5 are coprime, since gcd(2,5) == 1.
|
||||
When i = 0 and j = 2: the first digit of nums[0] is 2, and the last digit of nums[2] is 1. Indeed, gcd(2,1) == 1.
|
||||
When i = 1 and j = 2: the first digit of nums[1] is 5, and the last digit of nums[2] is 1. Indeed, gcd(5,1) == 1.
|
||||
When i = 1 and j = 3: the first digit of nums[1] is 5, and the last digit of nums[3] is 4. Indeed, gcd(5,4) == 1.
|
||||
When i = 2 and j = 3: the first digit of nums[2] is 1, and the last digit of nums[3] is 4. Indeed, gcd(1,4) == 1.
|
||||
Thus, we return 5.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [11,21,12]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are 2 beautiful pairs:
|
||||
When i = 0 and j = 1: the first digit of nums[0] is 1, and the last digit of nums[1] is 1. Indeed, gcd(1,1) == 1.
|
||||
When i = 0 and j = 2: the first digit of nums[0] is 1, and the last digit of nums[2] is 2. Indeed, gcd(1,2) == 1.
|
||||
Thus, we return 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 9999</code></li>
|
||||
<li><code>nums[i] % 10 != 0</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user