mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-12 19:01:47 +08:00
add leetcode problem-cn part4
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<p>Table: <code>Logins</code></p>
|
||||
|
||||
<pre>
|
||||
+----------------+----------+
|
||||
| Column Name | Type |
|
||||
+----------------+----------+
|
||||
| user_id | int |
|
||||
| time_stamp | datetime |
|
||||
+----------------+----------+
|
||||
(user_id, time_stamp) is the primary key for this table.
|
||||
Each row contains information about the login time for the user with ID user_id.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write an SQL query to report the <strong>latest</strong> login for all users in the year <code>2020</code>. Do <strong>not</strong> include the users who did not login in <code>2020</code>.</p>
|
||||
|
||||
<p>Return the result table <strong>in any order</strong>.</p>
|
||||
|
||||
<p>The query result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Logins table:
|
||||
+---------+---------------------+
|
||||
| user_id | time_stamp |
|
||||
+---------+---------------------+
|
||||
| 6 | 2020-06-30 15:06:07 |
|
||||
| 6 | 2021-04-21 14:06:06 |
|
||||
| 6 | 2019-03-07 00:18:15 |
|
||||
| 8 | 2020-02-01 05:10:53 |
|
||||
| 8 | 2020-12-30 00:46:50 |
|
||||
| 2 | 2020-01-16 02:49:50 |
|
||||
| 2 | 2019-08-25 07:59:08 |
|
||||
| 14 | 2019-07-14 09:00:00 |
|
||||
| 14 | 2021-01-06 11:59:59 |
|
||||
+---------+---------------------+
|
||||
<strong>Output:</strong>
|
||||
+---------+---------------------+
|
||||
| user_id | last_stamp |
|
||||
+---------+---------------------+
|
||||
| 6 | 2020-06-30 15:06:07 |
|
||||
| 8 | 2020-12-30 00:46:50 |
|
||||
| 2 | 2020-01-16 02:49:50 |
|
||||
+---------+---------------------+
|
||||
<strong>Explanation:</strong>
|
||||
User 6 logged into their account 3 times but only once in 2020, so we include this login in the result table.
|
||||
User 8 logged into their account 2 times in 2020, once in February and once in December. We include only the latest one (December) in the result table.
|
||||
User 2 logged into their account 2 times but only once in 2020, so we include this login in the result table.
|
||||
User 14 did not login in 2020, so we do not include them in the result table.
|
||||
</pre>
|
@@ -0,0 +1,34 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>In one operation, you can pick two numbers from the array whose sum equals <code>k</code> and remove them from the array.</p>
|
||||
|
||||
<p>Return <em>the maximum number of operations you can perform on the array</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4], k = 5
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
|
||||
- Remove numbers 1 and 4, then nums = [2,3]
|
||||
- Remove numbers 2 and 3, then nums = []
|
||||
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,1,3,4,3], k = 6
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
|
||||
- Remove the first two 3's, then nums = [1,4,3]
|
||||
There are no more pairs that sum up to 6, hence a total of 1 operation.</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>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>Given an integer <code>n</code> (in base <code>10</code>) and a base <code>k</code>, return <em>the <strong>sum</strong> of the digits of </em><code>n</code><em> <strong>after</strong> converting </em><code>n</code><em> from base </em><code>10</code><em> to base </em><code>k</code>.</p>
|
||||
|
||||
<p>After converting, each digit should be interpreted as a base <code>10</code> number, and the sum should be returned in base <code>10</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 34, k = 6
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation: </strong>34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 10, k = 10
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation: </strong>n is already in base 10. 1 + 0 = 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>2 <= k <= 10</code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p>
|
||||
|
||||
<p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose two elements, <code>x</code> and <code>y</code>.</li>
|
||||
<li>Receive a score of <code>i * gcd(x, y)</code>.</li>
|
||||
<li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p>
|
||||
|
||||
<p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The optimal choice of operations is:
|
||||
(1 * gcd(1, 2)) = 1
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,4,6,8]
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> The optimal choice of operations is:
|
||||
(1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5,6]
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong> The optimal choice of operations is:
|
||||
(1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 7</code></li>
|
||||
<li><code>nums.length == 2 * n</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given an undirected graph. You are given an integer <code>n</code> which is the number of nodes in the graph and an array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an undirected edge between <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.</p>
|
||||
|
||||
<p>A <strong>connected trio</strong> is a set of <strong>three</strong> nodes where there is an edge between <b>every</b> pair of them.</p>
|
||||
|
||||
<p>The <strong>degree of a connected trio</strong> is the number of edges where one endpoint is in the trio, and the other is not.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> degree of a connected trio in the graph, or</em> <code>-1</code> <em>if the graph has no connected trios.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/26/trios1.png" style="width: 388px; height: 164px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There is exactly one trio, which is [1,2,3]. The edges that form its degree are bolded in the figure above.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/26/trios2.png" style="width: 388px; height: 164px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7, edges = [[1,3],[4,1],[4,3],[2,5],[5,6],[6,7],[7,5],[2,6]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are exactly three trios:
|
||||
1) [1,4,3] with degree 0.
|
||||
2) [2,5,6] with degree 2.
|
||||
3) [5,6,7] with degree 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 400</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>1 <= edges.length <= n * (n-1) / 2</code></li>
|
||||
<li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li>
|
||||
<li><code>u<sub>i </sub>!= v<sub>i</sub></code></li>
|
||||
<li>There are no repeated edges.</li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>You are given two <strong>non-increasing 0-indexed </strong>integer arrays <code>nums1</code> and <code>nums2</code>.</p>
|
||||
|
||||
<p>A pair of indices <code>(i, j)</code>, where <code>0 <= i < nums1.length</code> and <code>0 <= j < nums2.length</code>, is <strong>valid</strong> if both <code>i <= j</code> and <code>nums1[i] <= nums2[j]</code>. The <strong>distance</strong> of the pair is <code>j - i</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum distance</strong> of any <strong>valid</strong> pair </em><code>(i, j)</code><em>. If there are no valid pairs, return </em><code>0</code>.</p>
|
||||
|
||||
<p>An array <code>arr</code> is <strong>non-increasing</strong> if <code>arr[i-1] >= arr[i]</code> for every <code>1 <= i < arr.length</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4).
|
||||
The maximum distance is 2 with pair (2,4).
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [2,2,2], nums2 = [10,10,1]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The valid pairs are (0,0), (0,1), and (1,1).
|
||||
The maximum distance is 1 with pair (0,1).
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [30,29,19,5], nums2 = [25,25,25,25,25]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4).
|
||||
The maximum distance is 2 with pair (2,4).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums1[i], nums2[j] <= 10<sup>5</sup></code></li>
|
||||
<li>Both <code>nums1</code> and <code>nums2</code> are <strong>non-increasing</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>You are given an array <code>nums</code> consisting of non-negative integers. You are also given a <code>queries</code> array, where <code>queries[i] = [x<sub>i</sub>, m<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>The answer to the <code>i<sup>th</sup></code> query is the maximum bitwise <code>XOR</code> value of <code>x<sub>i</sub></code> and any element of <code>nums</code> that does not exceed <code>m<sub>i</sub></code>. In other words, the answer is <code>max(nums[j] XOR x<sub>i</sub>)</code> for all <code>j</code> such that <code>nums[j] <= m<sub>i</sub></code>. If all elements in <code>nums</code> are larger than <code>m<sub>i</sub></code>, then the answer is <code>-1</code>.</p>
|
||||
|
||||
<p>Return <em>an integer array </em><code>answer</code><em> where </em><code>answer.length == queries.length</code><em> and </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>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]
|
||||
<strong>Output:</strong> [3,3,7]
|
||||
<strong>Explanation:</strong>
|
||||
1) 0 and 1 are the only two integers not greater than 1. 0 XOR 3 = 3 and 1 XOR 3 = 2. The larger of the two is 3.
|
||||
2) 1 XOR 2 = 3.
|
||||
3) 5 XOR 2 = 7.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,2,4,6,6,3], queries = [[12,4],[8,1],[6,3]]
|
||||
<strong>Output:</strong> [15,-1,5]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length, queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[i].length == 2</code></li>
|
||||
<li><code>0 <= nums[j], x<sub>i</sub>, m<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given an integer array <code>nums</code>, choose four <strong>distinct</strong> indices <code>w</code>, <code>x</code>, <code>y</code>, and <code>z</code> such that the <strong>product difference</strong> between pairs <code>(nums[w], nums[x])</code> and <code>(nums[y], nums[z])</code> is <strong>maximized</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> such product difference</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,6,2,7,4]
|
||||
<strong>Output:</strong> 34
|
||||
<strong>Explanation:</strong> We can choose indices 1 and 3 for the first pair (6, 7) and indices 2 and 4 for the second pair (2, 4).
|
||||
The product difference is (6 * 7) - (2 * 4) = 34.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,2,5,9,7,4,8]
|
||||
<strong>Output:</strong> 64
|
||||
<strong>Explanation:</strong> We can choose indices 3 and 6 for the first pair (9, 8) and indices 1 and 5 for the second pair (2, 4).
|
||||
The product difference is (9 * 8) - (2 * 4) = 64.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>4 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>The <strong>XOR sum</strong> of the two integer arrays is <code>(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])</code> (<strong>0-indexed</strong>).</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the <strong>XOR sum</strong> of <code>[1,2,3]</code> and <code>[3,2,1]</code> is equal to <code>(1 XOR 3) + (2 XOR 2) + (3 XOR 1) = 2 + 0 + 2 = 4</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Rearrange the elements of <code>nums2</code> such that the resulting <strong>XOR sum</strong> is <b>minimized</b>.</p>
|
||||
|
||||
<p>Return <em>the <strong>XOR sum</strong> after the rearrangement</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2], nums2 = [2,3]
|
||||
<strong>Output:</strong> 2
|
||||
<b>Explanation:</b> Rearrange <code>nums2</code> so that it becomes <code>[3,2]</code>.
|
||||
The XOR sum is (1 XOR 3) + (2 XOR 2) = 2 + 0 = 2.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,0,3], nums2 = [5,3,4]
|
||||
<strong>Output:</strong> 8
|
||||
<b>Explanation:</b> Rearrange <code>nums2</code> so that it becomes <code>[5,4,3]</code>.
|
||||
The XOR sum is (1 XOR 5) + (0 XOR 4) + (3 XOR 3) = 4 + 4 + 0 = 8.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length</code></li>
|
||||
<li><code>n == nums2.length</code></li>
|
||||
<li><code>1 <= n <= 14</code></li>
|
||||
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>Given a string <code>s</code>, return <em>the length of the longest substring between two equal characters, excluding the two characters.</em> If there is no such substring return <code>-1</code>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aa"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The optimal substring here is an empty substring between the two <code>'a's</code>.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abca"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The optimal substring here is "bc".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "cbzxy"
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> There are no characters that appear twice in s.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 300</code></li>
|
||||
<li><code>s</code> contains only lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>Given <code>n</code> <code>points</code> on a 2D plane where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>, Return<em> the <strong>widest vertical area</strong> between two points such that no points are inside the area.</em></p>
|
||||
|
||||
<p>A <strong>vertical area</strong> is an area of fixed-width extending infinitely along the y-axis (i.e., infinite height). The <strong>widest vertical area</strong> is the one with the maximum width.</p>
|
||||
|
||||
<p>Note that points <strong>on the edge</strong> of a vertical area <strong>are not</strong> considered included in the area.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/19/points3.png" style="width: 276px; height: 371px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[8,7],[9,9],[7,4],[9,7]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Both the red and the blue area are optimal.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[3,1],[9,0],[1,0],[1,4],[5,3],[8,8]]
|
||||
<strong>Output:</strong> 3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == points.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has <code>n</code> empty baskets, the <code>i<sup>th</sup></code> basket is at <code>position[i]</code>, Morty has <code>m</code> balls and needs to distribute the balls into the baskets such that the <strong>minimum magnetic force</strong> between any two balls is <strong>maximum</strong>.</p>
|
||||
|
||||
<p>Rick stated that magnetic force between two different balls at positions <code>x</code> and <code>y</code> is <code>|x - y|</code>.</p>
|
||||
|
||||
<p>Given the integer array <code>position</code> and the integer <code>m</code>. Return <em>the required force</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/11/q3v1.jpg" style="width: 562px; height: 195px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> position = [1,2,3,4,7], m = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> position = [5,4,3,2,1,1000000000], m = 2
|
||||
<strong>Output:</strong> 999999999
|
||||
<strong>Explanation:</strong> We can use baskets 1 and 1000000000.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == position.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= position[i] <= 10<sup>9</sup></code></li>
|
||||
<li>All integers in <code>position</code> are <strong>distinct</strong>.</li>
|
||||
<li><code>2 <= m <= position.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>Given an array of integers <code>nums</code>, find the maximum length of a subarray where the product of all its elements is positive.</p>
|
||||
|
||||
<p>A subarray of an array is a consecutive sequence of zero or more values taken out of that array.</p>
|
||||
|
||||
<p>Return <em>the maximum length of a subarray with positive product</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,-2,-3,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The array nums already has a positive product of 24.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1,-2,-3,-4]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The longest subarray with positive product is [1,-2,-3] which has a product of 6.
|
||||
Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive.</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,-2,-3,0,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The longest subarray with positive product is [-1,-2] or [-2,-3].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>Given a 2D array of characters <code>grid</code> of size <code>m x n</code>, you need to find if there exists any cycle consisting of the <strong>same value</strong> in <code>grid</code>.</p>
|
||||
|
||||
<p>A cycle is a path of <strong>length 4 or more</strong> in the grid that starts and ends at the same cell. From a given cell, you can move to one of the cells adjacent to it - in one of the four directions (up, down, left, or right), if it has the <strong>same value</strong> of the current cell.</p>
|
||||
|
||||
<p>Also, you cannot move to the cell that you visited in your last move. For example, the cycle <code>(1, 1) -> (1, 2) -> (1, 1)</code> is invalid because from <code>(1, 2)</code> we visited <code>(1, 1)</code> which was the last visited cell.</p>
|
||||
|
||||
<p>Return <code>true</code> if any cycle of the same value exists in <code>grid</code>, otherwise, return <code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/1.png" style="width: 231px; height: 152px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [["a","a","a","a"],["a","b","b","a"],["a","b","b","a"],["a","a","a","a"]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>There are two valid cycles shown in different colors in the image below:
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/11.png" style="width: 225px; height: 163px;" />
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/22.png" style="width: 236px; height: 154px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [["c","c","c","a"],["c","d","c","c"],["c","c","e","c"],["f","c","c","c"]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>There is only one valid cycle highlighted in the image below:
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/2.png" style="width: 229px; height: 157px;" />
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/3.png" style="width: 183px; height: 120px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [["a","b","b"],["b","z","b"],["b","b","a"]]
|
||||
<strong>Output:</strong> false
|
||||
</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 <= 500</code></li>
|
||||
<li><code>grid</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the number of special positions in </em><code>mat</code><em>.</em></p>
|
||||
|
||||
<p>A position <code>(i, j)</code> is called <strong>special</strong> if <code>mat[i][j] == 1</code> and all other elements in row <code>i</code> and column <code>j</code> are <code>0</code> (rows and columns are <strong>0-indexed</strong>).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/special1.jpg" style="width: 244px; height: 245px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[1,0,0],[0,0,1],[1,0,0]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> (1, 2) is a special position because mat[1][2] == 1 and all other elements in row 1 and column 2 are 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/special-grid.jpg" style="width: 244px; height: 245px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[1,0,0],[0,1,0],[0,0,1]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> (0, 0), (1, 1) and (2, 2) are special positions.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == mat.length</code></li>
|
||||
<li><code>n == mat[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 100</code></li>
|
||||
<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>There is a tree (i.e., a connected, undirected graph that has no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges. Each node has a value associated with it, and the <strong>root</strong> of the tree is node <code>0</code>.</p>
|
||||
|
||||
<p>To represent this tree, you are given an integer array <code>nums</code> and a 2D array <code>edges</code>. Each <code>nums[i]</code> represents the <code>i<sup>th</sup></code> node's value, and each <code>edges[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> represents an edge between nodes <code>u<sub>j</sub></code> and <code>v<sub>j</sub></code> in the tree.</p>
|
||||
|
||||
<p>Two values <code>x</code> and <code>y</code> are <strong>coprime</strong> 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>An ancestor of a node <code>i</code> is any other node on the shortest path from node <code>i</code> to the <strong>root</strong>. A node is <strong>not </strong>considered an ancestor of itself.</p>
|
||||
|
||||
<p>Return <em>an array </em><code>ans</code><em> of size </em><code>n</code>, <em>where </em><code>ans[i]</code><em> is the closest ancestor to node </em><code>i</code><em> such that </em><code>nums[i]</code> <em>and </em><code>nums[ans[i]]</code> are <strong>coprime</strong>, or <code>-1</code><em> if there is no such ancestor</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2021/01/06/untitled-diagram.png" style="width: 191px; height: 281px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,3,2], edges = [[0,1],[1,2],[1,3]]
|
||||
<strong>Output:</strong> [-1,0,0,1]
|
||||
<strong>Explanation:</strong> In the above figure, each node's value is in parentheses.
|
||||
- Node 0 has no coprime ancestors.
|
||||
- Node 1 has only one ancestor, node 0. Their values are coprime (gcd(2,3) == 1).
|
||||
- Node 2 has two ancestors, nodes 1 and 0. Node 1's value is not coprime (gcd(3,3) == 3), but node 0's
|
||||
value is (gcd(2,3) == 1), so node 0 is the closest valid ancestor.
|
||||
- Node 3 has two ancestors, nodes 1 and 0. It is coprime with node 1 (gcd(3,2) == 1), so node 1 is its
|
||||
closest valid ancestor.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/01/06/untitled-diagram1.png" style="width: 441px; height: 291px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,6,10,2,3,6,15], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]
|
||||
<strong>Output:</strong> [-1,0,-1,0,0,0,-1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums.length == n</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[j].length == 2</code></li>
|
||||
<li><code>0 <= u<sub>j</sub>, v<sub>j</sub> < n</code></li>
|
||||
<li><code>u<sub>j</sub> != v<sub>j</sub></code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>You are given two strings <code>word1</code> and <code>word2</code>. Merge the strings by adding letters in alternating order, starting with <code>word1</code>. If a string is longer than the other, append the additional letters onto the end of the merged string.</p>
|
||||
|
||||
<p>Return <em>the merged string.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word1 = "abc", word2 = "pqr"
|
||||
<strong>Output:</strong> "apbqcr"
|
||||
<strong>Explanation:</strong> The merged string will be merged as so:
|
||||
word1: a b c
|
||||
word2: p q r
|
||||
merged: a p b q c r
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word1 = "ab", word2 = "pqrs"
|
||||
<strong>Output:</strong> "apbqrs"
|
||||
<strong>Explanation:</strong> Notice that as word2 is longer, "rs" is appended to the end.
|
||||
word1: a b
|
||||
word2: p q r s
|
||||
merged: a p b q r s
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word1 = "abcd", word2 = "pq"
|
||||
<strong>Output:</strong> "apbqcd"
|
||||
<strong>Explanation:</strong> Notice that as word1 is longer, "cd" is appended to the end.
|
||||
word1: a b c d
|
||||
word2: p q
|
||||
merged: a p b q c d
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word1.length, word2.length <= 100</code></li>
|
||||
<li><code>word1</code> and <code>word2</code> consist of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>You are given a 2D integer array <code>logs</code> where each <code>logs[i] = [birth<sub>i</sub>, death<sub>i</sub>]</code> indicates the birth and death years of the <code>i<sup>th</sup></code> person.</p>
|
||||
|
||||
<p>The <strong>population</strong> of some year <code>x</code> is the number of people alive during that year. The <code>i<sup>th</sup></code> person is counted in year <code>x</code>'s population if <code>x</code> is in the <strong>inclusive</strong> range <code>[birth<sub>i</sub>, death<sub>i</sub> - 1]</code>. Note that the person is <strong>not</strong> counted in the year that they die.</p>
|
||||
|
||||
<p>Return <em>the <strong>earliest</strong> year with the <strong>maximum population</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> logs = [[1993,1999],[2000,2010]]
|
||||
<strong>Output:</strong> 1993
|
||||
<strong>Explanation:</strong> The maximum population is 1, and 1993 is the earliest year with this population.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> logs = [[1950,1961],[1960,1971],[1970,1981]]
|
||||
<strong>Output:</strong> 1960
|
||||
<strong>Explanation:</strong>
|
||||
The maximum population is 2, and it had happened in years 1960 and 1970.
|
||||
The earlier year between them is 1960.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= logs.length <= 100</code></li>
|
||||
<li><code>1950 <= birth<sub>i</sub> < death<sub>i</sub> <= 2050</code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>You are given two strings <code>s1</code> and <code>s2</code> of equal length. A <strong>string swap</strong> is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if it is possible to make both strings equal by performing <strong>at most one string swap </strong>on <strong>exactly one</strong> of the strings. </em>Otherwise, return <code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "bank", s2 = "kanb"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> For example, swap the first character with the last character of s2 to make "bank".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "attack", s2 = "defend"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is impossible to make them equal with one string swap.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "kelb", s2 = "kelb"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The two strings are already equal, so no string swap operation is required.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s1.length, s2.length <= 100</code></li>
|
||||
<li><code>s1.length == s2.length</code></li>
|
||||
<li><code>s1</code> and <code>s2</code> consist of only lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,67 @@
|
||||
<p>You have the task of delivering some boxes from storage to their ports using only one ship. However, this ship has a <strong>limit</strong> on the <strong>number of boxes</strong> and the <strong>total weight</strong> that it can carry.</p>
|
||||
|
||||
<p>You are given an array <code>boxes</code>, where <code>boxes[i] = [ports<sub>i</sub>, weight<sub>i</sub>]</code>, and three integers <code>portsCount</code>, <code>maxBoxes</code>, and <code>maxWeight</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li><code>ports<sub>i</sub></code> is the port where you need to deliver the <code>i<sup>th</sup></code> box and <code>weights<sub>i</sub></code> is the weight of the <code>i<sup>th</sup></code> box.</li>
|
||||
<li><code>portsCount</code> is the number of ports.</li>
|
||||
<li><code>maxBoxes</code> and <code>maxWeight</code> are the respective box and weight limits of the ship.</li>
|
||||
</ul>
|
||||
|
||||
<p>The boxes need to be delivered <strong>in the order they are given</strong>. The ship will follow these steps:</p>
|
||||
|
||||
<ul>
|
||||
<li>The ship will take some number of boxes from the <code>boxes</code> queue, not violating the <code>maxBoxes</code> and <code>maxWeight</code> constraints.</li>
|
||||
<li>For each loaded box <strong>in order</strong>, the ship will make a <strong>trip</strong> to the port the box needs to be delivered to and deliver it. If the ship is already at the correct port, no <strong>trip</strong> is needed, and the box can immediately be delivered.</li>
|
||||
<li>The ship then makes a return <strong>trip</strong> to storage to take more boxes from the queue.</li>
|
||||
</ul>
|
||||
|
||||
<p>The ship must end at storage after all the boxes have been delivered.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of <strong>trips</strong> the ship needs to make to deliver all boxes to their respective ports.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> boxes = [[1,1],[2,1],[1,1]], portsCount = 2, maxBoxes = 3, maxWeight = 3
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The optimal strategy is as follows:
|
||||
- The ship takes all the boxes in the queue, goes to port 1, then port 2, then port 1 again, then returns to storage. 4 trips.
|
||||
So the total number of trips is 4.
|
||||
Note that the first and third boxes cannot be delivered together because the boxes need to be delivered in order (i.e. the second box needs to be delivered at port 2 before the third box).
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> boxes = [[1,2],[3,3],[3,1],[3,1],[2,4]], portsCount = 3, maxBoxes = 3, maxWeight = 6
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The optimal strategy is as follows:
|
||||
- The ship takes the first box, goes to port 1, then returns to storage. 2 trips.
|
||||
- The ship takes the second, third and fourth boxes, goes to port 3, then returns to storage. 2 trips.
|
||||
- The ship takes the fifth box, goes to port 3, then returns to storage. 2 trips.
|
||||
So the total number of trips is 2 + 2 + 2 = 6.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> boxes = [[1,4],[1,2],[2,1],[2,1],[3,2],[3,4]], portsCount = 3, maxBoxes = 6, maxWeight = 7
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The optimal strategy is as follows:
|
||||
- The ship takes the first and second boxes, goes to port 1, then returns to storage. 2 trips.
|
||||
- The ship takes the third and fourth boxes, goes to port 2, then returns to storage. 2 trips.
|
||||
- The ship takes the fifth and sixth boxes, goes to port 3, then returns to storage. 2 trips.
|
||||
So the total number of trips is 2 + 2 + 2 = 6.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= boxes.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= portsCount, maxBoxes, maxWeight <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= ports<sub>i</sub> <= portsCount</code></li>
|
||||
<li><code>1 <= weights<sub>i</sub> <= maxWeight</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
|
||||
|
||||
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where each <code>adjacentPairs[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that the elements <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> are adjacent in <code>nums</code>.</p>
|
||||
|
||||
<p>It is guaranteed that every adjacent pair of elements <code>nums[i]</code> and <code>nums[i+1]</code> will exist in <code>adjacentPairs</code>, either as <code>[nums[i], nums[i+1]]</code> or <code>[nums[i+1], nums[i]]</code>. The pairs can appear <strong>in any order</strong>.</p>
|
||||
|
||||
<p>Return <em>the original array </em><code>nums</code><em>. If there are multiple solutions, return <strong>any of them</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> adjacentPairs = [[2,1],[3,4],[3,2]]
|
||||
<strong>Output:</strong> [1,2,3,4]
|
||||
<strong>Explanation:</strong> This array has all its adjacent pairs in adjacentPairs.
|
||||
Notice that adjacentPairs[i] may not be in left-to-right order.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> adjacentPairs = [[4,-2],[1,4],[-3,1]]
|
||||
<strong>Output:</strong> [-2,4,1,-3]
|
||||
<strong>Explanation:</strong> There can be negative numbers.
|
||||
Another solution is [-3,1,4,-2], which would also be accepted.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> adjacentPairs = [[100000,-100000]]
|
||||
<strong>Output:</strong> [100000,-100000]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums.length == n</code></li>
|
||||
<li><code>adjacentPairs.length == n - 1</code></li>
|
||||
<li><code>adjacentPairs[i].length == 2</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= nums[i], u<sub>i</sub>, v<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
<li>There exists some <code>nums</code> that has <code>adjacentPairs</code> as its pairs.</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>There is an undirected weighted connected graph. You are given a positive integer <code>n</code> which denotes that the graph has <code>n</code> nodes labeled from <code>1</code> to <code>n</code>, and an array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, weight<sub>i</sub>]</code> denotes that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> with weight equal to <code>weight<sub>i</sub></code>.</p>
|
||||
|
||||
<p>A path from node <code>start</code> to node <code>end</code> is a sequence of nodes <code>[z<sub>0</sub>, z<sub>1</sub>,<sub> </sub>z<sub>2</sub>, ..., z<sub>k</sub>]</code> such that <code>z<sub>0 </sub>= start</code> and <code>z<sub>k</sub> = end</code> and there is an edge between <code>z<sub>i</sub></code> and <code>z<sub>i+1</sub></code> where <code>0 <= i <= k-1</code>.</p>
|
||||
|
||||
<p>The distance of a path is the sum of the weights on the edges of the path. Let <code>distanceToLastNode(x)</code> denote the shortest distance of a path between node <code>n</code> and node <code>x</code>. A <strong>restricted path</strong> is a path that also satisfies that <code>distanceToLastNode(z<sub>i</sub>) > distanceToLastNode(z<sub>i+1</sub>)</code> where <code>0 <= i <= k-1</code>.</p>
|
||||
|
||||
<p>Return <em>the number of restricted paths from node</em> <code>1</code> <em>to node</em> <code>n</code>. Since that number may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/17/restricted_paths_ex1.png" style="width: 351px; height: 341px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, edges = [[1,2,3],[1,3,3],[2,3,1],[1,4,2],[5,2,2],[3,5,1],[5,4,10]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Each circle contains the node number in black and its <code>distanceToLastNode value in blue. </code>The three restricted paths are:
|
||||
1) 1 --> 2 --> 5
|
||||
2) 1 --> 2 --> 3 --> 5
|
||||
3) 1 --> 3 --> 5
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/17/restricted_paths_ex22.png" style="width: 356px; height: 401px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7, edges = [[1,3,1],[4,1,2],[7,3,4],[2,5,3],[5,6,1],[6,7,2],[7,5,3],[2,6,4]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Each circle contains the node number in black and its <code>distanceToLastNode value in blue. </code>The only restricted path is 1 --> 3 --> 7.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>n - 1 <= edges.length <= 4 * 10<sup>4</sup></code></li>
|
||||
<li><code>edges[i].length == 3</code></li>
|
||||
<li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li>
|
||||
<li><code>u<sub>i </sub>!= v<sub>i</sub></code></li>
|
||||
<li><code>1 <= weight<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
<li>There is at most one edge between any two nodes.</li>
|
||||
<li>There is at least one path between any two nodes.</li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>You are given an integer array <code>nums</code>. The <strong>absolute sum</strong> of a subarray <code>[nums<sub>l</sub>, nums<sub>l+1</sub>, ..., nums<sub>r-1</sub>, nums<sub>r</sub>]</code> is <code>abs(nums<sub>l</sub> + nums<sub>l+1</sub> + ... + nums<sub>r-1</sub> + nums<sub>r</sub>)</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> absolute sum of any <strong>(possibly empty)</strong> subarray of </em><code>nums</code>.</p>
|
||||
|
||||
<p>Note that <code>abs(x)</code> is defined as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>x</code> is a negative integer, then <code>abs(x) = -x</code>.</li>
|
||||
<li>If <code>x</code> is a non-negative integer, then <code>abs(x) = x</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,-3,2,3,-4]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The subarray [2,3] has absolute sum = abs(2+3) = abs(5) = 5.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,-5,1,-4,3,-2]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> The subarray [-5,1,-4] has absolute sum = abs(-5+1-4) = abs(-8) = 8.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>There are <code>3n</code> piles of coins of varying size, you and your friends will take piles of coins as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>In each step, you will choose <strong>any </strong><code>3</code> piles of coins (not necessarily consecutive).</li>
|
||||
<li>Of your choice, Alice will pick the pile with the maximum number of coins.</li>
|
||||
<li>You will pick the next pile with the maximum number of coins.</li>
|
||||
<li>Your friend Bob will pick the last pile.</li>
|
||||
<li>Repeat until there are no more piles of coins.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given an array of integers <code>piles</code> where <code>piles[i]</code> is the number of coins in the <code>i<sup>th</sup></code> pile.</p>
|
||||
|
||||
<p>Return the maximum number of coins that you can have.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> piles = [2,4,1,2,7,8]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation: </strong>Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with <strong>7</strong> coins and Bob the last one.
|
||||
Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with <strong>2</strong> coins and Bob the last one.
|
||||
The maximum number of coins which you can have are: 7 + 2 = 9.
|
||||
On the other hand if we choose this arrangement (1, <strong>2</strong>, 8), (2, <strong>4</strong>, 7) you only get 2 + 4 = 6 coins which is not optimal.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> piles = [2,4,5]
|
||||
<strong>Output:</strong> 4
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> piles = [9,8,7,6,5,1,2,3,4]
|
||||
<strong>Output:</strong> 18
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= piles.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>piles.length % 3 == 0</code></li>
|
||||
<li><code>1 <= piles[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are participating in an online chess tournament. There is a chess round that starts every <code>15</code> minutes. The first round of the day starts at <code>00:00</code>, and after every <code>15</code> minutes, a new round starts.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the second round starts at <code>00:15</code>, the fourth round starts at <code>00:45</code>, and the seventh round starts at <code>01:30</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are given two strings <code>loginTime</code> and <code>logoutTime</code> where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>loginTime</code> is the time you will login to the game, and</li>
|
||||
<li><code>logoutTime</code> is the time you will logout from the game.</li>
|
||||
</ul>
|
||||
|
||||
<p>If <code>logoutTime</code> is <strong>earlier</strong> than <code>loginTime</code>, this means you have played from <code>loginTime</code> to midnight and from midnight to <code>logoutTime</code>.</p>
|
||||
|
||||
<p>Return <em>the number of full chess rounds you have played in the tournament</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong> All the given times follow the 24-hour clock. That means the first round of the day starts at <code>00:00</code> and the last round of the day starts at <code>23:45</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> loginTime = "09:31", logoutTime = "10:14"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> You played one full round from 09:45 to 10:00.
|
||||
You did not play the full round from 09:30 to 09:45 because you logged in at 09:31 after it began.
|
||||
You did not play the full round from 10:00 to 10:15 because you logged out at 10:14 before it ended.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> loginTime = "21:30", logoutTime = "03:00"
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong> You played 10 full rounds from 21:30 to 00:00 and 12 full rounds from 00:00 to 03:00.
|
||||
10 + 12 = 22.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>loginTime</code> and <code>logoutTime</code> are in the format <code>hh:mm</code>.</li>
|
||||
<li><code>00 <= hh <= 23</code></li>
|
||||
<li><code>00 <= mm <= 59</code></li>
|
||||
<li><code>loginTime</code> and <code>logoutTime</code> are not equal.</li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>(0-indexed)</strong> array of positive integers <code>candiesCount</code> where <code>candiesCount[i]</code> represents the number of candies of the <code>i<sup>th</sup></code> type you have. You are also given a 2D array <code>queries</code> where <code>queries[i] = [favoriteType<sub>i</sub>, favoriteDay<sub>i</sub>, dailyCap<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>You play a game with the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>You start eating candies on day <code><strong>0</strong></code>.</li>
|
||||
<li>You <b>cannot</b> eat <strong>any</strong> candy of type <code>i</code> unless you have eaten <strong>all</strong> candies of type <code>i - 1</code>.</li>
|
||||
<li>You must eat <strong>at least</strong> <strong>one</strong> candy per day until you have eaten all the candies.</li>
|
||||
</ul>
|
||||
|
||||
<p>Construct a boolean array <code>answer</code> such that <code>answer.length == queries.length</code> and <code>answer[i]</code> is <code>true</code> if you can eat a candy of type <code>favoriteType<sub>i</sub></code> on day <code>favoriteDay<sub>i</sub></code> without eating <strong>more than</strong> <code>dailyCap<sub>i</sub></code> candies on <strong>any</strong> day, and <code>false</code> otherwise. Note that you can eat different types of candy on the same day, provided that you follow rule 2.</p>
|
||||
|
||||
<p>Return <em>the constructed array </em><code>answer</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candiesCount = [7,4,5,3,8], queries = [[0,2,2],[4,2,4],[2,13,1000000000]]
|
||||
<strong>Output:</strong> [true,false,true]
|
||||
<strong>Explanation:</strong>
|
||||
1- If you eat 2 candies (type 0) on day 0 and 2 candies (type 0) on day 1, you will eat a candy of type 0 on day 2.
|
||||
2- You can eat at most 4 candies each day.
|
||||
If you eat 4 candies every day, you will eat 4 candies (type 0) on day 0 and 4 candies (type 0 and type 1) on day 1.
|
||||
On day 2, you can only eat 4 candies (type 1 and type 2), so you cannot eat a candy of type 4 on day 2.
|
||||
3- If you eat 1 candy each day, you will eat a candy of type 2 on day 13.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candiesCount = [5,2,6,4,1], queries = [[3,1,2],[4,10,3],[3,10,100],[4,100,30],[1,3,1]]
|
||||
<strong>Output:</strong> [false,true,true,false,false]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= candiesCount.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= candiesCount[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[i].length == 3</code></li>
|
||||
<li><code>0 <= favoriteType<sub>i</sub> < candiesCount.length</code></li>
|
||||
<li><code>0 <= favoriteDay<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= dailyCap<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given an integer array <code>coins</code> of length <code>n</code> which represents the <code>n</code> coins that you own. The value of the <code>i<sup>th</sup></code> coin is <code>coins[i]</code>. You can <strong>make</strong> some value <code>x</code> if you can choose some of your <code>n</code> coins such that their values sum up to <code>x</code>.</p>
|
||||
|
||||
<p>Return the <em>maximum number of consecutive integer values that you <strong>can</strong> <strong>make</strong> with your coins <strong>starting</strong> from and <strong>including</strong> </em><code>0</code>.</p>
|
||||
|
||||
<p>Note that you may have multiple coins of the same value.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> coins = [1,3]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>You can make the following values:
|
||||
- 0: take []
|
||||
- 1: take [1]
|
||||
You can make 2 consecutive integer values starting from 0.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> coins = [1,1,1,4]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation: </strong>You can make the following values:
|
||||
- 0: take []
|
||||
- 1: take [1]
|
||||
- 2: take [1,1]
|
||||
- 3: take [1,1,1]
|
||||
- 4: take [4]
|
||||
- 5: take [4,1]
|
||||
- 6: take [4,1,1]
|
||||
- 7: take [4,1,1,1]
|
||||
You can make 8 consecutive integer values starting from 0.</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,10,3,1]
|
||||
<strong>Output:</strong> 20</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>coins.length == n</code></li>
|
||||
<li><code>1 <= n <= 4 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= coins[i] <= 4 * 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given a binary string <code>s</code>. You are allowed to perform two types of operations on the string in any sequence:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Type-1: Remove</strong> the character at the start of the string <code>s</code> and <strong>append</strong> it to the end of the string.</li>
|
||||
<li><strong>Type-2: Pick</strong> any character in <code>s</code> and <strong>flip</strong> its value, i.e., if its value is <code>'0'</code> it becomes <code>'1'</code> and vice-versa.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of <strong>type-2</strong> operations you need to perform</em> <em>such that </em><code>s</code> <em>becomes <strong>alternating</strong>.</em></p>
|
||||
|
||||
<p>The string is called <strong>alternating</strong> if no two adjacent characters are equal.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the strings <code>"010"</code> and <code>"1010"</code> are alternating, while the string <code>"0100"</code> is not.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "111000"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation</strong>: Use the first operation two times to make s = "100011".
|
||||
Then, use the second operation on the third and sixth elements to make s = "10<u>1</u>01<u>0</u>".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "010"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation</strong>: The string is already alternating.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "1110"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation</strong>: Use the second operation on the second element to make s = "1<u>0</u>10".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>.</p>
|
||||
|
||||
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aababbab"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> You can either:
|
||||
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
|
||||
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "bbaaaaabb"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The only solution is to delete the first two characters.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given a string <code>s</code> (<strong>0-indexed</strong>). You are asked to perform the following operation on <code>s</code> until you get a sorted string:</p>
|
||||
|
||||
<ol>
|
||||
<li>Find <strong>the largest index</strong> <code>i</code> such that <code>1 <= i < s.length</code> and <code>s[i] < s[i - 1]</code>.</li>
|
||||
<li>Find <strong>the largest index</strong> <code>j</code> such that <code>i <= j < s.length</code> and <code>s[k] < s[i - 1]</code> for all the possible values of <code>k</code> in the range <code>[i, j]</code> inclusive.</li>
|
||||
<li>Swap the two characters at indices <code>i - 1</code> and <code>j</code>.</li>
|
||||
<li>Reverse the suffix starting at index <code>i</code>.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>the number of operations needed to make the string sorted.</em> Since the answer can be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "cba"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The simulation goes as follows:
|
||||
Operation 1: i=2, j=2. Swap s[1] and s[2] to get s="cab", then reverse the suffix starting at 2. Now, s="cab".
|
||||
Operation 2: i=1, j=2. Swap s[0] and s[2] to get s="bac", then reverse the suffix starting at 1. Now, s="bca".
|
||||
Operation 3: i=2, j=2. Swap s[1] and s[2] to get s="bac", then reverse the suffix starting at 2. Now, s="bac".
|
||||
Operation 4: i=1, j=1. Swap s[0] and s[1] to get s="abc", then reverse the suffix starting at 1. Now, s="acb".
|
||||
Operation 5: i=2, j=2. Swap s[1] and s[2] to get s="abc", then reverse the suffix starting at 2. Now, s="abc".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aabaa"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The simulation goes as follows:
|
||||
Operation 1: i=3, j=4. Swap s[2] and s[4] to get s="aaaab", then reverse the substring starting at 3. Now, s="aaaba".
|
||||
Operation 2: i=4, j=4. Swap s[3] and s[4] to get s="aaaab", then reverse the substring starting at 4. Now, s="aaaab".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 3000</code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>You are given an array <code>nums</code> and an integer <code>k</code>. The <font face="monospace">XOR</font> of a segment <code>[left, right]</code> where <code>left <= right</code> is the <code>XOR</code> of all the elements with indices between <code>left</code> and <code>right</code>, inclusive: <code>nums[left] XOR nums[left+1] XOR ... XOR nums[right]</code>.</p>
|
||||
|
||||
<p>Return <em>the minimum number of elements to change in the array </em>such that the <code>XOR</code> of all segments of size <code>k</code> is equal to zero.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,0,3,0], k = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation: </strong>Modify the array from [<u><strong>1</strong></u>,<u><strong>2</strong></u>,0,<u><strong>3</strong></u>,0] to from [<u><strong>0</strong></u>,<u><strong>0</strong></u>,0,<u><strong>0</strong></u>,0].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,4,5,2,1,7,3,4,7], k = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation: </strong>Modify the array from [3,4,<strong><u>5</u></strong>,<strong><u>2</u></strong>,<strong><u>1</u></strong>,7,3,4,7] to [3,4,<strong><u>7</u></strong>,<strong><u>3</u></strong>,<strong><u>4</u></strong>,7,3,4,7].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,4,1,2,5,1,2,6], k = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation: </strong>Modify the array from [1,2,<strong><u>4,</u></strong>1,2,<strong><u>5</u></strong>,1,2,<strong><u>6</u></strong>] to [1,2,<strong><u>3</u></strong>,1,2,<strong><u>3</u></strong>,1,2,<strong><u>3</u></strong>].</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= nums.length <= 2000</code></li>
|
||||
<li><code>0 <= nums[i] < 2<sup>10</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>You have an array <code>arr</code> of length <code>n</code> where <code>arr[i] = (2 * i) + 1</code> for all valid values of <code>i</code> (i.e., <code>0 <= i < n</code>).</p>
|
||||
|
||||
<p>In one operation, you can select two indices <code>x</code> and <code>y</code> where <code>0 <= x, y < n</code> and subtract <code>1</code> from <code>arr[x]</code> and add <code>1</code> to <code>arr[y]</code> (i.e., perform <code>arr[x] -=1 </code>and <code>arr[y] += 1</code>). The goal is to make all the elements of the array <strong>equal</strong>. It is <strong>guaranteed</strong> that all the elements of the array can be made equal using some operations.</p>
|
||||
|
||||
<p>Given an integer <code>n</code>, the length of the array, return <em>the minimum number of operations</em> needed to make all the elements of arr equal.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> arr = [1, 3, 5]
|
||||
First operation choose x = 2 and y = 0, this leads arr to be [2, 3, 4]
|
||||
In the second operation choose x = 2 and y = 0 again, thus arr = [3, 3, 3].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6
|
||||
<strong>Output:</strong> 9
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>You are given an integer array <code>nums</code> of <strong>even</strong> length <code>n</code> and an integer <code>limit</code>. In one move, you can replace any integer from <code>nums</code> with another integer between <code>1</code> and <code>limit</code>, inclusive.</p>
|
||||
|
||||
<p>The array <code>nums</code> is <strong>complementary</strong> if for all indices <code>i</code> (<strong>0-indexed</strong>), <code>nums[i] + nums[n - 1 - i]</code> equals the same number. For example, the array <code>[1,2,3,4]</code> is complementary because for all indices <code>i</code>, <code>nums[i] + nums[n - 1 - i] = 5</code>.</p>
|
||||
|
||||
<p>Return the <em><strong>minimum</strong> number of moves required to make </em><code>nums</code><em> <strong>complementary</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,4,3], limit = 4
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> In 1 move, you can change nums to [1,2,<u>2</u>,3] (underlined elements are changed).
|
||||
nums[0] + nums[3] = 1 + 3 = 4.
|
||||
nums[1] + nums[2] = 2 + 2 = 4.
|
||||
nums[2] + nums[1] = 2 + 2 = 4.
|
||||
nums[3] + nums[0] = 3 + 1 = 4.
|
||||
Therefore, nums[i] + nums[n-1-i] = 4 for every i, so nums is complementary.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,2,1], limit = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In 2 moves, you can change nums to [<u>2</u>,2,2,<u>2</u>]. You cannot change any number to 3 since 3 > limit.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1,2], limit = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> nums is already complementary.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= limit <= 10<sup>5</sup></code></li>
|
||||
<li><code>n</code> is even.</li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>Given an integer array <code>nums</code>, your goal is to make all elements in <code>nums</code> equal. To complete one operation, follow these steps:</p>
|
||||
|
||||
<ol>
|
||||
<li>Find the <strong>largest</strong> value in <code>nums</code>. Let its index be <code>i</code> (<strong>0-indexed</strong>) and its value be <code>largest</code>. If there are multiple elements with the largest value, pick the smallest <code>i</code>.</li>
|
||||
<li>Find the <strong>next largest</strong> value in <code>nums</code> <strong>strictly smaller</strong> than <code>largest</code>. Let its value be <code>nextLargest</code>.</li>
|
||||
<li>Reduce <code>nums[i]</code> to <code>nextLargest</code>.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>the number of operations to make all elements in </em><code>nums</code><em> equal</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,1,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> It takes 3 operations to make all elements in nums equal:
|
||||
1. largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [<u>3</u>,1,3].
|
||||
2. largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [<u>1</u>,1,3].
|
||||
3. largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,<u>1</u>].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> All elements in nums are already equal.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,2,2,3]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> It takes 4 operations to make all elements in nums equal:
|
||||
1. largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,<u>2</u>].
|
||||
2. largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,<u>1</u>,2,2].
|
||||
3. largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,<u>1</u>,2].
|
||||
4. largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,<u>1</u>].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 5 * 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>Given an array of positive integers <code>nums</code>, remove the <strong>smallest</strong> subarray (possibly <strong>empty</strong>) such that the <strong>sum</strong> of the remaining elements is divisible by <code>p</code>. It is <strong>not</strong> allowed to remove the whole array.</p>
|
||||
|
||||
<p>Return <em>the length of the smallest subarray that you need to remove, or </em><code>-1</code><em> if it's impossible</em>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is defined as a contiguous block of elements in the array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,1,4,2], p = 6
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The sum of the elements in nums is 10, which is not divisible by 6. We can remove the subarray [4], and the sum of the remaining elements is 6, which is divisible by 6.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,3,5,2], p = 9
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We cannot remove a single element to get a sum divisible by 9. The best way is to remove the subarray [5,2], leaving us with [6,3] with sum 9.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3], p = 3
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Here the sum is 6. which is already divisible by 3. Thus we do not need to remove anything.
|
||||
</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>
|
||||
<li><code>1 <= p <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>Given an integer <code>n</code>, you must transform it into <code>0</code> using the following operations any number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Change the rightmost (<code>0<sup>th</sup></code>) bit in the binary representation of <code>n</code>.</li>
|
||||
<li>Change the <code>i<sup>th</sup></code> bit in the binary representation of <code>n</code> if the <code>(i-1)<sup>th</sup></code> bit is set to <code>1</code> and the <code>(i-2)<sup>th</sup></code> through <code>0<sup>th</sup></code> bits are set to <code>0</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum number of operations to transform </em><code>n</code><em> into </em><code>0</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The binary representation of 3 is "11".
|
||||
"<u>1</u>1" -> "<u>0</u>1" with the 2<sup>nd</sup> operation since the 0<sup>th</sup> bit is 1.
|
||||
"0<u>1</u>" -> "0<u>0</u>" with the 1<sup>st</sup> operation.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The binary representation of 6 is "110".
|
||||
"<u>1</u>10" -> "<u>0</u>10" with the 2<sup>nd</sup> operation since the 1<sup>st</sup> bit is 1 and 0<sup>th</sup> through 0<sup>th</sup> bits are 0.
|
||||
"01<u>0</u>" -> "01<u>1</u>" with the 1<sup>st</sup> operation.
|
||||
"0<u>1</u>1" -> "0<u>0</u>1" with the 2<sup>nd</sup> operation since the 0<sup>th</sup> bit is 1.
|
||||
"00<u>1</u>" -> "00<u>0</u>" with the 1<sup>st</sup> operation.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>servers</code> and <code>tasks</code> of lengths <code>n</code> and <code>m</code> respectively. <code>servers[i]</code> is the <strong>weight</strong> of the <code>i<sup>th</sup></code> server, and <code>tasks[j]</code> is the <strong>time needed</strong> to process the <code>j<sup>th</sup></code> task <strong>in seconds</strong>.</p>
|
||||
|
||||
<p>Tasks are assigned to the servers using a <strong>task queue</strong>. Initially, all servers are free, and the queue is <strong>empty</strong>.</p>
|
||||
|
||||
<p>At second <code>j</code>, the <code>j<sup>th</sup></code> task is <strong>inserted</strong> into the queue (starting with the <code>0<sup>th</sup></code> task being inserted at second <code>0</code>). As long as there are free servers and the queue is not empty, the task in the front of the queue will be assigned to a free server with the <strong>smallest weight</strong>, and in case of a tie, it is assigned to a free server with the <strong>smallest index</strong>.</p>
|
||||
|
||||
<p>If there are no free servers and the queue is not empty, we wait until a server becomes free and immediately assign the next task. If multiple servers become free at the same time, then multiple tasks from the queue will be assigned <strong>in order of insertion</strong> following the weight and index priorities above.</p>
|
||||
|
||||
<p>A server that is assigned task <code>j</code> at second <code>t</code> will be free again at second <code>t + tasks[j]</code>.</p>
|
||||
|
||||
<p>Build an array <code>ans</code> of length <code>m</code>, where <code>ans[j]</code> is the <strong>index</strong> of the server the <code>j<sup>th</sup></code> task will be assigned to.</p>
|
||||
|
||||
<p>Return <em>the array </em><code>ans</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> servers = [3,3,2], tasks = [1,2,3,2,1,2]
|
||||
<strong>Output:</strong> [2,2,0,2,1,2]
|
||||
<strong>Explanation: </strong>Events in chronological order go as follows:
|
||||
- At second 0, task 0 is added and processed using server 2 until second 1.
|
||||
- At second 1, server 2 becomes free. Task 1 is added and processed using server 2 until second 3.
|
||||
- At second 2, task 2 is added and processed using server 0 until second 5.
|
||||
- At second 3, server 2 becomes free. Task 3 is added and processed using server 2 until second 5.
|
||||
- At second 4, task 4 is added and processed using server 1 until second 5.
|
||||
- At second 5, all servers become free. Task 5 is added and processed using server 2 until second 7.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> servers = [5,1,4,3,2], tasks = [2,1,2,4,5,2,1]
|
||||
<strong>Output:</strong> [1,4,1,4,1,3,2]
|
||||
<strong>Explanation: </strong>Events in chronological order go as follows:
|
||||
- At second 0, task 0 is added and processed using server 1 until second 2.
|
||||
- At second 1, task 1 is added and processed using server 4 until second 2.
|
||||
- At second 2, servers 1 and 4 become free. Task 2 is added and processed using server 1 until second 4.
|
||||
- At second 3, task 3 is added and processed using server 4 until second 7.
|
||||
- At second 4, server 1 becomes free. Task 4 is added and processed using server 1 until second 9.
|
||||
- At second 5, task 5 is added and processed using server 3 until second 7.
|
||||
- At second 6, task 6 is added and processed using server 2 until second 7.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>servers.length == n</code></li>
|
||||
<li><code>tasks.length == m</code></li>
|
||||
<li><code>1 <= n, m <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= servers[i], tasks[j] <= 2 * 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>Alice has <code>n</code> balloons arranged on a rope. You are given a <strong>0-indexed</strong> string <code>colors</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> balloon.</p>
|
||||
|
||||
<p>Alice wants the rope to be <strong>colorful</strong>. She does not want <strong>two consecutive balloons</strong> to be of the same color, so she asks Bob for help. Bob can remove some balloons from the rope to make it <strong>colorful</strong>. You are given a <strong>0-indexed</strong> integer array <code>neededTime</code> where <code>neededTime[i]</code> is the time (in seconds) that Bob needs to remove the <code>i<sup>th</sup></code> balloon from the rope.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum time</strong> Bob needs to make the rope <strong>colorful</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/ballon1.jpg" style="width: 404px; height: 243px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> colors = "abaac", neededTime = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> In the above image, 'a' is blue, 'b' is red, and 'c' is green.
|
||||
Bob can remove the blue balloon at index 2. This takes 3 seconds.
|
||||
There are no longer two consecutive balloons of the same color. Total time = 3.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/balloon2.jpg" style="width: 244px; height: 243px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> colors = "abc", neededTime = [1,2,3]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The rope is already colorful. Bob does not need to remove any balloons from the rope.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/balloon3.jpg" style="width: 404px; height: 243px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> colors = "aabaa", neededTime = [1,2,3,4,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Bob will remove the ballons at indices 0 and 4. Each ballon takes 1 second to remove.
|
||||
There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == colors.length == neededTime.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= neededTime[i] <= 10<sup>4</sup></code></li>
|
||||
<li><code>colors</code> contains only lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>You are given an <code>m x n</code> binary grid <code>grid</code> where <code>1</code> represents land and <code>0</code> represents water. An <strong>island</strong> is a maximal <strong>4-directionally</strong> (horizontal or vertical) connected group of <code>1</code>'s.</p>
|
||||
|
||||
<p>The grid is said to be <strong>connected</strong> if we have <strong>exactly one island</strong>, otherwise is said <strong>disconnected</strong>.</p>
|
||||
|
||||
<p>In one day, we are allowed to change <strong>any </strong>single land cell <code>(1)</code> into a water cell <code>(0)</code>.</p>
|
||||
|
||||
<p>Return <em>the minimum number of days to disconnect the grid</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/land1.jpg" style="width: 500px; height: 169px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]]
|
||||
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We need at least 2 days to get a disconnected grid.
|
||||
Change land grid[1][1] and grid[0][2] to water and get 2 disconnected island.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/land2.jpg" style="width: 404px; height: 85px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Grid of full water is also disconnected ([[1,1]] -> [[0,0]]), 0 islands.
|
||||
</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 <= 30</code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>Alice and Bob have an undirected graph of <code>n</code> nodes and 3 types of edges:</p>
|
||||
|
||||
<ul>
|
||||
<li>Type 1: Can be traversed by Alice only.</li>
|
||||
<li>Type 2: Can be traversed by Bob only.</li>
|
||||
<li>Type 3: Can by traversed by both Alice and Bob.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given an array <code>edges</code> where <code>edges[i] = [type<sub>i</sub>, u<sub>i</sub>, v<sub>i</sub>]</code> represents a bidirectional edge of type <code>type<sub>i</sub></code> between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.</p>
|
||||
|
||||
<p>Return <em>the maximum number of edges you can remove, or return</em> <code>-1</code> <em>if it's impossible for the graph to be fully traversed by Alice and Bob.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/08/19/ex1.png" style="width: 179px; height: 191px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>If we remove the 2 edges [1,1,2] and [1,1,3]. The graph will still be fully traversable by Alice and Bob. Removing any additional edge will not make it so. So the maximum number of edges we can remove is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/08/19/ex2.png" style="width: 178px; height: 190px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation: </strong>Notice that removing any edge will not make the graph fully traversable by Alice and Bob.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/08/19/ex3.png" style="width: 178px; height: 190px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]
|
||||
<strong>Output:</strong> -1
|
||||
<b>Explanation: </b>In the current graph, Alice cannot reach node 4 from the other nodes. Likewise, Bob cannot reach 1. Therefore it's impossible to make the graph fully traversable.</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10^5</code></li>
|
||||
<li><code>1 <= edges.length <= min(10^5, 3 * n * (n-1) / 2)</code></li>
|
||||
<li><code>edges[i].length == 3</code></li>
|
||||
<li><code>1 <= edges[i][0] <= 3</code></li>
|
||||
<li><code>1 <= edges[i][1] < edges[i][2] <= n</code></li>
|
||||
<li>All tuples <code>(type<sub>i</sub>, u<sub>i</sub>, v<sub>i</sub>)</code> are distinct.</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>Table: <code>Users</code></p>
|
||||
|
||||
<pre>
|
||||
+----------------+---------+
|
||||
| Column Name | Type |
|
||||
+----------------+---------+
|
||||
| user_id | int |
|
||||
| name | varchar |
|
||||
+----------------+---------+
|
||||
user_id is the primary key for this table.
|
||||
This table contains the ID and the name of the user. The name consists of only lowercase and uppercase characters.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write an SQL query to fix the names so that only the first character is uppercase and the rest are lowercase.</p>
|
||||
|
||||
<p>Return the result table ordered by <code>user_id</code>.</p>
|
||||
|
||||
<p>The query result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Users table:
|
||||
+---------+-------+
|
||||
| user_id | name |
|
||||
+---------+-------+
|
||||
| 1 | aLice |
|
||||
| 2 | bOB |
|
||||
+---------+-------+
|
||||
<strong>Output:</strong>
|
||||
+---------+-------+
|
||||
| user_id | name |
|
||||
+---------+-------+
|
||||
| 1 | Alice |
|
||||
| 2 | Bob |
|
||||
+---------+-------+
|
||||
</pre>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given a binary string <code>binary</code> consisting of only <code>0</code>'s or <code>1</code>'s. You can apply each of the following operations any number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Operation 1: If the number contains the substring <code>"00"</code>, you can replace it with <code>"10"</code>.
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>"<u>00</u>010" -> "<u>10</u>010</code>"</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>Operation 2: If the number contains the substring <code>"10"</code>, you can replace it with <code>"01"</code>.
|
||||
<ul>
|
||||
<li>For example, <code>"000<u>10</u>" -> "000<u>01</u>"</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p><em>Return the <strong>maximum binary string</strong> you can obtain after any number of operations. Binary string <code>x</code> is greater than binary string <code>y</code> if <code>x</code>'s decimal representation is greater than <code>y</code>'s decimal representation.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> binary = "000110"
|
||||
<strong>Output:</strong> "111011"
|
||||
<strong>Explanation:</strong> A valid transformation sequence can be:
|
||||
"0001<u>10</u>" -> "0001<u>01</u>"
|
||||
"<u>00</u>0101" -> "<u>10</u>0101"
|
||||
"1<u>00</u>101" -> "1<u>10</u>101"
|
||||
"110<u>10</u>1" -> "110<u>01</u>1"
|
||||
"11<u>00</u>11" -> "11<u>10</u>11"
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> binary = "01"
|
||||
<strong>Output:</strong> "01"
|
||||
<strong>Explanation:</strong> "01" cannot be transformed any further.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= binary.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>binary</code> consist of <code>'0'</code> and <code>'1'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>The <strong>numeric value</strong> of a <strong>lowercase character</strong> is defined as its position <code>(1-indexed)</code> in the alphabet, so the numeric value of <code>a</code> is <code>1</code>, the numeric value of <code>b</code> is <code>2</code>, the numeric value of <code>c</code> is <code>3</code>, and so on.</p>
|
||||
|
||||
<p>The <strong>numeric value</strong> of a <strong>string</strong> consisting of lowercase characters is defined as the sum of its characters' numeric values. For example, the numeric value of the string <code>"abe"</code> is equal to <code>1 + 2 + 5 = 8</code>.</p>
|
||||
|
||||
<p>You are given two integers <code>n</code> and <code>k</code>. Return <em>the <strong>lexicographically smallest string</strong> with <strong>length</strong> equal to <code>n</code> and <strong>numeric value</strong> equal to <code>k</code>.</em></p>
|
||||
|
||||
<p>Note that a string <code>x</code> is lexicographically smaller than string <code>y</code> if <code>x</code> comes before <code>y</code> in dictionary order, that is, either <code>x</code> is a prefix of <code>y</code>, or if <code>i</code> is the first position such that <code>x[i] != y[i]</code>, then <code>x[i]</code> comes before <code>y[i]</code> in alphabetic order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, k = 27
|
||||
<strong>Output:</strong> "aay"
|
||||
<strong>Explanation:</strong> The numeric value of the string is 1 + 1 + 25 = 27, and it is the smallest string with such a value and length equal to 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, k = 73
|
||||
<strong>Output:</strong> "aaszz"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>n <= k <= 26 * n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>You are given a floating-point number <code>hour</code>, representing the amount of time you have to reach the office. To commute to the office, you must take <code>n</code> trains in sequential order. You are also given an integer array <code>dist</code> of length <code>n</code>, where <code>dist[i]</code> describes the distance (in kilometers) of the <code>i<sup>th</sup></code> train ride.</p>
|
||||
|
||||
<p>Each train can only depart at an integer hour, so you may need to wait in between each train ride.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if the <code>1<sup>st</sup></code> train ride takes <code>1.5</code> hours, you must wait for an additional <code>0.5</code> hours before you can depart on the <code>2<sup>nd</sup></code> train ride at the 2 hour mark.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum positive integer</strong> speed <strong>(in kilometers per hour)</strong> that all the trains must travel at for you to reach the office on time, or </em><code>-1</code><em> if it is impossible to be on time</em>.</p>
|
||||
|
||||
<p>Tests are generated such that the answer will not exceed <code>10<sup>7</sup></code> and <code>hour</code> will have <strong>at most two digits after the decimal point</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> dist = [1,3,2], hour = 6
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation: </strong>At speed 1:
|
||||
- The first train ride takes 1/1 = 1 hour.
|
||||
- Since we are already at an integer hour, we depart immediately at the 1 hour mark. The second train takes 3/1 = 3 hours.
|
||||
- Since we are already at an integer hour, we depart immediately at the 4 hour mark. The third train takes 2/1 = 2 hours.
|
||||
- You will arrive at exactly the 6 hour mark.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> dist = [1,3,2], hour = 2.7
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation: </strong>At speed 3:
|
||||
- The first train ride takes 1/3 = 0.33333 hours.
|
||||
- Since we are not at an integer hour, we wait until the 1 hour mark to depart. The second train ride takes 3/3 = 1 hour.
|
||||
- Since we are already at an integer hour, we depart immediately at the 2 hour mark. The third train takes 2/3 = 0.66667 hours.
|
||||
- You will arrive at the 2.66667 hour mark.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> dist = [1,3,2], hour = 1.9
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It is impossible because the earliest the third train can depart is at the 2 hour mark.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == dist.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= dist[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= hour <= 10<sup>9</sup></code></li>
|
||||
<li>There will be at most two digits after the decimal point in <code>hour</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>You are given an integer <code>hoursBefore</code>, the number of hours you have to travel to your meeting. To arrive at your meeting, you have to travel through <code>n</code> roads. The road lengths are given as an integer array <code>dist</code> of length <code>n</code>, where <code>dist[i]</code> describes the length of the <code>i<sup>th</sup></code> road in <strong>kilometers</strong>. In addition, you are given an integer <code>speed</code>, which is the speed (in <strong>km/h</strong>) you will travel at.</p>
|
||||
|
||||
<p>After you travel road <code>i</code>, you must rest and wait for the <strong>next integer hour</strong> before you can begin traveling on the next road. Note that you do not have to rest after traveling the last road because you are already at the meeting.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if traveling a road takes <code>1.4</code> hours, you must wait until the <code>2</code> hour mark before traveling the next road. If traveling a road takes exactly <code>2</code> hours, you do not need to wait.</li>
|
||||
</ul>
|
||||
|
||||
<p>However, you are allowed to <strong>skip</strong> some rests to be able to arrive on time, meaning you do not need to wait for the next integer hour. Note that this means you may finish traveling future roads at different hour marks.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, suppose traveling the first road takes <code>1.4</code> hours and traveling the second road takes <code>0.6</code> hours. Skipping the rest after the first road will mean you finish traveling the second road right at the <code>2</code> hour mark, letting you start traveling the third road immediately.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum number of skips required</strong> to arrive at the meeting on time, or</em> <code>-1</code><em> if it is<strong> impossible</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> dist = [1,3,2], speed = 4, hoursBefore = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
Without skipping any rests, you will arrive in (1/4 + 3/4) + (3/4 + 1/4) + (2/4) = 2.5 hours.
|
||||
You can skip the first rest to arrive in ((1/4 + <u>0</u>) + (3/4 + 0)) + (2/4) = 1.5 hours.
|
||||
Note that the second rest is shortened because you finish traveling the second road at an integer hour due to skipping the first rest.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> dist = [7,3,5,5], speed = 2, hoursBefore = 10
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
Without skipping any rests, you will arrive in (7/2 + 1/2) + (3/2 + 1/2) + (5/2 + 1/2) + (5/2) = 11.5 hours.
|
||||
You can skip the first and third rest to arrive in ((7/2 + <u>0</u>) + (3/2 + 0)) + ((5/2 + <u>0</u>) + (5/2)) = 10 hours.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> dist = [7,3,5,5], speed = 1, hoursBefore = 10
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It is impossible to arrive at the meeting on time even if you skip all the rests.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == dist.length</code></li>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
<li><code>1 <= dist[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= speed <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= hoursBefore <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>You are given an array of positive integers <code>arr</code>. Perform some operations (possibly none) on <code>arr</code> so that it satisfies these conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The value of the <strong>first</strong> element in <code>arr</code> must be <code>1</code>.</li>
|
||||
<li>The absolute difference between any 2 adjacent elements must be <strong>less than or equal to </strong><code>1</code>. In other words, <code>abs(arr[i] - arr[i - 1]) <= 1</code> for each <code>i</code> where <code>1 <= i < arr.length</code> (<strong>0-indexed</strong>). <code>abs(x)</code> is the absolute value of <code>x</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>There are 2 types of operations that you can perform any number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Decrease</strong> the value of any element of <code>arr</code> to a <strong>smaller positive integer</strong>.</li>
|
||||
<li><strong>Rearrange</strong> the elements of <code>arr</code> to be in any order.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible value of an element in </em><code>arr</code><em> after performing the operations to satisfy the conditions</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [2,2,1,2,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
We can satisfy the conditions by rearranging <code>arr</code> so it becomes <code>[1,2,2,2,1]</code>.
|
||||
The largest element in <code>arr</code> is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [100,1,1000]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
One possible way to satisfy the conditions is by doing the following:
|
||||
1. Rearrange <code>arr</code> so it becomes <code>[1,100,1000]</code>.
|
||||
2. Decrease the value of the second element to 2.
|
||||
3. Decrease the value of the third element to 3.
|
||||
Now <code>arr = [1,2,3], which </code>satisfies the conditions.
|
||||
The largest element in <code>arr is 3.</code>
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The array already satisfies the conditions, and the largest element is 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>You are given two strings <code>a</code> and <code>b</code> of the same length. Choose an index and split both strings <strong>at the same index</strong>, splitting <code>a</code> into two strings: <code>a<sub>prefix</sub></code> and <code>a<sub>suffix</sub></code> where <code>a = a<sub>prefix</sub> + a<sub>suffix</sub></code>, and splitting <code>b</code> into two strings: <code>b<sub>prefix</sub></code> and <code>b<sub>suffix</sub></code> where <code>b = b<sub>prefix</sub> + b<sub>suffix</sub></code>. Check if <code>a<sub>prefix</sub> + b<sub>suffix</sub></code> or <code>b<sub>prefix</sub> + a<sub>suffix</sub></code> forms a palindrome.</p>
|
||||
|
||||
<p>When you split a string <code>s</code> into <code>s<sub>prefix</sub></code> and <code>s<sub>suffix</sub></code>, either <code>s<sub>suffix</sub></code> or <code>s<sub>prefix</sub></code> is allowed to be empty. For example, if <code>s = "abc"</code>, then <code>"" + "abc"</code>, <code>"a" + "bc"</code>, <code>"ab" + "c"</code> , and <code>"abc" + ""</code> are valid splits.</p>
|
||||
|
||||
<p>Return <code>true</code><em> if it is possible to form</em><em> a palindrome string, otherwise return </em><code>false</code>.</p>
|
||||
|
||||
<p><strong>Notice</strong> that <code>x + y</code> denotes the concatenation of strings <code>x</code> and <code>y</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = "x", b = "y"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explaination:</strong> If either a or b are palindromes the answer is true since you can split in the following way:
|
||||
a<sub>prefix</sub> = "", a<sub>suffix</sub> = "x"
|
||||
b<sub>prefix</sub> = "", b<sub>suffix</sub> = "y"
|
||||
Then, a<sub>prefix</sub> + b<sub>suffix</sub> = "" + "y" = "y", which is a palindrome.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = "xbdef", b = "xecab"
|
||||
<strong>Output:</strong> false
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = "ulacfd", b = "jizalu"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explaination:</strong> Split them at index 3:
|
||||
a<sub>prefix</sub> = "ula", a<sub>suffix</sub> = "cfd"
|
||||
b<sub>prefix</sub> = "jiz", b<sub>suffix</sub> = "alu"
|
||||
Then, a<sub>prefix</sub> + b<sub>suffix</sub> = "ula" + "alu" = "ulaalu", which is a palindrome.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= a.length, b.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>a.length == b.length</code></li>
|
||||
<li><code>a</code> and <code>b</code> consist of lowercase English letters</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>Given a binary string <code>s</code>, you can split <code>s</code> into 3 <strong>non-empty</strong> strings <code>s1</code>, <code>s2</code>, and <code>s3</code> where <code>s1 + s2 + s3 = s</code>.</p>
|
||||
|
||||
<p>Return the number of ways <code>s</code> can be split such that the number of ones is the same in <code>s1</code>, <code>s2</code>, and <code>s3</code>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "10101"
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> There are four ways to split s in 3 parts where each part contain the same number of letters '1'.
|
||||
"1|010|1"
|
||||
"1|01|01"
|
||||
"10|10|1"
|
||||
"10|1|01"
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "1001"
|
||||
<strong>Output:</strong> 0
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "0000"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are three ways to split s in 3 parts.
|
||||
"0|0|00"
|
||||
"0|00|0"
|
||||
"00|0|0"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given an array of <code>n</code> integers, <code>nums</code>, where there are at most <code>50</code> unique values in the array. You are also given an array of <code>m</code> customer order quantities, <code>quantity</code>, where <code>quantity[i]</code> is the amount of integers the <code>i<sup>th</sup></code> customer ordered. Determine if it is possible to distribute <code>nums</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <code>i<sup>th</sup></code> customer gets <strong>exactly</strong> <code>quantity[i]</code> integers,</li>
|
||||
<li>The integers the <code>i<sup>th</sup></code> customer gets are <strong>all equal</strong>, and</li>
|
||||
<li>Every customer is satisfied.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>true</code><em> if it is possible to distribute </em><code>nums</code><em> according to the above conditions</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4], quantity = [2]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The 0<sup>th</sup> customer cannot be given two different integers.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,3], quantity = [2]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The 0<sup>th</sup> customer is given [3,3]. The integers [1,2] are not used.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,2,2], quantity = [2,2]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The 0<sup>th</sup> customer is given [1,1], and the 1st customer is given [2,2].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
<li><code>m == quantity.length</code></li>
|
||||
<li><code>1 <= m <= 10</code></li>
|
||||
<li><code>1 <= quantity[i] <= 10<sup>5</sup></code></li>
|
||||
<li>There are at most <code>50</code> unique values in <code>nums</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>Given a wooden stick of length <code>n</code> units. The stick is labelled from <code>0</code> to <code>n</code>. For example, a stick of length <strong>6</strong> is labelled as follows:</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/21/statement.jpg" style="width: 521px; height: 111px;" />
|
||||
<p>Given an integer array <code>cuts</code> where <code>cuts[i]</code> denotes a position you should perform a cut at.</p>
|
||||
|
||||
<p>You should perform the cuts in order, you can change the order of the cuts as you wish.</p>
|
||||
|
||||
<p>The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation.</p>
|
||||
|
||||
<p>Return <em>the minimum total cost</em> of the cuts.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/23/e1.jpg" style="width: 350px; height: 284px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7, cuts = [1,3,4,5]
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong> Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario:
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/21/e11.jpg" style="width: 350px; height: 284px;" />
|
||||
The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20.
|
||||
Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16).</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 9, cuts = [5,6,1,4,2]
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong> If you try the given cuts ordering the cost will be 25.
|
||||
There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= cuts.length <= min(n - 1, 100)</code></li>
|
||||
<li><code>1 <= cuts[i] <= n - 1</code></li>
|
||||
<li>All the integers in <code>cuts</code> array are <strong>distinct</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code>, return <code>true</code> <em>if it can be made <strong>strictly increasing</strong> after removing <strong>exactly one</strong> element, or </em><code>false</code><em> otherwise. If the array is already strictly increasing, return </em><code>true</code>.</p>
|
||||
|
||||
<p>The array <code>nums</code> is <strong>strictly increasing</strong> if <code>nums[i - 1] < nums[i]</code> for each index <code>(1 <= i < nums.length).</code></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,<u>10</u>,5,7]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> By removing 10 at index 2 from nums, it becomes [1,2,5,7].
|
||||
[1,2,5,7] is strictly increasing, so return true.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,1,2]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong>
|
||||
[3,1,2] is the result of removing the element at index 0.
|
||||
[2,1,2] is the result of removing the element at index 1.
|
||||
[2,3,2] is the result of removing the element at index 2.
|
||||
[2,3,1] is the result of removing the element at index 3.
|
||||
No resulting array is strictly increasing, so return false.</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The result of removing any element is [1,1].
|
||||
[1,1] is not strictly increasing, so return false.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>Given two strings <code>s</code> and <code>part</code>, perform the following operation on <code>s</code> until <strong>all</strong> occurrences of the substring <code>part</code> are removed:</p>
|
||||
|
||||
<ul>
|
||||
<li>Find the <strong>leftmost</strong> occurrence of the substring <code>part</code> and <strong>remove</strong> it from <code>s</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>s</code><em> after removing all occurrences of </em><code>part</code>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters in a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "daabcbaabcbc", part = "abc"
|
||||
<strong>Output:</strong> "dab"
|
||||
<strong>Explanation</strong>: The following operations are done:
|
||||
- s = "da<strong><u>abc</u></strong>baabcbc", remove "abc" starting at index 2, so s = "dabaabcbc".
|
||||
- s = "daba<strong><u>abc</u></strong>bc", remove "abc" starting at index 4, so s = "dababc".
|
||||
- s = "dab<strong><u>abc</u></strong>", remove "abc" starting at index 3, so s = "dab".
|
||||
Now s has no occurrences of "abc".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "axxxxyyyyb", part = "xy"
|
||||
<strong>Output:</strong> "ab"
|
||||
<strong>Explanation</strong>: The following operations are done:
|
||||
- s = "axxx<strong><u>xy</u></strong>yyyb", remove "xy" starting at index 4 so s = "axxxyyyb".
|
||||
- s = "axx<strong><u>xy</u></strong>yyb", remove "xy" starting at index 3 so s = "axxyyb".
|
||||
- s = "ax<strong><u>xy</u></strong>yb", remove "xy" starting at index 2 so s = "axyb".
|
||||
- s = "a<strong><u>xy</u></strong>b", remove "xy" starting at index 1 so s = "ab".
|
||||
Now s has no occurrences of "xy".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>1 <= part.length <= 1000</code></li>
|
||||
<li><code>s</code> and <code>part</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
|
||||
|
||||
<ul>
|
||||
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
|
||||
|
||||
<ul>
|
||||
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
|
||||
<ul>
|
||||
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
|
||||
<strong>Output:</strong> 19
|
||||
<strong>Explanation:</strong>
|
||||
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
|
||||
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
|
||||
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
|
||||
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
|
||||
Total score = 5 + 4 + 5 + 5 = 19.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
|
||||
<strong>Output:</strong> 20
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing <strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum score</strong> you can get by erasing <strong>exactly one</strong> subarray.</em></p>
|
||||
|
||||
<p>An array <code>b</code> is called to be a <span class="tex-font-style-it">subarray</span> of <code>a</code> if it forms a contiguous subsequence of <code>a</code>, that is, if it is equal to <code>a[l],a[l+1],...,a[r]</code> for some <code>(l,r)</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,2,4,5,6]
|
||||
<strong>Output:</strong> 17
|
||||
<strong>Explanation:</strong> The optimal subarray here is [2,4,5,6].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,2,1,2,5,2,1,2,5]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> The optimal subarray here is [5,2,1] or [1,2,5].
|
||||
</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>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>Given a string <code>s</code> consisting only of characters <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>. You are asked to apply the following algorithm on the string any number of times:</p>
|
||||
|
||||
<ol>
|
||||
<li>Pick a <strong>non-empty</strong> prefix from the string <code>s</code> where all the characters in the prefix are equal.</li>
|
||||
<li>Pick a <strong>non-empty</strong> suffix from the string <code>s</code> where all the characters in this suffix are equal.</li>
|
||||
<li>The prefix and the suffix should not intersect at any index.</li>
|
||||
<li>The characters from the prefix and suffix must be the same.</li>
|
||||
<li>Delete both the prefix and the suffix.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>the <strong>minimum length</strong> of </em><code>s</code> <em>after performing the above operation any number of times (possibly zero times)</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "ca"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>You can't remove any characters, so the string stays as is.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "cabaabac"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> An optimal sequence of operations is:
|
||||
- Take prefix = "c" and suffix = "c" and remove them, s = "abaaba".
|
||||
- Take prefix = "a" and suffix = "a" and remove them, s = "baab".
|
||||
- Take prefix = "b" and suffix = "b" and remove them, s = "aa".
|
||||
- Take prefix = "a" and suffix = "a" and remove them, s = "".</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aabccabba"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> An optimal sequence of operations is:
|
||||
- Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb".
|
||||
- Take prefix = "b" and suffix = "bb" and remove them, s = "cca".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> only consists of characters <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>Given an integer array <code>arr</code>, remove a subarray (can be empty) from <code>arr</code> such that the remaining elements in <code>arr</code> are <strong>non-decreasing</strong>.</p>
|
||||
|
||||
<p>Return <em>the length of the shortest subarray to remove</em>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous subsequence of the array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [1,2,3,10,4,2,3,5]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted.
|
||||
Another correct solution is to remove the subarray [3,10,4].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [5,4,3,2,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [1,2,3]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The array is already non-decreasing. We do not need to remove any elements.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= arr[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>Given an integer <code>n</code>, return <code>true</code> <em>if it is possible to represent </em><code>n</code><em> as the sum of distinct powers of three.</em> Otherwise, return <code>false</code>.</p>
|
||||
|
||||
<p>An integer <code>y</code> is a power of three if there exists an integer <code>x</code> such that <code>y == 3<sup>x</sup></code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 12
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> 12 = 3<sup>1</sup> + 3<sup>2</sup>
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 91
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> 91 = 3<sup>0</sup> + 3<sup>2</sup> + 3<sup>4</sup>
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 21
|
||||
<strong>Output:</strong> false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,27 @@
|
||||
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p>
|
||||
|
||||
<p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> sentence = "thequickbrownfoxjumpsoverthelazydog"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> sentence = "leetcode"
|
||||
<strong>Output:</strong> false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= sentence.length <= 1000</code></li>
|
||||
<li><code>sentence</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given <code>coordinates</code>, a string that represents the coordinates of a square of the chessboard. Below is a chessboard for your reference.</p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p>
|
||||
|
||||
<p>Return <code>true</code><em> if the square is white, and </em><code>false</code><em> if the square is black</em>.</p>
|
||||
|
||||
<p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first, and the number second.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> coordinates = "a1"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> From the chessboard above, the square with coordinates "a1" is black, so return false.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> coordinates = "h3"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> From the chessboard above, the square with coordinates "h3" is white, so return true.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> coordinates = "c7"
|
||||
<strong>Output:</strong> false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>coordinates.length == 2</code></li>
|
||||
<li><code>'a' <= coordinates[0] <= 'h'</code></li>
|
||||
<li><code>'1' <= coordinates[1] <= '8'</code></li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>You are given a string <code>s</code> of even length. Split this string into two halves of equal lengths, and let <code>a</code> be the first half and <code>b</code> be the second half.</p>
|
||||
|
||||
<p>Two strings are <strong>alike</strong> if they have the same number of vowels (<code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, <code>'u'</code>, <code>'A'</code>, <code>'E'</code>, <code>'I'</code>, <code>'O'</code>, <code>'U'</code>). Notice that <code>s</code> contains uppercase and lowercase letters.</p>
|
||||
|
||||
<p>Return <code>true</code><em> if </em><code>a</code><em> and </em><code>b</code><em> are <strong>alike</strong></em>. Otherwise, return <code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "book"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> a = "b<u>o</u>" and b = "<u>o</u>k". a has 1 vowel and b has 1 vowel. Therefore, they are alike.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "textbook"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> a = "t<u>e</u>xt" and b = "b<u>oo</u>k". a has 1 vowel whereas b has 2. Therefore, they are not alike.
|
||||
Notice that the vowel o is counted twice.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 1000</code></li>
|
||||
<li><code>s.length</code> is even.</li>
|
||||
<li><code>s</code> consists of <strong>uppercase and lowercase</strong> letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>Given two <code>n x n</code> binary matrices <code>mat</code> and <code>target</code>, return <code>true</code><em> if it is possible to make </em><code>mat</code><em> equal to </em><code>target</code><em> by <strong>rotating</strong> </em><code>mat</code><em> in <strong>90-degree increments</strong>, or </em><code>false</code><em> otherwise.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/20/grid3.png" style="width: 301px; height: 121px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,1],[1,0]], target = [[1,0],[0,1]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>We can rotate mat 90 degrees clockwise to make mat equal target.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/20/grid4.png" style="width: 301px; height: 121px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,1],[1,1]], target = [[1,0],[0,1]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is impossible to make mat equal to target by rotating mat.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/26/grid4.png" style="width: 661px; height: 184px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>We can rotate mat 90 degrees clockwise two times to make mat equal target.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == mat.length == target.length</code></li>
|
||||
<li><code>n == mat[i].length == target[i].length</code></li>
|
||||
<li><code>1 <= n <= 10</code></li>
|
||||
<li><code>mat[i][j]</code> and <code>target[i][j]</code> are either <code>0</code> or <code>1</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>A certain bug's home is on the x-axis at position <code>x</code>. Help them get there from position <code>0</code>.</p>
|
||||
|
||||
<p>The bug jumps according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>It can jump exactly <code>a</code> positions <strong>forward</strong> (to the right).</li>
|
||||
<li>It can jump exactly <code>b</code> positions <strong>backward</strong> (to the left).</li>
|
||||
<li>It cannot jump backward twice in a row.</li>
|
||||
<li>It cannot jump to any <code>forbidden</code> positions.</li>
|
||||
</ul>
|
||||
|
||||
<p>The bug may jump forward <strong>beyond</strong> its home, but it <strong>cannot jump</strong> to positions numbered with <strong>negative</strong> integers.</p>
|
||||
|
||||
<p>Given an array of integers <code>forbidden</code>, where <code>forbidden[i]</code> means that the bug cannot jump to the position <code>forbidden[i]</code>, and integers <code>a</code>, <code>b</code>, and <code>x</code>, return <em>the minimum number of jumps needed for the bug to reach its home</em>. If there is no possible sequence of jumps that lands the bug on position <code>x</code>, return <code>-1.</code></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> forbidden = [14,4,18,1,15], a = 3, b = 15, x = 9
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> 3 jumps forward (0 -> 3 -> 6 -> 9) will get the bug home.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> forbidden = [8,3,16,6,12,20], a = 15, b = 13, x = 11
|
||||
<strong>Output:</strong> -1
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> forbidden = [1,6,2,14,5,17,4], a = 16, b = 9, x = 7
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> One jump forward (0 -> 16) then one jump backward (16 -> 7) will get the bug home.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= forbidden.length <= 1000</code></li>
|
||||
<li><code>1 <= a, b, forbidden[i] <= 2000</code></li>
|
||||
<li><code>0 <= x <= 2000</code></li>
|
||||
<li>All the elements in <code>forbidden</code> are distinct.</li>
|
||||
<li>Position <code>x</code> is not forbidden.</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>Given an integer array <code>nums</code> <strong>(0-indexed)</strong> and two integers <code>target</code> and <code>start</code>, find an index <code>i</code> such that <code>nums[i] == target</code> and <code>abs(i - start)</code> is <strong>minimized</strong>. Note that <code>abs(x)</code> is the absolute value of <code>x</code>.</p>
|
||||
|
||||
<p>Return <code>abs(i - start)</code>.</p>
|
||||
|
||||
<p>It is <strong>guaranteed</strong> that <code>target</code> exists in <code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5], target = 5, start = 3
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> nums[4] = 5 is the only value equal to target, so the answer is abs(4 - 3) = 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1], target = 1, start = 0
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> nums[0] = 1 is the only value equal to target, so the answer is abs(0 - 0) = 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Every value of nums is 1, but nums[0] minimizes abs(i - start), which is abs(0 - 0) = 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= start < nums.length</code></li>
|
||||
<li><code>target</code> is in <code>nums</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>You are given a 2D integer array <code>intervals</code>, where <code>intervals[i] = [left<sub>i</sub>, right<sub>i</sub>]</code> describes the <code>i<sup>th</sup></code> interval starting at <code>left<sub>i</sub></code> and ending at <code>right<sub>i</sub></code> <strong>(inclusive)</strong>. The <strong>size</strong> of an interval is defined as the number of integers it contains, or more formally <code>right<sub>i</sub> - left<sub>i</sub> + 1</code>.</p>
|
||||
|
||||
<p>You are also given an integer array <code>queries</code>. The answer to the <code>j<sup>th</sup></code> query is the <strong>size of the smallest interval</strong> <code>i</code> such that <code>left<sub>i</sub> <= queries[j] <= right<sub>i</sub></code>. If no such interval exists, the answer is <code>-1</code>.</p>
|
||||
|
||||
<p>Return <em>an array containing the answers to the queries</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]
|
||||
<strong>Output:</strong> [3,3,1,4]
|
||||
<strong>Explanation:</strong> The queries are processed as follows:
|
||||
- Query = 2: The interval [2,4] is the smallest interval containing 2. The answer is 4 - 2 + 1 = 3.
|
||||
- Query = 3: The interval [2,4] is the smallest interval containing 3. The answer is 4 - 2 + 1 = 3.
|
||||
- Query = 4: The interval [4,4] is the smallest interval containing 4. The answer is 4 - 4 + 1 = 1.
|
||||
- Query = 5: The interval [3,6] is the smallest interval containing 5. The answer is 6 - 3 + 1 = 4.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]
|
||||
<strong>Output:</strong> [2,-1,4,6]
|
||||
<strong>Explanation:</strong> The queries are processed as follows:
|
||||
- Query = 2: The interval [2,3] is the smallest interval containing 2. The answer is 3 - 2 + 1 = 2.
|
||||
- Query = 19: None of the intervals contain 19. The answer is -1.
|
||||
- Query = 5: The interval [2,5] is the smallest interval containing 5. The answer is 5 - 2 + 1 = 4.
|
||||
- Query = 22: The interval [20,25] is the smallest interval containing 22. The answer is 25 - 20 + 1 = 6.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= intervals.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>intervals[i].length == 2</code></li>
|
||||
<li><code>1 <= left<sub>i</sub> <= right<sub>i</sub> <= 10<sup>7</sup></code></li>
|
||||
<li><code>1 <= queries[j] <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>A decimal number is called <strong>deci-binary</strong> if each of its digits is either <code>0</code> or <code>1</code> without any leading zeros. For example, <code>101</code> and <code>1100</code> are <strong>deci-binary</strong>, while <code>112</code> and <code>3001</code> are not.</p>
|
||||
|
||||
<p>Given a string <code>n</code> that represents a positive decimal integer, return <em>the <strong>minimum</strong> number of positive <strong>deci-binary</strong> numbers needed so that they sum up to </em><code>n</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = "32"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> 10 + 11 + 11 = 32
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = "82734"
|
||||
<strong>Output:</strong> 8
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = "27346209830709182346"
|
||||
<strong>Output:</strong> 9
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>n</code> consists of only digits.</li>
|
||||
<li><code>n</code> does not contain any leading zeros and represents a positive integer.</li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>You are given <code>n</code> tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>th</sup></code> task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p>
|
||||
|
||||
<p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p>
|
||||
|
||||
<ul>
|
||||
<li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li>
|
||||
<li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li>
|
||||
<li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li>
|
||||
<li>The CPU can finish a task then start a new one instantly.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the order in which the CPU will process the tasks.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]]
|
||||
<strong>Output:</strong> [0,2,3,1]
|
||||
<strong>Explanation: </strong>The events go as follows:
|
||||
- At time = 1, task 0 is available to process. Available tasks = {0}.
|
||||
- Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}.
|
||||
- At time = 2, task 1 is available to process. Available tasks = {1}.
|
||||
- At time = 3, task 2 is available to process. Available tasks = {1, 2}.
|
||||
- Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}.
|
||||
- At time = 4, task 3 is available to process. Available tasks = {1, 3}.
|
||||
- At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}.
|
||||
- At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}.
|
||||
- At time = 10, the CPU finishes task 1 and becomes idle.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]
|
||||
<strong>Output:</strong> [4,3,2,0,1]
|
||||
<strong>Explanation</strong><strong>: </strong>The events go as follows:
|
||||
- At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}.
|
||||
- Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}.
|
||||
- At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}.
|
||||
- At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}.
|
||||
- At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}.
|
||||
- At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}.
|
||||
- At time = 40, the CPU finishes task 1 and becomes idle.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>tasks.length == n</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
|
||||
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> There are:
|
||||
- 1 box of the first type that contains 3 units.
|
||||
- 2 boxes of the second type that contain 2 units each.
|
||||
- 3 boxes of the third type that contain 1 unit each.
|
||||
You can take all the boxes of the first and second types, and one box of the third type.
|
||||
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
|
||||
<strong>Output:</strong> 91
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= boxTypes.length <= 1000</code></li>
|
||||
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
|
||||
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,59 @@
|
||||
<p>You are given a <strong>valid</strong> boolean expression as a string <code>expression</code> consisting of the characters <code>'1'</code>,<code>'0'</code>,<code>'&'</code> (bitwise <strong>AND</strong> operator),<code>'|'</code> (bitwise <strong>OR</strong> operator),<code>'('</code>, and <code>')'</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>"()1|1"</code> and <code>"(1)&()"</code> are <strong>not valid</strong> while <code>"1"</code>, <code>"(((1))|(0))"</code>, and <code>"1|(0&(1))"</code> are <strong>valid</strong> expressions.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the <strong>minimum cost</strong> to change the final value of the expression</em>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>expression = "1|1|(0&0)&1"</code>, its <strong>value</strong> is <code>1|1|(0&0)&1 = 1|1|0&1 = 1|0&1 = 1&1 = 1</code>. We want to apply operations so that the<strong> new</strong> expression evaluates to <code>0</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>cost</strong> of changing the final value of an expression is the <strong>number of operations</strong> performed on the expression. The types of <strong>operations</strong> are described as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>Turn a <code>'1'</code> into a <code>'0'</code>.</li>
|
||||
<li>Turn a <code>'0'</code> into a <code>'1'</code>.</li>
|
||||
<li>Turn a <code>'&'</code> into a <code>'|'</code>.</li>
|
||||
<li>Turn a <code>'|'</code> into a <code>'&'</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note:</strong> <code>'&'</code> does <strong>not</strong> take precedence over <code>'|'</code> in the <strong>order of calculation</strong>. Evaluate parentheses <strong>first</strong>, then in <strong>left-to-right</strong> order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> expression = "1&(0|1)"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can turn "1&(0<u><strong>|</strong></u>1)" into "1&(0<u><strong>&</strong></u>1)" by changing the '|' to a '&' using 1 operation.
|
||||
The new expression evaluates to 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> expression = "(0&0)&(0&0&0)"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can turn "(0<u><strong>&0</strong></u>)<strong><u>&</u></strong>(0&0&0)" into "(0<u><strong>|1</strong></u>)<u><strong>|</strong></u>(0&0&0)" using 3 operations.
|
||||
The new expression evaluates to 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> expression = "(0|(1|0&1))"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can turn "(0|(<u><strong>1</strong></u>|0&1))" into "(0|(<u><strong>0</strong></u>|0&1))" using 1 operation.
|
||||
The new expression evaluates to 0.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= expression.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>expression</code> only contains <code>'1'</code>,<code>'0'</code>,<code>'&'</code>,<code>'|'</code>,<code>'('</code>, and <code>')'</code></li>
|
||||
<li>All parentheses are properly matched.</li>
|
||||
<li>There will be no empty parentheses (i.e: <code>"()"</code> is not a substring of <code>expression</code>).</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>A sentence is a list of words that are separated by a single space with no leading or trailing spaces. For example, <code>"Hello World"</code>, <code>"HELLO"</code>, <code>"hello world hello world"</code> are all sentences. Words consist of <strong>only</strong> uppercase and lowercase English letters.</p>
|
||||
|
||||
<p>Two sentences <code>sentence1</code> and <code>sentence2</code> are <strong>similar</strong> if it is possible to insert an arbitrary sentence <strong>(possibly empty)</strong> inside one of these sentences such that the two sentences become equal. For example, <code>sentence1 = "Hello my name is Jane"</code> and <code>sentence2 = "Hello Jane"</code> can be made equal by inserting <code>"my name is"</code> between <code>"Hello"</code> and <code>"Jane"</code> in <code>sentence2</code>.</p>
|
||||
|
||||
<p>Given two sentences <code>sentence1</code> and <code>sentence2</code>, return <code>true</code> <em>if </em><code>sentence1</code> <em>and </em><code>sentence2</code> <em>are similar.</em> Otherwise, return <code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> sentence1 = "My name is Haley", sentence2 = "My Haley"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> sentence2 can be turned to sentence1 by inserting "name is" between "My" and "Haley".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> sentence1 = "of", sentence2 = "A lot of words"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation: </strong>No single sentence can be inserted inside one of the sentences to make it equal to the other.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> sentence1 = "Eating right now", sentence2 = "Eating"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> sentence2 can be turned to sentence1 by inserting "right now" at the end of the sentence.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= sentence1.length, sentence2.length <= 100</code></li>
|
||||
<li><code>sentence1</code> and <code>sentence2</code> consist of lowercase and uppercase English letters and spaces.</li>
|
||||
<li>The words in <code>sentence1</code> and <code>sentence2</code> are separated by a single space.</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>Given a<strong> directed acyclic graph</strong>, with <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, and an array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represents a directed edge from node <code>from<sub>i</sub></code> to node <code>to<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Find <em>the smallest set of vertices from which all nodes in the graph are reachable</em>. It's guaranteed that a unique solution exists.</p>
|
||||
|
||||
<p>Notice that you can return the vertices in any order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/07/07/untitled22.png" style="width: 231px; height: 181px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]
|
||||
<strong>Output:</strong> [0,3]
|
||||
<b>Explanation: </b>It's not possible to reach all the nodes from a single vertex. From 0 we can reach [0,1,2,5]. From 3 we can reach [3,4,2,5]. So we output [0,3].</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/07/07/untitled.png" style="width: 201px; height: 201px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]]
|
||||
<strong>Output:</strong> [0,2,3]
|
||||
<strong>Explanation: </strong>Notice that vertices 0, 3 and 2 are not reachable from any other node, so we must include them. Also any of these vertices can reach nodes 1 and 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10^5</code></li>
|
||||
<li><code>1 <= edges.length <= min(10^5, n * (n - 1) / 2)</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= from<sub>i,</sub> to<sub>i</sub> < n</code></li>
|
||||
<li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>You are given an integer array <code>heights</code> representing the heights of buildings, some <code>bricks</code>, and some <code>ladders</code>.</p>
|
||||
|
||||
<p>You start your journey from building <code>0</code> and move to the next building by possibly using bricks or ladders.</p>
|
||||
|
||||
<p>While moving from building <code>i</code> to building <code>i+1</code> (<strong>0-indexed</strong>),</p>
|
||||
|
||||
<ul>
|
||||
<li>If the current building's height is <strong>greater than or equal</strong> to the next building's height, you do <strong>not</strong> need a ladder or bricks.</li>
|
||||
<li>If the current building's height is <b>less than</b> the next building's height, you can either use <strong>one ladder</strong> or <code>(h[i+1] - h[i])</code> <strong>bricks</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p><em>Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/27/q4.gif" style="width: 562px; height: 561px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Starting at building 0, you can follow these steps:
|
||||
- Go to building 1 without using ladders nor bricks since 4 >= 2.
|
||||
- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 < 7.
|
||||
- Go to building 3 without using ladders nor bricks since 7 >= 6.
|
||||
- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 < 9.
|
||||
It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
|
||||
<strong>Output:</strong> 7
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> heights = [14,3,19,3], bricks = 17, ladders = 0
|
||||
<strong>Output:</strong> 3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= heights.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= heights[i] <= 10<sup>6</sup></code></li>
|
||||
<li><code>0 <= bricks <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= ladders <= heights.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>You are given an array <code>rectangles</code> where <code>rectangles[i] = [l<sub>i</sub>, w<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> rectangle of length <code>l<sub>i</sub></code> and width <code>w<sub>i</sub></code>.</p>
|
||||
|
||||
<p>You can cut the <code>i<sup>th</sup></code> rectangle to form a square with a side length of <code>k</code> if both <code>k <= l<sub>i</sub></code> and <code>k <= w<sub>i</sub></code>. For example, if you have a rectangle <code>[4,6]</code>, you can cut it to get a square with a side length of at most <code>4</code>.</p>
|
||||
|
||||
<p>Let <code>maxLen</code> be the side length of the <strong>largest</strong> square you can obtain from any of the given rectangles.</p>
|
||||
|
||||
<p>Return <em>the <strong>number</strong> of rectangles that can make a square with a side length of </em><code>maxLen</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> rectangles = [[5,8],[3,9],[5,12],[16,5]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The largest squares you can get from each rectangle are of lengths [5,3,5,5].
|
||||
The largest possible square is of length 5, and you can get it out of 3 rectangles.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> rectangles = [[2,3],[3,7],[4,3],[3,7]]
|
||||
<strong>Output:</strong> 3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= rectangles.length <= 1000</code></li>
|
||||
<li><code>rectangles[i].length == 2</code></li>
|
||||
<li><code>1 <= l<sub>i</sub>, w<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>l<sub>i</sub> != w<sub>i</sub></code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>Table: <code>Products</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| product_id | int |
|
||||
| low_fats | enum |
|
||||
| recyclable | enum |
|
||||
+-------------+---------+
|
||||
product_id is the primary key for this table.
|
||||
low_fats is an ENUM of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not.
|
||||
recyclable is an ENUM of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not.</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write an SQL query to find the ids of products that are both low fat and recyclable.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The query result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Products table:
|
||||
+-------------+----------+------------+
|
||||
| product_id | low_fats | recyclable |
|
||||
+-------------+----------+------------+
|
||||
| 0 | Y | N |
|
||||
| 1 | Y | Y |
|
||||
| 2 | N | Y |
|
||||
| 3 | Y | Y |
|
||||
| 4 | N | N |
|
||||
+-------------+----------+------------+
|
||||
<strong>Output:</strong>
|
||||
+-------------+
|
||||
| product_id |
|
||||
+-------------+
|
||||
| 1 |
|
||||
| 3 |
|
||||
+-------------+
|
||||
<strong>Explanation:</strong> Only products 1 and 3 are both low fat and recyclable.
|
||||
</pre>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given two strings <code>s</code> and <code>p</code> where <code>p</code> is a <strong>subsequence </strong>of <code>s</code>. You are also given a <strong>distinct 0-indexed </strong>integer array <code>removable</code> containing a subset of indices of <code>s</code> (<code>s</code> is also <strong>0-indexed</strong>).</p>
|
||||
|
||||
<p>You want to choose an integer <code>k</code> (<code>0 <= k <= removable.length</code>) such that, after removing <code>k</code> characters from <code>s</code> using the <strong>first</strong> <code>k</code> indices in <code>removable</code>, <code>p</code> is still a <strong>subsequence</strong> of <code>s</code>. More formally, you will mark the character at <code>s[removable[i]]</code> for each <code>0 <= i < k</code>, then remove all marked characters and check if <code>p</code> is still a subsequence.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> </em><code>k</code><em> you can choose such that </em><code>p</code><em> is still a <strong>subsequence</strong> of </em><code>s</code><em> after the removals</em>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcacb", p = "ab", removable = [3,1,0]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation</strong>: After removing the characters at indices 3 and 1, "a<s><strong>b</strong></s>c<s><strong>a</strong></s>cb" becomes "accb".
|
||||
"ab" is a subsequence of "<strong><u>a</u></strong>cc<strong><u>b</u></strong>".
|
||||
If we remove the characters at indices 3, 1, and 0, "<s><strong>ab</strong></s>c<s><strong>a</strong></s>cb" becomes "ccb", and "ab" is no longer a subsequence.
|
||||
Hence, the maximum k is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcbddddd", p = "abcd", removable = [3,2,1,4,5,6]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation</strong>: After removing the character at index 3, "abc<s><strong>b</strong></s>ddddd" becomes "abcddddd".
|
||||
"abcd" is a subsequence of "<u><strong>abcd</strong></u>dddd".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcab", p = "abc", removable = [0,1,2,3,4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation</strong>: If you remove the first index in the array removable, "abc" is no longer a subsequence.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= p.length <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= removable.length < s.length</code></li>
|
||||
<li><code>0 <= removable[i] < s.length</code></li>
|
||||
<li><code>p</code> is a <strong>subsequence</strong> of <code>s</code>.</li>
|
||||
<li><code>s</code> and <code>p</code> both consist of lowercase English letters.</li>
|
||||
<li>The elements in <code>removable</code> are <strong>distinct</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>You are given an array <code>points</code>, an integer <code>angle</code>, and your <code>location</code>, where <code>location = [pos<sub>x</sub>, pos<sub>y</sub>]</code> and <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> both denote <strong>integral coordinates</strong> on the X-Y plane.</p>
|
||||
|
||||
<p>Initially, you are facing directly east from your position. You <strong>cannot move</strong> from your position, but you can <strong>rotate</strong>. In other words, <code>pos<sub>x</sub></code> and <code>pos<sub>y</sub></code> cannot be changed. Your field of view in <strong>degrees</strong> is represented by <code>angle</code>, determining how wide you can see from any given view direction. Let <code>d</code> be the amount in degrees that you rotate counterclockwise. Then, your field of view is the <strong>inclusive</strong> range of angles <code>[d - angle/2, d + angle/2]</code>.</p>
|
||||
|
||||
<p>
|
||||
<video autoplay="" controls="" height="360" muted="" style="max-width:100%;height:auto;" width="480"><source src="https://assets.leetcode.com/uploads/2020/09/30/angle.mp4" type="video/mp4" />Your browser does not support the video tag or this video format.</video>
|
||||
</p>
|
||||
|
||||
<p>You can <strong>see</strong> some set of points if, for each point, the <strong>angle</strong> formed by the point, your position, and the immediate east direction from your position is <strong>in your field of view</strong>.</p>
|
||||
|
||||
<p>There can be multiple points at one coordinate. There may be points at your location, and you can always see these points regardless of your rotation. Points do not obstruct your vision to other points.</p>
|
||||
|
||||
<p>Return <em>the maximum number of points you can see</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/30/89a07e9b-00ab-4967-976a-c723b2aa8656.png" style="width: 400px; height: 300px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The shaded region represents your field of view. All points can be made visible in your field of view, including [3,3] even though [2,2] is in front and in the same line of sight.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[2,1],[2,2],[3,4],[1,1]], angle = 90, location = [1,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> All points can be made visible in your field of view, including the one at your location.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/30/5010bfd3-86e6-465f-ac64-e9df941d2e49.png" style="width: 690px; height: 348px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[1,0],[2,1]], angle = 13, location = [1,1]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> You can only see one of the two points, as shown above.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>location.length == 2</code></li>
|
||||
<li><code>0 <= angle < 360</code></li>
|
||||
<li><code>0 <= pos<sub>x</sub>, pos<sub>y</sub>, x<sub>i</sub>, y<sub>i</sub> <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>There are <code>n</code> oranges in the kitchen and you decided to eat some of these oranges every day as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>Eat one orange.</li>
|
||||
<li>If the number of remaining oranges <code>n</code> is divisible by <code>2</code> then you can eat <code>n / 2</code> oranges.</li>
|
||||
<li>If the number of remaining oranges <code>n</code> is divisible by <code>3</code> then you can eat <code>2 * (n / 3)</code> oranges.</li>
|
||||
</ul>
|
||||
|
||||
<p>You can only choose one of the actions per day.</p>
|
||||
|
||||
<p>Given the integer <code>n</code>, return <em>the minimum number of days to eat</em> <code>n</code> <em>oranges</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 10
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> You have 10 oranges.
|
||||
Day 1: Eat 1 orange, 10 - 1 = 9.
|
||||
Day 2: Eat 6 oranges, 9 - 2*(9/3) = 9 - 6 = 3. (Since 9 is divisible by 3)
|
||||
Day 3: Eat 2 oranges, 3 - 2*(3/3) = 3 - 2 = 1.
|
||||
Day 4: Eat the last orange 1 - 1 = 0.
|
||||
You need at least 4 days to eat the 10 oranges.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> You have 6 oranges.
|
||||
Day 1: Eat 3 oranges, 6 - 6/2 = 6 - 3 = 3. (Since 6 is divisible by 2).
|
||||
Day 2: Eat 2 oranges, 3 - 2*(3/3) = 3 - 2 = 1. (Since 3 is divisible by 3)
|
||||
Day 3: Eat the last orange 1 - 1 = 0.
|
||||
You need at least 3 days to eat the 6 oranges.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2 * 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>There is a special kind of apple tree that grows apples every day for <code>n</code> days. On the <code>i<sup>th</sup></code> day, the tree grows <code>apples[i]</code> apples that will rot after <code>days[i]</code> days, that is on day <code>i + days[i]</code> the apples will be rotten and cannot be eaten. On some days, the apple tree does not grow any apples, which are denoted by <code>apples[i] == 0</code> and <code>days[i] == 0</code>.</p>
|
||||
|
||||
<p>You decided to eat <strong>at most</strong> one apple a day (to keep the doctors away). Note that you can keep eating after the first <code>n</code> days.</p>
|
||||
|
||||
<p>Given two integer arrays <code>days</code> and <code>apples</code> of length <code>n</code>, return <em>the maximum number of apples you can eat.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> apples = [1,2,3,5,2], days = [3,2,1,4,2]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> You can eat 7 apples:
|
||||
- On the first day, you eat an apple that grew on the first day.
|
||||
- On the second day, you eat an apple that grew on the second day.
|
||||
- On the third day, you eat an apple that grew on the second day. After this day, the apples that grew on the third day rot.
|
||||
- On the fourth to the seventh days, you eat apples that grew on the fourth day.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> You can eat 5 apples:
|
||||
- On the first to the third day you eat apples that grew on the first day.
|
||||
- Do nothing on the fouth and fifth days.
|
||||
- On the sixth and seventh days you eat apples that grew on the sixth day.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == apples.length == days.length</code></li>
|
||||
<li><code>1 <= n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= apples[i], days[i] <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>days[i] = 0</code> if and only if <code>apples[i] = 0</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>You are given two linked lists: <code>list1</code> and <code>list2</code> of sizes <code>n</code> and <code>m</code> respectively.</p>
|
||||
|
||||
<p>Remove <code>list1</code>'s nodes from the <code>a<sup>th</sup></code> node to the <code>b<sup>th</sup></code> node, and put <code>list2</code> in their place.</p>
|
||||
|
||||
<p>The blue edges and nodes in the following figure indicate the result:</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/fig1.png" style="height: 130px; width: 504px;" />
|
||||
<p><em>Build the result list and return its head.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/merge_linked_list_ex1.png" style="width: 406px; height: 140px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> list1 = [0,1,2,3,4,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]
|
||||
<strong>Output:</strong> [0,1,2,1000000,1000001,1000002,5]
|
||||
<strong>Explanation:</strong> We remove the nodes 3 and 4 and put the entire list2 in their place. The blue edges and nodes in the above figure indicate the result.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/merge_linked_list_ex2.png" style="width: 463px; height: 140px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [1000000,1000001,1000002,1000003,1000004]
|
||||
<strong>Output:</strong> [0,1,1000000,1000001,1000002,1000003,1000004,6]
|
||||
<strong>Explanation:</strong> The blue edges and nodes in the above figure indicate the result.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= list1.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= a <= b < list1.length - 1</code></li>
|
||||
<li><code>1 <= list2.length <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,66 @@
|
||||
<p>You are given <code>n</code> <strong>BST (binary search tree) root nodes</strong> for <code>n</code> separate BSTs stored in an array <code>trees</code> (<strong>0-indexed</strong>). Each BST in <code>trees</code> has <strong>at most 3 nodes</strong>, and no two roots have the same value. In one operation, you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Select two <strong>distinct</strong> indices <code>i</code> and <code>j</code> such that the value stored at one of the <strong>leaves </strong>of <code>trees[i]</code> is equal to the <strong>root value</strong> of <code>trees[j]</code>.</li>
|
||||
<li>Replace the leaf node in <code>trees[i]</code> with <code>trees[j]</code>.</li>
|
||||
<li>Remove <code>trees[j]</code> from <code>trees</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the <strong>root</strong> of the resulting BST if it is possible to form a valid BST after performing </em><code>n - 1</code><em> operations, or</em><em> </em><code>null</code> <i>if it is impossible to create a valid BST</i>.</p>
|
||||
|
||||
<p>A BST (binary search tree) is a binary tree where each node satisfies the following property:</p>
|
||||
|
||||
<ul>
|
||||
<li>Every node in the node's left subtree has a value <strong>strictly less</strong> than the node's value.</li>
|
||||
<li>Every node in the node's right subtree has a value <strong>strictly greater</strong> than the node's value.</li>
|
||||
</ul>
|
||||
|
||||
<p>A leaf is a node that has no children.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/08/d1.png" style="width: 450px; height: 163px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> trees = [[2,1],[3,2,5],[5,4]]
|
||||
<strong>Output:</strong> [3,2,5,1,null,4]
|
||||
<strong>Explanation:</strong>
|
||||
In the first operation, pick i=1 and j=0, and merge trees[0] into trees[1].
|
||||
Delete trees[0], so trees = [[3,2,5,1],[5,4]].
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/24/diagram.png" style="width: 450px; height: 181px;" />
|
||||
In the second operation, pick i=0 and j=1, and merge trees[1] into trees[0].
|
||||
Delete trees[1], so trees = [[3,2,5,1,null,4]].
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/24/diagram-2.png" style="width: 220px; height: 165px;" />
|
||||
The resulting tree, shown above, is a valid BST, so return its root.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/08/d2.png" style="width: 450px; height: 171px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> trees = [[5,3,8],[3,2,6]]
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong>
|
||||
Pick i=0 and j=1 and merge trees[1] into trees[0].
|
||||
Delete trees[1], so trees = [[5,3,8,2,6]].
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/24/diagram-3.png" style="width: 240px; height: 196px;" />
|
||||
The resulting tree is shown above. This is the only valid operation that can be performed, but the resulting tree is not a valid BST, so return null.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/08/d3.png" style="width: 430px; height: 168px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> trees = [[5,4],[3]]
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> It is impossible to perform any operations.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == trees.length</code></li>
|
||||
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
|
||||
<li>The number of nodes in each tree is in the range <code>[1, 3]</code>.</li>
|
||||
<li>Each node in the input may have children but no grandchildren.</li>
|
||||
<li>No two roots of <code>trees</code> have the same value.</li>
|
||||
<li>All the trees in the input are <strong>valid BSTs</strong>.</li>
|
||||
<li><code>1 <= TreeNode.val <= 5 * 10<sup>4</sup></code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>A <strong>triplet</strong> is an array of three integers. You are given a 2D integer array <code>triplets</code>, where <code>triplets[i] = [a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>]</code> describes the <code>i<sup>th</sup></code> <strong>triplet</strong>. You are also given an integer array <code>target = [x, y, z]</code> that describes the <strong>triplet</strong> you want to obtain.</p>
|
||||
|
||||
<p>To obtain <code>target</code>, you may apply the following operation on <code>triplets</code> <strong>any number</strong> of times (possibly <strong>zero</strong>):</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose two indices (<strong>0-indexed</strong>) <code>i</code> and <code>j</code> (<code>i != j</code>) and <strong>update</strong> <code>triplets[j]</code> to become <code>[max(a<sub>i</sub>, a<sub>j</sub>), max(b<sub>i</sub>, b<sub>j</sub>), max(c<sub>i</sub>, c<sub>j</sub>)]</code>.
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>triplets[i] = [2, 5, 3]</code> and <code>triplets[j] = [1, 7, 5]</code>, <code>triplets[j]</code> will be updated to <code>[max(2, 1), max(5, 7), max(3, 5)] = [2, 7, 5]</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>true</code> <em>if it is possible to obtain the </em><code>target</code><em> <strong>triplet</strong> </em><code>[x, y, z]</code><em> as an<strong> element</strong> of </em><code>triplets</code><em>, or </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Perform the following operations:
|
||||
- Choose the first and last triplets [<u>[2,5,3]</u>,[1,8,4],<u>[1,7,5]</u>]. Update the last triplet to be [max(2,1), max(5,7), max(3,5)] = [2,7,5]. triplets = [[2,5,3],[1,8,4],<u>[2,7,5]</u>]
|
||||
The target triplet [2,7,5] is now an element of triplets.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> triplets = [[3,4,5],[4,5,6]], target = [3,2,5]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is impossible to have [3,2,5] as an element because there is no 2 in any of the triplets.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> triplets = [[2,5,3],[2,3,4],[1,2,5],[5,2,3]], target = [5,5,5]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>Perform the following operations:
|
||||
- Choose the first and third triplets [<u>[2,5,3]</u>,[2,3,4],<u>[1,2,5]</u>,[5,2,3]]. Update the third triplet to be [max(2,1), max(5,2), max(3,5)] = [2,5,5]. triplets = [[2,5,3],[2,3,4],<u>[2,5,5]</u>,[5,2,3]].
|
||||
- Choose the third and fourth triplets [[2,5,3],[2,3,4],<u>[2,5,5]</u>,<u>[5,2,3]</u>]. Update the fourth triplet to be [max(2,5), max(5,2), max(5,3)] = [5,5,5]. triplets = [[2,5,3],[2,3,4],[2,5,5],<u>[5,5,5]</u>].
|
||||
The target triplet [5,5,5] is now an element of triplets.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= triplets.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>triplets[i].length == target.length == 3</code></li>
|
||||
<li><code>1 <= a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>, x, y, z <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>Given a binary string <code>s</code>, return <code>true</code><em> if the <strong>longest</strong> contiguous segment of </em><code>1</code>'<em>s is <strong>strictly longer</strong> than the <strong>longest</strong> contiguous segment of </em><code>0</code>'<em>s in </em><code>s</code>, or return <code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, in <code>s = "<u>11</u>01<u>000</u>10"</code> the longest continuous segment of <code>1</code>s has length <code>2</code>, and the longest continuous segment of <code>0</code>s has length <code>3</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Note that if there are no <code>0</code>'s, then the longest continuous segment of <code>0</code>'s is considered to have a length <code>0</code>. The same applies if there is no <code>1</code>'s.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "1101"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong>
|
||||
The longest contiguous segment of 1s has length 2: "<u>11</u>01"
|
||||
The longest contiguous segment of 0s has length 1: "11<u>0</u>1"
|
||||
The segment of 1s is longer, so return true.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "111000"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong>
|
||||
The longest contiguous segment of 1s has length 3: "<u>111</u>000"
|
||||
The longest contiguous segment of 0s has length 3: "111<u>000</u>"
|
||||
The segment of 1s is not longer, so return false.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "110100010"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong>
|
||||
The longest contiguous segment of 1s has length 2: "<u>11</u>0100010"
|
||||
The longest contiguous segment of 0s has length 3: "1101<u>000</u>10"
|
||||
The segment of 1s is not longer, so return false.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>You are given an integer array <code>nums</code>. The unique elements of an array are the elements that appear <strong>exactly once</strong> in the array.</p>
|
||||
|
||||
<p>Return <em>the <strong>sum</strong> of all the unique elements of </em><code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,2]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The unique elements are [1,3], and the sum is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1,1,1]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no unique elements, and the sum is 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> The unique elements are [1,2,3,4,5], and the sum is 15.
|
||||
</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>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>Given a string <code>s</code>, return <code>true</code> <em>if it is possible to split the string</em> <code>s</code> <em>into three <strong>non-empty</strong> palindromic substrings. Otherwise, return </em><code>false</code>.</p>
|
||||
|
||||
<p>A string is said to be palindrome if it the same string when reversed.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcbdd"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>"abcbdd" = "a" + "bcb" + "dd", and all three substrings are palindromes.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "bcbddxy"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation: </strong>s cannot be split into 3 palindromes.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= s.length <= 2000</code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>Given an integer <code>n</code> and an integer array <code>rounds</code>. We have a circular track which consists of <code>n</code> sectors labeled from <code>1</code> to <code>n</code>. A marathon will be held on this track, the marathon consists of <code>m</code> rounds. The <code>i<sup>th</sup></code> round starts at sector <code>rounds[i - 1]</code> and ends at sector <code>rounds[i]</code>. For example, round 1 starts at sector <code>rounds[0]</code> and ends at sector <code>rounds[1]</code></p>
|
||||
|
||||
<p>Return <em>an array of the most visited sectors</em> sorted in <strong>ascending</strong> order.</p>
|
||||
|
||||
<p>Notice that you circulate the track in ascending order of sector numbers in the counter-clockwise direction (See the first example).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/14/tmp.jpg" style="width: 433px; height: 341px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, rounds = [1,3,1,2]
|
||||
<strong>Output:</strong> [1,2]
|
||||
<strong>Explanation:</strong> The marathon starts at sector 1. The order of the visited sectors is as follows:
|
||||
1 --> 2 --> 3 (end of round 1) --> 4 --> 1 (end of round 2) --> 2 (end of round 3 and the marathon)
|
||||
We can see that both sectors 1 and 2 are visited twice and they are the most visited sectors. Sectors 3 and 4 are visited only once.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, rounds = [2,1,2,1,2,1,2,1,2]
|
||||
<strong>Output:</strong> [2]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7, rounds = [1,3,5,7]
|
||||
<strong>Output:</strong> [1,2,3,4,5,6,7]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 100</code></li>
|
||||
<li><code>1 <= m <= 100</code></li>
|
||||
<li><code>rounds.length == m + 1</code></li>
|
||||
<li><code>1 <= rounds[i] <= n</code></li>
|
||||
<li><code>rounds[i] != rounds[i + 1]</code> for <code>0 <= i < m</code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>You are given an integer matrix <code>isWater</code> of size <code>m x n</code> that represents a map of <strong>land</strong> and <strong>water</strong> cells.</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>isWater[i][j] == 0</code>, cell <code>(i, j)</code> is a <strong>land</strong> cell.</li>
|
||||
<li>If <code>isWater[i][j] == 1</code>, cell <code>(i, j)</code> is a <strong>water</strong> cell.</li>
|
||||
</ul>
|
||||
|
||||
<p>You must assign each cell a height in a way that follows these rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>The height of each cell must be non-negative.</li>
|
||||
<li>If the cell is a <strong>water</strong> cell, its height must be <code>0</code>.</li>
|
||||
<li>Any two adjacent cells must have an absolute height difference of <strong>at most</strong> <code>1</code>. A cell is adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching).</li>
|
||||
</ul>
|
||||
|
||||
<p>Find an assignment of heights such that the maximum height in the matrix is <strong>maximized</strong>.</p>
|
||||
|
||||
<p>Return <em>an integer matrix </em><code>height</code><em> of size </em><code>m x n</code><em> where </em><code>height[i][j]</code><em> is cell </em><code>(i, j)</code><em>'s height. If there are multiple solutions, return <strong>any</strong> of them</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-82045-am.png" style="width: 220px; height: 219px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> isWater = [[0,1],[0,0]]
|
||||
<strong>Output:</strong> [[1,0],[2,1]]
|
||||
<strong>Explanation:</strong> The image shows the assigned heights of each cell.
|
||||
The blue cell is the water cell, and the green cells are the land cells.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-82050-am.png" style="width: 300px; height: 296px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> isWater = [[0,0,1],[1,0,0],[0,0,0]]
|
||||
<strong>Output:</strong> [[1,1,0],[0,1,1],[1,2,2]]
|
||||
<strong>Explanation:</strong> A height of 2 is the maximum possible height of any assignment.
|
||||
Any height assignment that has a maximum height of 2 while still meeting the rules will also be accepted.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == isWater.length</code></li>
|
||||
<li><code>n == isWater[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 1000</code></li>
|
||||
<li><code>isWater[i][j]</code> is <code>0</code> or <code>1</code>.</li>
|
||||
<li>There is at least <strong>one</strong> water cell.</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>Given a <strong>zero-based permutation</strong> <code>nums</code> (<strong>0-indexed</strong>), build an array <code>ans</code> of the <strong>same length</strong> where <code>ans[i] = nums[nums[i]]</code> for each <code>0 <= i < nums.length</code> and return it.</p>
|
||||
|
||||
<p>A <strong>zero-based permutation</strong> <code>nums</code> is an array of <strong>distinct</strong> integers from <code>0</code> to <code>nums.length - 1</code> (<strong>inclusive</strong>).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,2,1,5,3,4]
|
||||
<strong>Output:</strong> [0,1,2,4,5,3]<strong>
|
||||
Explanation:</strong> The array ans is built as follows:
|
||||
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
|
||||
= [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
|
||||
= [0,1,2,4,5,3]</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,0,1,2,3,4]
|
||||
<strong>Output:</strong> [4,5,0,1,2,3]
|
||||
<strong>Explanation:</strong> The array ans is built as follows:
|
||||
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
|
||||
= [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]
|
||||
= [4,5,0,1,2,3]</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>0 <= nums[i] < nums.length</code></li>
|
||||
<li>The elements in <code>nums</code> are <strong>distinct</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Follow-up:</strong> Can you solve it without using an extra space (i.e., <code>O(1)</code> memory)?</p>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given two integers <code>memory1</code> and <code>memory2</code> representing the available memory in bits on two memory sticks. There is currently a faulty program running that consumes an increasing amount of memory every second.</p>
|
||||
|
||||
<p>At the <code>i<sup>th</sup></code> second (starting from 1), <code>i</code> bits of memory are allocated to the stick with <strong>more available memory</strong> (or from the first memory stick if both have the same available memory). If neither stick has at least <code>i</code> bits of available memory, the program <strong>crashes</strong>.</p>
|
||||
|
||||
<p>Return <em>an array containing </em><code>[crashTime, memory1<sub>crash</sub>, memory2<sub>crash</sub>]</code><em>, where </em><code>crashTime</code><em> is the time (in seconds) when the program crashed and </em><code>memory1<sub>crash</sub></code><em> and </em><code>memory2<sub>crash</sub></code><em> are the available bits of memory in the first and second sticks respectively</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> memory1 = 2, memory2 = 2
|
||||
<strong>Output:</strong> [3,1,0]
|
||||
<strong>Explanation:</strong> The memory is allocated as follows:
|
||||
- At the 1<sup>st</sup> second, 1 bit of memory is allocated to stick 1. The first stick now has 1 bit of available memory.
|
||||
- At the 2<sup>nd</sup> second, 2 bits of memory are allocated to stick 2. The second stick now has 0 bits of available memory.
|
||||
- At the 3<sup>rd</sup> second, the program crashes. The sticks have 1 and 0 bits available respectively.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> memory1 = 8, memory2 = 11
|
||||
<strong>Output:</strong> [6,0,4]
|
||||
<strong>Explanation:</strong> The memory is allocated as follows:
|
||||
- At the 1<sup>st</sup> second, 1 bit of memory is allocated to stick 2. The second stick now has 10 bit of available memory.
|
||||
- At the 2<sup>nd</sup> second, 2 bits of memory are allocated to stick 2. The second stick now has 8 bits of available memory.
|
||||
- At the 3<sup>rd</sup> second, 3 bits of memory are allocated to stick 1. The first stick now has 5 bits of available memory.
|
||||
- At the 4<sup>th</sup> second, 4 bits of memory are allocated to stick 2. The second stick now has 4 bits of available memory.
|
||||
- At the 5<sup>th</sup> second, 5 bits of memory are allocated to stick 1. The first stick now has 0 bits of available memory.
|
||||
- At the 6<sup>th</sup> second, the program crashes. The sticks have 0 and 4 bits available respectively.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= memory1, memory2 <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>Given <code>n</code> points on a 1-D plane, where the <code>i<sup>th</sup></code> point (from <code>0</code> to <code>n-1</code>) is at <code>x = i</code>, find the number of ways we can draw <strong>exactly</strong> <code>k</code> <strong>non-overlapping</strong> line segments such that each segment covers two or more points. The endpoints of each segment must have <strong>integral coordinates</strong>. The <code>k</code> line segments <strong>do not</strong> have to cover all <code>n</code> points, and they are <strong>allowed</strong> to share endpoints.</p>
|
||||
|
||||
<p>Return <em>the number of ways we can draw </em><code>k</code><em> non-overlapping line segments</em><em>.</em> Since this number can be huge, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/07/ex1.png" style="width: 179px; height: 222px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, k = 2
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The two line segments are shown in red and blue.
|
||||
The image above shows the 5 different ways {(0,2),(2,3)}, {(0,1),(1,3)}, {(0,1),(2,3)}, {(1,2),(2,3)}, {(0,1),(1,2)}.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, k = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The 3 ways are {(0,1)}, {(0,2)}, {(1,2)}.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 30, k = 7
|
||||
<strong>Output:</strong> 796297179
|
||||
<strong>Explanation:</strong> The total number of possible ways to draw 7 line segments is 3796297200. Taking this number modulo 10<sup>9</sup> + 7 gives us 796297179.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 1000</code></li>
|
||||
<li><code>1 <= k <= n-1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>A <strong>good meal</strong> is a meal that contains <strong>exactly two different food items</strong> with a sum of deliciousness equal to a power of two.</p>
|
||||
|
||||
<p>You can pick <strong>any</strong> two different foods to make a good meal.</p>
|
||||
|
||||
<p>Given an array of integers <code>deliciousness</code> where <code>deliciousness[i]</code> is the deliciousness of the <code>i<sup>th</sup></code> item of food, return <em>the number of different <strong>good meals</strong> you can make from this list modulo</em> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>Note that items with different indices are considered different even if they have the same deliciousness value.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> deliciousness = [1,3,5,7,9]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation: </strong>The good meals are (1,3), (1,7), (3,5) and, (7,9).
|
||||
Their respective sums are 4, 8, 8, and 16, all of which are powers of 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> deliciousness = [1,1,1,3,3,3,7]
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation: </strong>The good meals are (1,1) with 3 ways, (1,3) with 9 ways, and (1,7) with 3 ways.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= deliciousness.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= deliciousness[i] <= 2<sup>20</sup></code></li>
|
||||
</ul>
|
51
算法题(国内版)/problem (English)/奇偶树(English) [even-odd-tree].html
Normal file
51
算法题(国内版)/problem (English)/奇偶树(English) [even-odd-tree].html
Normal file
@@ -0,0 +1,51 @@
|
||||
<p>A binary tree is named <strong>Even-Odd</strong> if it meets the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The root of the binary tree is at level index <code>0</code>, its children are at level index <code>1</code>, their children are at level index <code>2</code>, etc.</li>
|
||||
<li>For every <strong>even-indexed</strong> level, all nodes at the level have <strong>odd</strong> integer values in <strong>strictly increasing</strong> order (from left to right).</li>
|
||||
<li>For every <b>odd-indexed</b> level, all nodes at the level have <b>even</b> integer values in <strong>strictly decreasing</strong> order (from left to right).</li>
|
||||
</ul>
|
||||
|
||||
<p>Given the <code>root</code> of a binary tree, <em>return </em><code>true</code><em> if the binary tree is <strong>Even-Odd</strong>, otherwise return </em><code>false</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/sample_1_1966.png" style="width: 362px; height: 229px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,10,4,3,null,7,9,12,8,6,null,null,2]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The node values on each level are:
|
||||
Level 0: [1]
|
||||
Level 1: [10,4]
|
||||
Level 2: [3,7,9]
|
||||
Level 3: [12,8,6,2]
|
||||
Since levels 0 and 2 are all odd and increasing and levels 1 and 3 are all even and decreasing, the tree is Even-Odd.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/sample_2_1966.png" style="width: 363px; height: 167px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,4,2,3,3,7]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The node values on each level are:
|
||||
Level 0: [5]
|
||||
Level 1: [4,2]
|
||||
Level 2: [3,3,7]
|
||||
Node values in level 2 must be in strictly increasing order, so the tree is not Even-Odd.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/22/sample_1_333_1966.png" style="width: 363px; height: 167px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,9,1,3,5,7]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> Node values in the level 1 should be even integers.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>Write an API that generates fancy sequences using the <code>append</code>, <code>addAll</code>, and <code>multAll</code> operations.</p>
|
||||
|
||||
<p>Implement the <code>Fancy</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>Fancy()</code> Initializes the object with an empty sequence.</li>
|
||||
<li><code>void append(val)</code> Appends an integer <code>val</code> to the end of the sequence.</li>
|
||||
<li><code>void addAll(inc)</code> Increments all existing values in the sequence by an integer <code>inc</code>.</li>
|
||||
<li><code>void multAll(m)</code> Multiplies all existing values in the sequence by an integer <code>m</code>.</li>
|
||||
<li><code>int getIndex(idx)</code> Gets the current value at index <code>idx</code> (0-indexed) of the sequence <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If the index is greater or equal than the length of the sequence, return <code>-1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["Fancy", "append", "addAll", "append", "multAll", "getIndex", "addAll", "append", "multAll", "getIndex", "getIndex", "getIndex"]
|
||||
[[], [2], [3], [7], [2], [0], [3], [10], [2], [0], [1], [2]]
|
||||
<strong>Output</strong>
|
||||
[null, null, null, null, null, 10, null, null, null, 26, 34, 20]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
Fancy fancy = new Fancy();
|
||||
fancy.append(2); // fancy sequence: [2]
|
||||
fancy.addAll(3); // fancy sequence: [2+3] -> [5]
|
||||
fancy.append(7); // fancy sequence: [5, 7]
|
||||
fancy.multAll(2); // fancy sequence: [5*2, 7*2] -> [10, 14]
|
||||
fancy.getIndex(0); // return 10
|
||||
fancy.addAll(3); // fancy sequence: [10+3, 14+3] -> [13, 17]
|
||||
fancy.append(10); // fancy sequence: [13, 17, 10]
|
||||
fancy.multAll(2); // fancy sequence: [13*2, 17*2, 10*2] -> [26, 34, 20]
|
||||
fancy.getIndex(0); // return 26
|
||||
fancy.getIndex(1); // return 34
|
||||
fancy.getIndex(2); // return 20
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= val, inc, m <= 100</code></li>
|
||||
<li><code>0 <= idx <= 10<sup>5</sup></code></li>
|
||||
<li>At most <code>10<sup>5</sup></code> calls total will be made to <code>append</code>, <code>addAll</code>, <code>multAll</code>, and <code>getIndex</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>There is a strange printer with the following two special requirements:</p>
|
||||
|
||||
<ul>
|
||||
<li>On each turn, the printer will print a solid rectangular pattern of a single color on the grid. This will cover up the existing colors in the rectangle.</li>
|
||||
<li>Once the printer has used a color for the above operation, <strong>the same color cannot be used again</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are given a <code>m x n</code> matrix <code>targetGrid</code>, where <code>targetGrid[row][col]</code> is the color in the position <code>(row, col)</code> of the grid.</p>
|
||||
|
||||
<p>Return <code>true</code><em> if it is possible to print the matrix </em><code>targetGrid</code><em>,</em><em> otherwise, return </em><code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/print1.jpg" style="width: 600px; height: 175px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> targetGrid = [[1,1,1,1],[1,2,2,1],[1,2,2,1],[1,1,1,1]]
|
||||
<strong>Output:</strong> true
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/print2.jpg" style="width: 600px; height: 367px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> targetGrid = [[1,1,1,1],[1,1,3,3],[1,1,3,4],[5,5,1,4]]
|
||||
<strong>Output:</strong> true
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> targetGrid = [[1,2,1],[2,1,2],[1,2,1]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is impossible to form targetGrid because it is not allowed to print the same color in different turns.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == targetGrid.length</code></li>
|
||||
<li><code>n == targetGrid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 60</code></li>
|
||||
<li><code>1 <= targetGrid[row][col] <= 60</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>You are given a positive integer <code>primeFactors</code>. You are asked to construct a positive integer <code>n</code> that satisfies the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The number of prime factors of <code>n</code> (not necessarily distinct) is <strong>at most</strong> <code>primeFactors</code>.</li>
|
||||
<li>The number of nice divisors of <code>n</code> is maximized. Note that a divisor of <code>n</code> is <strong>nice</strong> if it is divisible by every prime factor of <code>n</code>. For example, if <code>n = 12</code>, then its prime factors are <code>[2,2,3]</code>, then <code>6</code> and <code>12</code> are nice divisors, while <code>3</code> and <code>4</code> are not.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of nice divisors of</em> <code>n</code>. Since that number can be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>Note that a prime number is a natural number greater than <code>1</code> that is not a product of two smaller natural numbers. The prime factors of a number <code>n</code> is a list of prime numbers such that their product equals <code>n</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> primeFactors = 5
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> 200 is a valid value of n.
|
||||
It has 5 prime factors: [2,2,2,5,5], and it has 6 nice divisors: [10,20,40,50,100,200].
|
||||
There is not other value of n that has at most 5 prime factors and more nice divisors.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> primeFactors = 8
|
||||
<strong>Output:</strong> 18
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= primeFactors <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
|
||||
|
||||
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
|
||||
<strong>Output:</strong> 20
|
||||
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
|
||||
</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] <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= k < nums.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>The <strong>min-product</strong> of an array is equal to the <strong>minimum value</strong> in the array <strong>multiplied by</strong> the array's <strong>sum</strong>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the array <code>[3,2,5]</code> (minimum value is <code>2</code>) has a min-product of <code>2 * (3+2+5) = 2 * 10 = 20</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given an array of integers <code>nums</code>, return <em>the <strong>maximum min-product</strong> of any <strong>non-empty subarray</strong> of </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>Note that the min-product should be maximized <strong>before</strong> performing the modulo operation. Testcases are generated such that the maximum min-product <strong>without</strong> modulo will fit in a <strong>64-bit signed integer</strong>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,<u>2,3,2</u>]
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong> The maximum min-product is achieved with the subarray [2,3,2] (minimum value is 2).
|
||||
2 * (2+3+2) = 2 * 7 = 14.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,<u>3,3</u>,1,2]
|
||||
<strong>Output:</strong> 18
|
||||
<strong>Explanation:</strong> The maximum min-product is achieved with the subarray [3,3] (minimum value is 3).
|
||||
3 * (3+3) = 3 * 6 = 18.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,1,<u>5,6,4</u>,2]
|
||||
<strong>Output:</strong> 60
|
||||
<strong>Explanation:</strong> The maximum min-product is achieved with the subarray [5,6,4] (minimum value is 4).
|
||||
4 * (5+6+4) = 4 * 15 = 60.
|
||||
</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>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a string <code>word</code> that consists of digits and lowercase English letters.</p>
|
||||
|
||||
<p>You will replace every non-digit character with a space. For example, <code>"a123bc34d8ef34"</code> will become <code>" 123 34 8 34"</code>. Notice that you are left with some integers that are separated by at least one space: <code>"123"</code>, <code>"34"</code>, <code>"8"</code>, and <code>"34"</code>.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>different</strong> integers after performing the replacement operations on </em><code>word</code>.</p>
|
||||
|
||||
<p>Two integers are considered different if their decimal representations <strong>without any leading zeros</strong> are different.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "a<u>123</u>bc<u>34</u>d<u>8</u>ef<u>34</u>"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation: </strong>The three different integers are "123", "34", and "8". Notice that "34" is only counted once.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "leet<u>1234</u>code<u>234</u>"
|
||||
<strong>Output:</strong> 2
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "a<u>1</u>b<u>01</u>c<u>001</u>"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation: </strong>The three integers "1", "01", and "001" all represent the same integer because
|
||||
the leading zeros are ignored when comparing their decimal values.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 1000</code></li>
|
||||
<li><code>word</code> consists of digits and lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>You are given a string <code>num</code>, representing a large integer. Return <em>the <strong>largest-valued odd</strong> integer (as a string) that is a <strong>non-empty substring</strong> of </em><code>num</code><em>, or an empty string </em><code>""</code><em> if no odd integer exists</em>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "52"
|
||||
<strong>Output:</strong> "5"
|
||||
<strong>Explanation:</strong> The only non-empty substrings are "5", "2", and "52". "5" is the only odd number.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "4206"
|
||||
<strong>Output:</strong> ""
|
||||
<strong>Explanation:</strong> There are no odd numbers in "4206".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "35427"
|
||||
<strong>Output:</strong> "35427"
|
||||
<strong>Explanation:</strong> "35427" is already an odd number.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>num</code> only consists of digits and does not contain any leading zeros.</li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>Given an alphanumeric string <code>s</code>, return <em>the <strong>second largest</strong> numerical digit that appears in </em><code>s</code><em>, or </em><code>-1</code><em> if it does not exist</em>.</p>
|
||||
|
||||
<p>An <strong>alphanumeric</strong><strong> </strong>string is a string consisting of lowercase English letters and digits.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "dfa12321afd"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The digits that appear in s are [1, 2, 3]. The second largest digit is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abc1111"
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> The digits that appear in s are [1]. There is no second largest digit.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 500</code></li>
|
||||
<li><code>s</code> consists of only lowercase English letters and/or digits.</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>A string <code>s</code> is called <strong>good</strong> if there are no two different characters in <code>s</code> that have the same <strong>frequency</strong>.</p>
|
||||
|
||||
<p>Given a string <code>s</code>, return<em> the <strong>minimum</strong> number of characters you need to delete to make </em><code>s</code><em> <strong>good</strong>.</em></p>
|
||||
|
||||
<p>The <strong>frequency</strong> of a character in a string is the number of times it appears in the string. For example, in the string <code>"aab"</code>, the <strong>frequency</strong> of <code>'a'</code> is <code>2</code>, while the <strong>frequency</strong> of <code>'b'</code> is <code>1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aab"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> <code>s</code> is already good.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aaabbbcc"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> You can delete two 'b's resulting in the good string "aaabcc".
|
||||
Another way it to delete one 'b' and one 'c' resulting in the good string "aaabbc".</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "ceabaacb"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> You can delete both 'c's resulting in the good string "eabaab".
|
||||
Note that we only care about characters that are still in the string at the end (i.e. frequency of 0 is ignored).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> contains only lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,61 @@
|
||||
<p>You are given an array <code>tasks</code> where <code>tasks[i] = [actual<sub>i</sub>, minimum<sub>i</sub>]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>actual<sub>i</sub></code> is the actual amount of energy you <strong>spend to finish</strong> the <code>i<sup>th</sup></code> task.</li>
|
||||
<li><code>minimum<sub>i</sub></code> is the minimum amount of energy you <strong>require to begin</strong> the <code>i<sup>th</sup></code> task.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, if the task is <code>[10, 12]</code> and your current energy is <code>11</code>, you cannot start this task. However, if your current energy is <code>13</code>, you can complete this task, and your energy will be <code>3</code> after finishing it.</p>
|
||||
|
||||
<p>You can finish the tasks in <strong>any order</strong> you like.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> initial amount of energy you will need</em> <em>to finish all the tasks</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [[1,2],[2,4],[4,8]]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
Starting with 8 energy, we finish the tasks in the following order:
|
||||
- 3rd task. Now energy = 8 - 4 = 4.
|
||||
- 2nd task. Now energy = 4 - 2 = 2.
|
||||
- 1st task. Now energy = 2 - 1 = 1.
|
||||
Notice that even though we have leftover energy, starting with 7 energy does not work because we cannot do the 3rd task.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [[1,3],[2,4],[10,11],[10,12],[8,9]]
|
||||
<strong>Output:</strong> 32
|
||||
<strong>Explanation:</strong>
|
||||
Starting with 32 energy, we finish the tasks in the following order:
|
||||
- 1st task. Now energy = 32 - 1 = 31.
|
||||
- 2nd task. Now energy = 31 - 2 = 29.
|
||||
- 3rd task. Now energy = 29 - 10 = 19.
|
||||
- 4th task. Now energy = 19 - 10 = 9.
|
||||
- 5th task. Now energy = 9 - 8 = 1.</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [[1,7],[2,8],[3,9],[4,10],[5,11],[6,12]]
|
||||
<strong>Output:</strong> 27
|
||||
<strong>Explanation:</strong>
|
||||
Starting with 27 energy, we finish the tasks in the following order:
|
||||
- 5th task. Now energy = 27 - 5 = 22.
|
||||
- 2nd task. Now energy = 22 - 2 = 20.
|
||||
- 3rd task. Now energy = 20 - 3 = 17.
|
||||
- 1st task. Now energy = 17 - 1 = 16.
|
||||
- 4th task. Now energy = 16 - 4 = 12.
|
||||
- 6th task. Now energy = 12 - 6 = 6.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tasks.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= actual<sub>i</sub> <= minimum<sub>i</sub> <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user