mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-13 03:11:42 +08:00
add leetcode problem-cn part3
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<p>We have two special characters:</p>
|
||||
|
||||
<ul>
|
||||
<li>The first character can be represented by one bit <code>0</code>.</li>
|
||||
<li>The second character can be represented by two bits (<code>10</code> or <code>11</code>).</li>
|
||||
</ul>
|
||||
|
||||
<p>Given a binary array <code>bits</code> that ends with <code>0</code>, return <code>true</code> if the last character must be a one-bit character.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> bits = [1,0,0]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The only way to decode it is two-bit character and one-bit character.
|
||||
So the last character is one-bit character.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> bits = [1,1,1,0]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The only way to decode it is two-bit character and two-bit character.
|
||||
So the last character is not one-bit character.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= bits.length <= 1000</code></li>
|
||||
<li><code>bits[i]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
</ul>
|
48
算法题(国内版)/problem (English)/24 点游戏(English) [24-game].html
Normal file
48
算法题(国内版)/problem (English)/24 点游戏(English) [24-game].html
Normal file
@@ -0,0 +1,48 @@
|
||||
<p>You are given an integer array <code>cards</code> of length <code>4</code>. You have four cards, each containing a number in the range <code>[1, 9]</code>. You should arrange the numbers on these cards in a mathematical expression using the operators <code>['+', '-', '*', '/']</code> and the parentheses <code>'('</code> and <code>')'</code> to get the value 24.</p>
|
||||
|
||||
<p>You are restricted with the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>The division operator <code>'/'</code> represents real division, not integer division.
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>4 / (1 - 2 / 3) = 4 / (1 / 3) = 12</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>Every operation done is between two numbers. In particular, we cannot use <code>'-'</code> as a unary operator.
|
||||
<ul>
|
||||
<li>For example, if <code>cards = [1, 1, 1, 1]</code>, the expression <code>"-1 - 1 - 1 - 1"</code> is <strong>not allowed</strong>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>You cannot concatenate numbers together
|
||||
<ul>
|
||||
<li>For example, if <code>cards = [1, 2, 1, 2]</code>, the expression <code>"12 + 12"</code> is not valid.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>true</code> if you can get such expression that evaluates to <code>24</code>, and <code>false</code> otherwise.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> cards = [4,1,8,7]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> (8-4) * (7-1) = 24
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> cards = [1,2,1,2]
|
||||
<strong>Output:</strong> false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>cards.length == 4</code></li>
|
||||
<li><code>1 <= cards[i] <= 9</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>You are given a string <code>s</code> of length <code>n</code> where <code>s[i]</code> is either:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>'D'</code> means decreasing, or</li>
|
||||
<li><code>'I'</code> means increasing.</li>
|
||||
</ul>
|
||||
|
||||
<p>A permutation <code>perm</code> of <code>n + 1</code> integers of all the integers in the range <code>[0, n]</code> is called a <strong>valid permutation</strong> if for all valid <code>i</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>s[i] == 'D'</code>, then <code>perm[i] > perm[i + 1]</code>, and</li>
|
||||
<li>If <code>s[i] == 'I'</code>, then <code>perm[i] < perm[i + 1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of <strong>valid permutations</strong> </em><code>perm</code>. Since the answer may be 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 = "DID"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The 5 valid permutations of (0, 1, 2, 3) are:
|
||||
(1, 0, 3, 2)
|
||||
(2, 0, 3, 1)
|
||||
(2, 1, 3, 0)
|
||||
(3, 0, 2, 1)
|
||||
(3, 1, 2, 0)
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "D"
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == s.length</code></li>
|
||||
<li><code>1 <= n <= 200</code></li>
|
||||
<li><code>s[i]</code> is either <code>'I'</code> or <code>'D'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>In the world of Dota2, there are two parties: the Radiant and the Dire.</p>
|
||||
|
||||
<p>The Dota2 senate consists of senators coming from two parties. Now the Senate wants to decide on a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise <strong>one</strong> of the two rights:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Ban one senator's right:</strong> A senator can make another senator lose all his rights in this and all the following rounds.</li>
|
||||
<li><strong>Announce the victory:</strong> If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and decide on the change in the game.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given a string <code>senate</code> representing each senator's party belonging. The character <code>'R'</code> and <code>'D'</code> represent the Radiant party and the Dire party. Then if there are <code>n</code> senators, the size of the given string will be <code>n</code>.</p>
|
||||
|
||||
<p>The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.</p>
|
||||
|
||||
<p>Suppose every senator is smart enough and will play the best strategy for his own party. Predict which party will finally announce the victory and change the Dota2 game. The output should be <code>"Radiant"</code> or <code>"Dire"</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> senate = "RD"
|
||||
<strong>Output:</strong> "Radiant"
|
||||
<strong>Explanation:</strong>
|
||||
The first senator comes from Radiant and he can just ban the next senator's right in round 1.
|
||||
And the second senator can't exercise any rights anymore since his right has been banned.
|
||||
And in round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> senate = "RDD"
|
||||
<strong>Output:</strong> "Dire"
|
||||
<strong>Explanation:</strong>
|
||||
The first senator comes from Radiant and he can just ban the next senator's right in round 1.
|
||||
And the second senator can't exercise any rights anymore since his right has been banned.
|
||||
And the third senator comes from Dire and he can ban the first senator's right in round 1.
|
||||
And in round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == senate.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>senate[i]</code> is either <code>'R'</code> or <code>'D'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of <strong>good subarrays</strong> of </em><code>nums</code>.</p>
|
||||
|
||||
<p>A <strong>good array</strong> is an array where the number of different integers in that array is exactly <code>k</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>[1,2,3,1,2]</code> has <code>3</code> different integers: <code>1</code>, <code>2</code>, and <code>3</code>.</li>
|
||||
</ul>
|
||||
|
||||
<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,2,1,2,3], k = 2
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1,3,4], k = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i], k <= nums.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, modify the array in the following way:</p>
|
||||
|
||||
<ul>
|
||||
<li>choose an index <code>i</code> and replace <code>nums[i]</code> with <code>-nums[i]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You should apply this process exactly <code>k</code> times. You may choose the same index <code>i</code> multiple times.</p>
|
||||
|
||||
<p>Return <em>the largest possible sum of the array after modifying it in this way</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,2,3], k = 1
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> Choose index 1 and nums becomes [4,-2,3].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,-1,0,2], k = 3
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> Choose indices (1, 2, 2) and nums becomes [3,1,0,2].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,-3,-1,5,-4], k = 2
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> Choose indices (1, 4) and nums becomes [2,3,-1,5,4].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-100 <= nums[i] <= 100</code></li>
|
||||
<li><code>1 <= k <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>There are <code>n</code> cities connected by some number of flights. You are given an array <code>flights</code> where <code>flights[i] = [from<sub>i</sub>, to<sub>i</sub>, price<sub>i</sub>]</code> indicates that there is a flight from city <code>from<sub>i</sub></code> to city <code>to<sub>i</sub></code> with cost <code>price<sub>i</sub></code>.</p>
|
||||
|
||||
<p>You are also given three integers <code>src</code>, <code>dst</code>, and <code>k</code>, return <em><strong>the cheapest price</strong> from </em><code>src</code><em> to </em><code>dst</code><em> with at most </em><code>k</code><em> stops. </em>If there is no such route, return<em> </em><code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-3drawio.png" style="width: 332px; height: 392px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
|
||||
<strong>Output:</strong> 700
|
||||
<strong>Explanation:</strong>
|
||||
The graph is shown above.
|
||||
The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost 100 + 600 = 700.
|
||||
Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2 stops.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-1drawio.png" style="width: 332px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
|
||||
<strong>Output:</strong> 200
|
||||
<strong>Explanation:</strong>
|
||||
The graph is shown above.
|
||||
The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost 100 + 100 = 200.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-2drawio.png" style="width: 332px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
|
||||
<strong>Output:</strong> 500
|
||||
<strong>Explanation:</strong>
|
||||
The graph is shown above.
|
||||
The optimal path with no stops from city 0 to 2 is marked in red and has cost 500.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>0 <= flights.length <= (n * (n - 1) / 2)</code></li>
|
||||
<li><code>flights[i].length == 3</code></li>
|
||||
<li><code>0 <= from<sub>i</sub>, to<sub>i</sub> < n</code></li>
|
||||
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
|
||||
<li><code>1 <= price<sub>i</sub> <= 10<sup>4</sup></code></li>
|
||||
<li>There will not be any multiple flights between two cities.</li>
|
||||
<li><code>0 <= src, dst, k < n</code></li>
|
||||
<li><code>src != dst</code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>You are given a binary array <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>A <strong>k-bit flip</strong> is choosing a <strong>subarray</strong> of length <code>k</code> from <code>nums</code> and simultaneously changing every <code>0</code> in the subarray to <code>1</code>, and every <code>1</code> in the subarray to <code>0</code>.</p>
|
||||
|
||||
<p>Return <em>the minimum number of <strong>k-bit flips</strong> required so that there is no </em><code>0</code><em> in the array</em>. If it is not possible, return <code>-1</code>.</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 = [0,1,0], k = 1
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Flip nums[0], then flip nums[2].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,0], k = 2
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> No matter how we flip subarrays of size 2, we cannot make the array become [1,1,1].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,0,0,1,0,1,1,0], k = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
Flip nums[0],nums[1],nums[2]: nums becomes [1,1,1,1,0,1,1,0]
|
||||
Flip nums[4],nums[5],nums[6]: nums becomes [1,1,1,1,1,0,0,0]
|
||||
Flip nums[5],nums[6],nums[7]: nums becomes [1,1,1,1,1,1,1,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k <= nums.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>For an integer array <code>nums</code>, an <strong>inverse pair</strong> is a pair of integers <code>[i, j]</code> where <code>0 <= i < j < nums.length</code> and <code>nums[i] > nums[j]</code>.</p>
|
||||
|
||||
<p>Given two integers n and k, return the number of different arrays consist of numbers from <code>1</code> to <code>n</code> such that there are exactly <code>k</code> <strong>inverse pairs</strong>. Since the answer can be huge, 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> n = 3, k = 0
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pairs.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, k = 1
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
<li><code>0 <= k <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>You are given a string expression representing a Lisp-like expression to return the integer value of.</p>
|
||||
|
||||
<p>The syntax for these expressions is given as follows.</p>
|
||||
|
||||
<ul>
|
||||
<li>An expression is either an integer, let expression, add expression, mult expression, or an assigned variable. Expressions always evaluate to a single integer.</li>
|
||||
<li>(An integer could be positive or negative.)</li>
|
||||
<li>A let expression takes the form <code>"(let v<sub>1</sub> e<sub>1</sub> v<sub>2</sub> e<sub>2</sub> ... v<sub>n</sub> e<sub>n</sub> expr)"</code>, where let is always the string <code>"let"</code>, then there are one or more pairs of alternating variables and expressions, meaning that the first variable <code>v<sub>1</sub></code> is assigned the value of the expression <code>e<sub>1</sub></code>, the second variable <code>v<sub>2</sub></code> is assigned the value of the expression <code>e<sub>2</sub></code>, and so on sequentially; and then the value of this let expression is the value of the expression <code>expr</code>.</li>
|
||||
<li>An add expression takes the form <code>"(add e<sub>1</sub> e<sub>2</sub>)"</code> where add is always the string <code>"add"</code>, there are always two expressions <code>e<sub>1</sub></code>, <code>e<sub>2</sub></code> and the result is the addition of the evaluation of <code>e<sub>1</sub></code> and the evaluation of <code>e<sub>2</sub></code>.</li>
|
||||
<li>A mult expression takes the form <code>"(mult e<sub>1</sub> e<sub>2</sub>)"</code> where mult is always the string <code>"mult"</code>, there are always two expressions <code>e<sub>1</sub></code>, <code>e<sub>2</sub></code> and the result is the multiplication of the evaluation of e1 and the evaluation of e2.</li>
|
||||
<li>For this question, we will use a smaller subset of variable names. A variable starts with a lowercase letter, then zero or more lowercase letters or digits. Additionally, for your convenience, the names <code>"add"</code>, <code>"let"</code>, and <code>"mult"</code> are protected and will never be used as variable names.</li>
|
||||
<li>Finally, there is the concept of scope. When an expression of a variable name is evaluated, within the context of that evaluation, the innermost scope (in terms of parentheses) is checked first for the value of that variable, and then outer scopes are checked sequentially. It is guaranteed that every expression is legal. Please see the examples for more details on the scope.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> expression = "(let x 2 (mult x (let x 3 y 4 (add x y))))"
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong> In the expression (add x y), when checking for the value of the variable x,
|
||||
we check from the innermost scope to the outermost in the context of the variable we are trying to evaluate.
|
||||
Since x = 3 is found first, the value of x is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> expression = "(let x 3 x 2 x)"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Assignment in let statements is processed sequentially.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> expression = "(let x 1 y 2 x (add x y) (add x y))"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The first (add x y) evaluates as 3, and is assigned to x.
|
||||
The second (add x y) evaluates as 3+2 = 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= expression.length <= 2000</code></li>
|
||||
<li>There are no leading or trailing spaces in <code>expression</code>.</li>
|
||||
<li>All tokens are separated by a single space in <code>expression</code>.</li>
|
||||
<li>The answer and all intermediate calculations of that answer are guaranteed to fit in a <strong>32-bit</strong> integer.</li>
|
||||
<li>The expression is guaranteed to be legal and evaluate to an integer.</li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>Given the <code>root</code> of an n-ary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
|
||||
|
||||
<p>Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png" style="width: 100%; max-width: 300px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,null,3,2,4,null,5,6]
|
||||
<strong>Output:</strong> [1,3,5,6,2,4]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png" style="width: 296px; height: 241px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
|
||||
<strong>Output:</strong> [1,2,3,6,7,11,14,4,8,12,5,9,13,10]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
|
||||
<li>The height of the n-ary tree is less than or equal to <code>1000</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
|
@@ -0,0 +1,30 @@
|
||||
<p>Given the <code>root</code> of an n-ary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
|
||||
|
||||
<p>Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png" style="width: 100%; max-width: 300px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,null,3,2,4,null,5,6]
|
||||
<strong>Output:</strong> [5,6,3,2,4,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png" style="width: 296px; height: 241px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
|
||||
<strong>Output:</strong> [2,6,14,11,7,3,12,8,4,13,9,10,5,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
|
||||
<li>The height of the n-ary tree is less than or equal to <code>1000</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
|
@@ -0,0 +1,30 @@
|
||||
<p>Given an n-ary tree, return the <em>level order</em> traversal of its nodes' values.</p>
|
||||
|
||||
<p><em>Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png" style="width: 100%; max-width: 300px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,null,3,2,4,null,5,6]
|
||||
<strong>Output:</strong> [[1],[3,2,4],[5,6]]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png" style="width: 296px; height: 241px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
|
||||
<strong>Output:</strong> [[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The height of the n-ary tree is less than or equal to <code>1000</code></li>
|
||||
<li>The total number of nodes is between <code>[0, 10<sup>4</sup>]</code></li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>Given a n-ary tree, find its maximum depth.</p>
|
||||
|
||||
<p>The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.</p>
|
||||
|
||||
<p><em>Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png" style="width: 100%; max-width: 300px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,null,3,2,4,null,5,6]
|
||||
<strong>Output:</strong> 3
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png" style="width: 296px; height: 241px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
|
||||
<strong>Output:</strong> 5
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The total number of nodes is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
|
||||
<li>The depth of the n-ary tree is less than or equal to <code>1000</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>There are <code>8</code> prison cells in a row and each cell is either occupied or vacant.</p>
|
||||
|
||||
<p>Each day, whether the cell is occupied or vacant changes according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.</li>
|
||||
<li>Otherwise, it becomes vacant.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.</p>
|
||||
|
||||
<p>You are given an integer array <code>cells</code> where <code>cells[i] == 1</code> if the <code>i<sup>th</sup></code> cell is occupied and <code>cells[i] == 0</code> if the <code>i<sup>th</sup></code> cell is vacant, and you are given an integer <code>n</code>.</p>
|
||||
|
||||
<p>Return the state of the prison after <code>n</code> days (i.e., <code>n</code> such changes described above).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> cells = [0,1,0,1,1,0,0,1], n = 7
|
||||
<strong>Output:</strong> [0,0,1,1,0,0,0,0]
|
||||
<strong>Explanation:</strong> The following table summarizes the state of the prison on each day:
|
||||
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
|
||||
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
|
||||
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
|
||||
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
|
||||
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
|
||||
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
|
||||
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
|
||||
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> cells = [1,0,0,1,0,0,1,0], n = 1000000000
|
||||
<strong>Output:</strong> [0,0,1,1,1,1,1,0]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>cells.length == 8</code></li>
|
||||
<li><code>cells[i]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>We can use run-length encoding (i.e., <strong>RLE</strong>) to encode a sequence of integers. In a run-length encoded array of even length <code>encoding</code> (<strong>0-indexed</strong>), for all even <code>i</code>, <code>encoding[i]</code> tells us the number of times that the non-negative integer value <code>encoding[i + 1]</code> is repeated in the sequence.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the sequence <code>arr = [8,8,8,5,5]</code> can be encoded to be <code>encoding = [3,8,2,5]</code>. <code>encoding = [3,8,0,9,2,5]</code> and <code>encoding = [2,8,1,8,2,5]</code> are also valid <strong>RLE</strong> of <code>arr</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given a run-length encoded array, design an iterator that iterates through it.</p>
|
||||
|
||||
<p>Implement the <code>RLEIterator</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>RLEIterator(int[] encoded)</code> Initializes the object with the encoded array <code>encoded</code>.</li>
|
||||
<li><code>int next(int n)</code> Exhausts the next <code>n</code> elements and returns the last element exhausted in this way. If there is no element left to exhaust, return <code>-1</code> instead.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["RLEIterator", "next", "next", "next", "next"]
|
||||
[[[3, 8, 0, 9, 2, 5]], [2], [1], [1], [2]]
|
||||
<strong>Output</strong>
|
||||
[null, 8, 8, 5, -1]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
RLEIterator rLEIterator = new RLEIterator([3, 8, 0, 9, 2, 5]); // This maps to the sequence [8,8,8,5,5].
|
||||
rLEIterator.next(2); // exhausts 2 terms of the sequence, returning 8. The remaining sequence is now [8, 5, 5].
|
||||
rLEIterator.next(1); // exhausts 1 term of the sequence, returning 8. The remaining sequence is now [5, 5].
|
||||
rLEIterator.next(1); // exhausts 1 term of the sequence, returning 5. The remaining sequence is now [5].
|
||||
rLEIterator.next(2); // exhausts 2 terms, returning -1. This is because the first term exhausted was 5,
|
||||
but the second term did not exist. Since the last term exhausted does not exist, we return -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= encoding.length <= 1000</code></li>
|
||||
<li><code>encoding.length</code> is even.</li>
|
||||
<li><code>0 <= encoding[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
<li>At most <code>1000</code> calls will be made to <code>next</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>A Range Module is a module that tracks ranges of numbers. Design a data structure to track the ranges represented as <strong>half-open intervals</strong> and query about them.</p>
|
||||
|
||||
<p>A <strong>half-open interval</strong> <code>[left, right)</code> denotes all the real numbers <code>x</code> where <code>left <= x < right</code>.</p>
|
||||
|
||||
<p>Implement the <code>RangeModule</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>RangeModule()</code> Initializes the object of the data structure.</li>
|
||||
<li><code>void addRange(int left, int right)</code> Adds the <strong>half-open interval</strong> <code>[left, right)</code>, tracking every real number in that interval. Adding an interval that partially overlaps with currently tracked numbers should add any numbers in the interval <code>[left, right)</code> that are not already tracked.</li>
|
||||
<li><code>boolean queryRange(int left, int right)</code> Returns <code>true</code> if every real number in the interval <code>[left, right)</code> is currently being tracked, and <code>false</code> otherwise.</li>
|
||||
<li><code>void removeRange(int left, int right)</code> Stops tracking every real number currently being tracked in the <strong>half-open interval</strong> <code>[left, right)</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["RangeModule", "addRange", "removeRange", "queryRange", "queryRange", "queryRange"]
|
||||
[[], [10, 20], [14, 16], [10, 14], [13, 15], [16, 17]]
|
||||
<strong>Output</strong>
|
||||
[null, null, null, true, false, true]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
RangeModule rangeModule = new RangeModule();
|
||||
rangeModule.addRange(10, 20);
|
||||
rangeModule.removeRange(14, 16);
|
||||
rangeModule.queryRange(10, 14); // return True,(Every number in [10, 14) is being tracked)
|
||||
rangeModule.queryRange(13, 15); // return False,(Numbers like 14, 14.03, 14.17 in [13, 15) are not being tracked)
|
||||
rangeModule.queryRange(16, 17); // return True, (The number 16 in [16, 17) is still being tracked, despite the remove operation)
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= left < right <= 10<sup>9</sup></code></li>
|
||||
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>addRange</code>, <code>queryRange</code>, and <code>removeRange</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size <code>groupSize</code>, and consists of <code>groupSize</code> consecutive cards.</p>
|
||||
|
||||
<p>Given an integer array <code>hand</code> where <code>hand[i]</code> is the value written on the <code>i<sup>th</sup></code> card and an integer <code>groupSize</code>, return <code>true</code> if she can rearrange the cards, or <code>false</code> otherwise.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> hand = [1,2,3,4,5], groupSize = 4
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> Alice's hand can not be rearranged into groups of 4.
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= hand.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= hand[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= groupSize <= hand.length</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Note:</strong> This question is the same as 1296: <a href="https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/" target="_blank">https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/</a></p>
|
@@ -0,0 +1,20 @@
|
||||
<p>Given an integer array <code>nums</code>, <em>find three numbers whose product is maximum and return the maximum product</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> nums = [1,2,3]
|
||||
<strong>Output:</strong> 6
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> 24
|
||||
</pre><p><strong>Example 3:</strong></p>
|
||||
<pre><strong>Input:</strong> nums = [-1,-2,-3]
|
||||
<strong>Output:</strong> -6
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-1000 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, find three non-overlapping subarrays of length <code>k</code> with maximum sum and return them.</p>
|
||||
|
||||
<p>Return the result as a list of indices representing the starting position of each interval (<strong>0-indexed</strong>). If there are multiple answers, return the lexicographically smallest one.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1,2,6,7,5,1], k = 2
|
||||
<strong>Output:</strong> [0,3,5]
|
||||
<strong>Explanation:</strong> Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
|
||||
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1,2,1,2,1,2,1], k = 2
|
||||
<strong>Output:</strong> [0,2,4]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] < 2<sup>16</sup></code></li>
|
||||
<li><code>1 <= k <= floor(nums.length / 3)</code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>Given an integer array <code>arr</code>, and an integer <code>target</code>, return the number of tuples <code>i, j, k</code> such that <code>i < j < k</code> and <code>arr[i] + arr[j] + arr[k] == target</code>.</p>
|
||||
|
||||
<p>As the answer can be very 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> arr = [1,1,2,2,3,3,4,4,5,5], target = 8
|
||||
<strong>Output:</strong> 20
|
||||
<strong>Explanation: </strong>
|
||||
Enumerating by the values (arr[i], arr[j], arr[k]):
|
||||
(1, 2, 5) occurs 8 times;
|
||||
(1, 3, 4) occurs 8 times;
|
||||
(2, 2, 4) occurs 2 times;
|
||||
(2, 3, 3) occurs 2 times.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [1,1,2,2,2,2], target = 5
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation: </strong>
|
||||
arr[i] = 1, arr[j] = arr[k] = 2 occurs 12 times:
|
||||
We choose one 1 from [1,1] in 2 ways,
|
||||
and two 2s from [2,2,2,2] in 6 ways.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= arr.length <= 3000</code></li>
|
||||
<li><code>0 <= arr[i] <= 100</code></li>
|
||||
<li><code>0 <= target <= 300</code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>You are given an array <code>arr</code> which consists of only zeros and ones, divide the array into <strong>three non-empty parts</strong> such that all of these parts represent the same binary value.</p>
|
||||
|
||||
<p>If it is possible, return any <code>[i, j]</code> with <code>i + 1 < j</code>, such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>arr[0], arr[1], ..., arr[i]</code> is the first part,</li>
|
||||
<li><code>arr[i + 1], arr[i + 2], ..., arr[j - 1]</code> is the second part, and</li>
|
||||
<li><code>arr[j], arr[j + 1], ..., arr[arr.length - 1]</code> is the third part.</li>
|
||||
<li>All three parts have equal binary values.</li>
|
||||
</ul>
|
||||
|
||||
<p>If it is not possible, return <code>[-1, -1]</code>.</p>
|
||||
|
||||
<p>Note that the entire part is used when considering what binary value it represents. For example, <code>[1,1,0]</code> represents <code>6</code> in decimal, not <code>3</code>. Also, leading zeros <strong>are allowed</strong>, so <code>[0,1,1]</code> and <code>[1,1]</code> represent the same value.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> arr = [1,0,1,0,1]
|
||||
<strong>Output:</strong> [0,3]
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> arr = [1,1,0,1,1]
|
||||
<strong>Output:</strong> [-1,-1]
|
||||
</pre><p><strong>Example 3:</strong></p>
|
||||
<pre><strong>Input:</strong> arr = [1,1,0,0,1]
|
||||
<strong>Output:</strong> [0,2]
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= arr.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>arr[i]</code> is <code>0</code> or <code>1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>You are given an <code>n x n</code> <code>grid</code> where we place some <code>1 x 1 x 1</code> cubes that are axis-aligned with the <code>x</code>, <code>y</code>, and <code>z</code> axes.</p>
|
||||
|
||||
<p>Each value <code>v = grid[i][j]</code> represents a tower of <code>v</code> cubes placed on top of the cell <code>(i, j)</code>.</p>
|
||||
|
||||
<p>We view the projection of these cubes onto the <code>xy</code>, <code>yz</code>, and <code>zx</code> planes.</p>
|
||||
|
||||
<p>A <strong>projection</strong> is like a shadow, that maps our <strong>3-dimensional</strong> figure to a <strong>2-dimensional</strong> plane. We are viewing the "shadow" when looking at the cubes from the top, the front, and the side.</p>
|
||||
|
||||
<p>Return <em>the total area of all three projections</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/02/shadow.png" style="width: 800px; height: 214px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,2],[3,4]]
|
||||
<strong>Output:</strong> 17
|
||||
<strong>Explanation:</strong> Here are the three projections ("shadows") of the shape made with each axis-aligned plane.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[2]]
|
||||
<strong>Output:</strong> 5
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,0],[0,2]]
|
||||
<strong>Output:</strong> 8
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given an <code>n x n</code> <code>grid</code> where you have placed some <code>1 x 1 x 1</code> cubes. Each value <code>v = grid[i][j]</code> represents a tower of <code>v</code> cubes placed on top of cell <code>(i, j)</code>.</p>
|
||||
|
||||
<p>After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.</p>
|
||||
|
||||
<p>Return <em>the total surface area of the resulting shapes</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong> The bottom face of each shape counts toward its surface area.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/08/tmp-grid2.jpg" style="width: 162px; height: 162px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,2],[3,4]]
|
||||
<strong>Output:</strong> 34
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/08/tmp-grid4.jpg" style="width: 242px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,1],[1,0,1],[1,1,1]]
|
||||
<strong>Output:</strong> 32
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/08/tmp-grid5.jpg" style="width: 242px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[2,2,2],[2,1,2],[2,2,2]]
|
||||
<strong>Output:</strong> 46
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,24 @@
|
||||
<p>Given an integer array <code>nums</code>, return <em>the largest perimeter of a triangle with a non-zero area, formed from three of these lengths</em>. If it is impossible to form any triangle of a non-zero area, return <code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,2]
|
||||
<strong>Output:</strong> 5
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1]
|
||||
<strong>Output:</strong> 0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,18 @@
|
||||
<p>Given a positive integer <code>n</code>, find <em>the smallest integer which has exactly the same digits existing in the integer</em> <code>n</code> <em>and is greater in value than</em> <code>n</code>. If no such positive integer exists, return <code>-1</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the returned integer should fit in <strong>32-bit integer</strong>, if there is a valid answer but it does not fit in <strong>32-bit integer</strong>, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> n = 12
|
||||
<strong>Output:</strong> 21
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> n = 21
|
||||
<strong>Output:</strong> -1
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
<p>Given an <code>n x n</code> array of integers <code>matrix</code>, return <em>the <strong>minimum sum</strong> of any <strong>falling path</strong> through</em> <code>matrix</code>.</p>
|
||||
|
||||
<p>A <strong>falling path</strong> starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position <code>(row, col)</code> will be <code>(row + 1, col - 1)</code>, <code>(row + 1, col)</code>, or <code>(row + 1, col + 1)</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/03/failing1-grid.jpg" style="width: 499px; height: 500px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> matrix = [[2,1,3],[6,5,4],[7,8,9]]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> There are two falling paths with a minimum sum as shown.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/03/failing2-grid.jpg" style="width: 164px; height: 365px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> matrix = [[-19,57],[-40,-5]]
|
||||
<strong>Output:</strong> -59
|
||||
<strong>Explanation:</strong> The falling path with a minimum sum is shown.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == matrix.length == matrix[i].length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>-100 <= matrix[i][j] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>Given a string s, return <em>the number of <strong>distinct non-empty subsequences</strong> of</em> <code>s</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
A <strong>subsequence</strong> of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., <code>"ace"</code> is a subsequence of <code>"<u>a</u>b<u>c</u>d<u>e</u>"</code> while <code>"aec"</code> is not.
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abc"
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> The 7 distinct subsequences are "a", "b", "c", "ab", "ac", "bc", and "abc".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aba"
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The 6 distinct subsequences are "a", "b", "ab", "aa", "ba", and "aba".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aaa"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The 3 distinct subsequences are "a", "aa" and "aaa".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 2000</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>You are given an <code>m x n</code> integer array <code>grid</code> where <code>grid[i][j]</code> could be:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>1</code> representing the starting square. There is exactly one starting square.</li>
|
||||
<li><code>2</code> representing the ending square. There is exactly one ending square.</li>
|
||||
<li><code>0</code> representing empty squares we can walk over.</li>
|
||||
<li><code>-1</code> representing obstacles that we cannot walk over.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/02/lc-unique1.jpg" style="width: 324px; height: 245px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We have the following two paths:
|
||||
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
|
||||
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/02/lc-unique2.jpg" style="width: 324px; height: 245px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We have the following four paths:
|
||||
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
|
||||
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
|
||||
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
|
||||
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/02/lc-unique3-.jpg" style="width: 164px; height: 165px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,1],[2,0]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no path that walks over every empty square exactly once.
|
||||
Note that the starting and ending square can be anywhere in the grid.
|
||||
</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 <= 20</code></li>
|
||||
<li><code>1 <= m * n <= 20</code></li>
|
||||
<li><code>-1 <= grid[i][j] <= 2</code></li>
|
||||
<li>There is exactly one starting cell and one ending cell.</li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>Given two integers <code>a</code> and <code>b</code>, return <strong>any</strong> string <code>s</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>s</code> has length <code>a + b</code> and contains exactly <code>a</code> <code>'a'</code> letters, and exactly <code>b</code> <code>'b'</code> letters,</li>
|
||||
<li>The substring <code>'aaa'</code> does not occur in <code>s</code>, and</li>
|
||||
<li>The substring <code>'bbb'</code> does not occur in <code>s</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 1, b = 2
|
||||
<strong>Output:</strong> "abb"
|
||||
<strong>Explanation:</strong> "abb", "bab" and "bba" are all correct answers.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 4, b = 1
|
||||
<strong>Output:</strong> "aabaa"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= a, b <= 100</code></li>
|
||||
<li>It is guaranteed such an <code>s</code> exists for the given <code>a</code> and <code>b</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>Given a positive integer <code>n</code>, return the number of the integers in the range <code>[0, n]</code> whose binary representations <strong>do not</strong> contain consecutive ones.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
Here are the non-negative integers <= 5 with their corresponding binary representations:
|
||||
0 : 0
|
||||
1 : 1
|
||||
2 : 10
|
||||
3 : 11
|
||||
4 : 100
|
||||
5 : 101
|
||||
Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1
|
||||
<strong>Output:</strong> 2
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> 3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.</p>
|
||||
|
||||
<p>You need to help them find out their <b>common interest</b> with the <b>least list index sum</b>. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"]
|
||||
<strong>Output:</strong> ["Shogun"]
|
||||
<strong>Explanation:</strong> The only restaurant they both like is "Shogun".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["KFC","Shogun","Burger King"]
|
||||
<strong>Output:</strong> ["Shogun"]
|
||||
<strong>Explanation:</strong> The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= list1.length, list2.length <= 1000</code></li>
|
||||
<li><code>1 <= list1[i].length, list2[i].length <= 30</code></li>
|
||||
<li><code>list1[i]</code> and <code>list2[i]</code> consist of spaces <code>' '</code> and English letters.</li>
|
||||
<li>All the stings of <code>list1</code> are <strong>unique</strong>.</li>
|
||||
<li>All the stings of <code>list2</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,27 @@
|
||||
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of <strong>steps</strong> required to make</em> <code>word1</code> <em>and</em> <code>word2</code> <em>the same</em>.</p>
|
||||
|
||||
<p>In one <strong>step</strong>, you can delete exactly one character in either string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word1 = "sea", word2 = "eat"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word1 = "leetcode", word2 = "etco"
|
||||
<strong>Output:</strong> 4
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word1.length, word2.length <= 500</code></li>
|
||||
<li><code>word1</code> and <code>word2</code> consist of only lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>Given two strings <code>s1</code> and <code>s2</code>, return <em>the lowest <strong>ASCII</strong> sum of deleted characters to make two strings equal</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "sea", s2 = "eat"
|
||||
<strong>Output:</strong> 231
|
||||
<strong>Explanation:</strong> Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.
|
||||
Deleting "t" from "eat" adds 116 to the sum.
|
||||
At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "delete", s2 = "leet"
|
||||
<strong>Output:</strong> 403
|
||||
<strong>Explanation:</strong> Deleting "dee" from "delete" to turn the string into "let",
|
||||
adds 100[d] + 101[e] + 101[e] to the sum.
|
||||
Deleting "e" from "leet" adds 101[e] to the sum.
|
||||
At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403.
|
||||
If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s1.length, s2.length <= 1000</code></li>
|
||||
<li><code>s1</code> and <code>s2</code> consist of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>Given an integer array <code>nums</code> and two integers <code>firstLen</code> and <code>secondLen</code>, return <em>the maximum sum of elements in two non-overlapping <strong>subarrays</strong> with lengths </em><code>firstLen</code><em> and </em><code>secondLen</code>.</p>
|
||||
|
||||
<p>The array with length <code>firstLen</code> could occur before or after the array with length <code>secondLen</code>, but they have to be non-overlapping.</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 = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2
|
||||
<strong>Output:</strong> 20
|
||||
<strong>Explanation:</strong> One choice of subarrays is [9] with length 1, and [6,5] with length 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2
|
||||
<strong>Output:</strong> 29
|
||||
<strong>Explanation:</strong> One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,5,6,0,9,5,0,3,8], firstLen = 4, secondLen = 3
|
||||
<strong>Output:</strong> 31
|
||||
<strong>Explanation:</strong> One choice of subarrays is [5,6,0,9] with length 4, and [0,3,8] with length 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= firstLen, secondLen <= 1000</code></li>
|
||||
<li><code>2 <= firstLen + secondLen <= 1000</code></li>
|
||||
<li><code>firstLen + secondLen <= nums.length <= 1000</code></li>
|
||||
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,23 @@
|
||||
<p>A <strong>sentence</strong> is a string of single-space separated words where each word consists only of lowercase letters.</p>
|
||||
|
||||
<p>A word is <strong>uncommon</strong> if it appears exactly once in one of the sentences, and <strong>does not appear</strong> in the other sentence.</p>
|
||||
|
||||
<p>Given two <strong>sentences</strong> <code>s1</code> and <code>s2</code>, return <em>a list of all the <strong>uncommon words</strong></em>. You may return the answer in <strong>any order</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> s1 = "this apple is sweet", s2 = "this apple is sour"
|
||||
<strong>Output:</strong> ["sweet","sour"]
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> s1 = "apple apple", s2 = "banana"
|
||||
<strong>Output:</strong> ["banana"]
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s1.length, s2.length <= 200</code></li>
|
||||
<li><code>s1</code> and <code>s2</code> consist of lowercase English letters and spaces.</li>
|
||||
<li><code>s1</code> and <code>s2</code> do not have leading or trailing spaces.</li>
|
||||
<li>All the words in <code>s1</code> and <code>s2</code> are separated by a single space.</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>A company is planning to interview <code>2n</code> people. Given the array <code>costs</code> where <code>costs[i] = [aCost<sub>i</sub>, bCost<sub>i</sub>]</code>, the cost of flying the <code>i<sup>th</sup></code> person to city <code>a</code> is <code>aCost<sub>i</sub></code>, and the cost of flying the <code>i<sup>th</sup></code> person to city <code>b</code> is <code>bCost<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Return <em>the minimum cost to fly every person to a city</em> such that exactly <code>n</code> people arrive in each city.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> costs = [[10,20],[30,200],[400,50],[30,20]]
|
||||
<strong>Output:</strong> 110
|
||||
<strong>Explanation: </strong>
|
||||
The first person goes to city A for a cost of 10.
|
||||
The second person goes to city A for a cost of 30.
|
||||
The third person goes to city B for a cost of 50.
|
||||
The fourth person goes to city B for a cost of 20.
|
||||
|
||||
The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
|
||||
<strong>Output:</strong> 1859
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]
|
||||
<strong>Output:</strong> 3086
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 * n == costs.length</code></li>
|
||||
<li><code>2 <= costs.length <= 100</code></li>
|
||||
<li><code>costs.length</code> is even.</li>
|
||||
<li><code>1 <= aCost<sub>i</sub>, bCost<sub>i</sub> <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,26 @@
|
||||
<p>Given the <code>root</code> of a Binary Search Tree and a target number <code>k</code>, return <em><code>true</code> if there exist two elements in the BST such that their sum is equal to the given target</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/sum_tree_1.jpg" style="width: 400px; height: 229px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,3,6,2,4,null,7], k = 9
|
||||
<strong>Output:</strong> true
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/sum_tree_2.jpg" style="width: 400px; height: 229px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,3,6,2,4,null,7], k = 28
|
||||
<strong>Output:</strong> false
|
||||
</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>4</sup>]</code>.</li>
|
||||
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
|
||||
<li><code>root</code> is guaranteed to be a <strong>valid</strong> binary search tree.</li>
|
||||
<li><code>-10<sup>5</sup> <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>You are asked to cut off all the trees in a forest for a golf event. The forest is represented as an <code>m x n</code> matrix. In this matrix:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0</code> means the cell cannot be walked through.</li>
|
||||
<li><code>1</code> represents an empty cell that can be walked through.</li>
|
||||
<li>A number greater than <code>1</code> represents a tree in a cell that can be walked through, and this number is the tree's height.</li>
|
||||
</ul>
|
||||
|
||||
<p>In one step, you can walk in any of the four directions: north, east, south, and west. If you are standing in a cell with a tree, you can choose whether to cut it off.</p>
|
||||
|
||||
<p>You must cut off the trees in order from shortest to tallest. When you cut off a tree, the value at its cell becomes <code>1</code> (an empty cell).</p>
|
||||
|
||||
<p>Starting from the point <code>(0, 0)</code>, return <em>the minimum steps you need to walk to cut off all the trees</em>. If you cannot cut off all the trees, return <code>-1</code>.</p>
|
||||
|
||||
<p>You are guaranteed that no two trees have the same height, and there is at least one tree needs to be cut off.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/trees1.jpg" style="width: 242px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> forest = [[1,2,3],[0,0,4],[7,6,5]]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> Following the path above allows you to cut off the trees from shortest to tallest in 6 steps.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/trees2.jpg" style="width: 242px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> forest = [[1,2,3],[0,0,0],[7,6,5]]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> The trees in the bottom row cannot be accessed as the middle row is blocked.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> forest = [[2,3,4],[0,0,5],[8,7,6]]
|
||||
<strong>Output:</strong> 6
|
||||
<b>Explanation:</b> You can follow the same path as Example 1 to cut off all the trees.
|
||||
Note that you can cut off the first tree at (0, 0) before making any steps.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == forest.length</code></li>
|
||||
<li><code>n == forest[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 50</code></li>
|
||||
<li><code>0 <= forest[i][j] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>Nearly everyone has used the <a href="https://en.wikipedia.org/wiki/Multiplication_table" target="_blank">Multiplication Table</a>. The multiplication table of size <code>m x n</code> is an integer matrix <code>mat</code> where <code>mat[i][j] == i * j</code> (<strong>1-indexed</strong>).</p>
|
||||
|
||||
<p>Given three integers <code>m</code>, <code>n</code>, and <code>k</code>, return <em>the </em><code>k<sup>th</sup></code><em> smallest element in the </em><code>m x n</code><em> multiplication table</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/02/multtable1-grid.jpg" style="width: 500px; height: 254px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 3, n = 3, k = 5
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The 5<sup>th</sup> smallest number is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/02/multtable2-grid.jpg" style="width: 493px; height: 293px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 2, n = 3, k = 6
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The 6<sup>th</sup> smallest number is 6.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m, n <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= k <= m * n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than </em><code>k</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,5,2,6], k = 100
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> The 8 subarrays that have product less than 100 are:
|
||||
[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]
|
||||
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3], k = 0
|
||||
<strong>Output:</strong> 0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
<li><code>0 <= k <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day, and an integer <code>fee</code> representing a transaction fee.</p>
|
||||
|
||||
<p>Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.</p>
|
||||
|
||||
<p><strong>Note:</strong> You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prices = [1,3,2,8,4,9], fee = 2
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> The maximum profit can be achieved by:
|
||||
- Buying at prices[0] = 1
|
||||
- Selling at prices[3] = 8
|
||||
- Buying at prices[4] = 4
|
||||
- Selling at prices[5] = 9
|
||||
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prices = [1,3,7,5,10,3], fee = 3
|
||||
<strong>Output:</strong> 6
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= prices.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= prices[i] < 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= fee < 5 * 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>Given an integer array of even length <code>arr</code>, return <code>true</code><em> if it is possible to reorder </em><code>arr</code><em> such that </em><code>arr[2 * i + 1] = 2 * arr[2 * i]</code><em> for every </em><code>0 <= i < len(arr) / 2</code><em>, or </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [3,1,3,6]
|
||||
<strong>Output:</strong> false
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [2,1,2,6]
|
||||
<strong>Output:</strong> false
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [4,-2,2,-4]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= arr.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>arr.length</code> is even.</li>
|
||||
<li><code>-10<sup>5</sup> <= arr[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>Given an array of integers <code>nums</code> which is sorted in ascending order, and an integer <code>target</code>, write a function to search <code>target</code> in <code>nums</code>. If <code>target</code> exists, then return its index. Otherwise, return <code>-1</code>.</p>
|
||||
|
||||
<p>You must write an algorithm with <code>O(log n)</code> runtime complexity.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,0,3,5,9,12], target = 9
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> 9 exists in nums and its index is 4
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,0,3,5,9,12], target = 2
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> 2 does not exist in nums so return -1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> < nums[i], target < 10<sup>4</sup></code></li>
|
||||
<li>All the integers in <code>nums</code> are <strong>unique</strong>.</li>
|
||||
<li><code>nums</code> is sorted in ascending order.</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given the <code>root</code> node of a binary search tree (BST) and a <code>value</code> to insert into the tree. Return <em>the root node of the BST after the insertion</em>. It is <strong>guaranteed</strong> that the new value does not exist in the original BST.</p>
|
||||
|
||||
<p><strong>Notice</strong> that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return <strong>any of them</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/05/insertbst.jpg" style="width: 752px; height: 221px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [4,2,7,1,3], val = 5
|
||||
<strong>Output:</strong> [4,2,7,1,3,5]
|
||||
<strong>Explanation:</strong> Another accepted tree is:
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/05/bst.jpg" style="width: 352px; height: 301px;" />
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [40,20,60,10,30,50,70], val = 25
|
||||
<strong>Output:</strong> [40,20,60,10,30,50,70,null,null,25]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
|
||||
<strong>Output:</strong> [4,2,7,1,3,5]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>-10<sup>8</sup> <= Node.val <= 10<sup>8</sup></code></li>
|
||||
<li>All the values <code>Node.val</code> are <strong>unique</strong>.</li>
|
||||
<li><code>-10<sup>8</sup> <= val <= 10<sup>8</sup></code></li>
|
||||
<li>It's <strong>guaranteed</strong> that <code>val</code> does not exist in the original BST.</li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>You are given the <code>root</code> of a binary search tree (BST) and an integer <code>val</code>.</p>
|
||||
|
||||
<p>Find the node in the BST that the node's value equals <code>val</code> and return the subtree rooted with that node. If such a node does not exist, return <code>null</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/12/tree1.jpg" style="width: 422px; height: 302px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [4,2,7,1,3], val = 2
|
||||
<strong>Output:</strong> [2,1,3]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/12/tree2.jpg" style="width: 422px; height: 302px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [4,2,7,1,3], val = 5
|
||||
<strong>Output:</strong> []
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 5000]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>7</sup></code></li>
|
||||
<li><code>root</code> is a binary search tree.</li>
|
||||
<li><code>1 <= val <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>Given the <code>root</code> node of a binary search tree and two integers <code>low</code> and <code>high</code>, return <em>the sum of values of all nodes with a value in the <strong>inclusive</strong> range </em><code>[low, high]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/bst1.jpg" style="width: 400px; height: 222px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [10,5,15,3,7,null,18], low = 7, high = 15
|
||||
<strong>Output:</strong> 32
|
||||
<strong>Explanation:</strong> Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/bst2.jpg" style="width: 400px; height: 335px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
|
||||
<strong>Output:</strong> 23
|
||||
<strong>Explanation:</strong> Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 2 * 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
|
||||
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,27 @@
|
||||
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum difference between the values of any two different nodes in the tree</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [4,2,6,1,3]
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,0,48,null,null,12,49]
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[2, 100]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Note:</strong> This question is the same as 530: <a href="https://leetcode.com/problems/minimum-absolute-difference-in-bst/" target="_blank">https://leetcode.com/problems/minimum-absolute-difference-in-bst/</a></p>
|
@@ -0,0 +1,30 @@
|
||||
<p>Given the <code>root</code> of a binary tree, the value of a target node <code>target</code>, and an integer <code>k</code>, return <em>an array of the values of all nodes that have a distance </em><code>k</code><em> from the target node.</em></p>
|
||||
|
||||
<p>You can return the answer in <strong>any order</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/28/sketch0.png" style="width: 500px; height: 429px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2
|
||||
<strong>Output:</strong> [7,4,1]
|
||||
Explanation: The nodes that are a distance 2 from the target node (with value 5) have values 7, 4, and 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1], target = 1, k = 3
|
||||
<strong>Output:</strong> []
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 500]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 500</code></li>
|
||||
<li>All the values <code>Node.val</code> are <strong>unique</strong>.</li>
|
||||
<li><code>target</code> is the value of one of the nodes in the tree.</li>
|
||||
<li><code>0 <= k <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly <code>two</code> or <code>zero</code> sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. More formally, the property <code>root.val = min(root.left.val, root.right.val)</code> always holds.</p>
|
||||
|
||||
<p>Given such a binary tree, you need to output the <b>second minimum</b> value in the set made of all the nodes' value in the whole tree.</p>
|
||||
|
||||
<p>If no such second minimum value exists, output -1 instead.</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/15/smbt1.jpg" style="width: 431px; height: 302px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [2,2,5,null,null,5,7]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The smallest value is 2, the second smallest value is 5.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/15/smbt2.jpg" style="width: 321px; height: 182px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [2,2,2]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> The smallest value is 2, but there isn't any second smallest value.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 25]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 2<sup>31</sup> - 1</code></li>
|
||||
<li><code>root.val == min(root.left.val, root.right.val)</code> for each internal node of the tree.</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>Given the <code>root</code> of a binary tree, return <em>the same tree where every subtree (of the given tree) not containing a </em><code>1</code><em> has been removed</em>.</p>
|
||||
|
||||
<p>A subtree of a node <code>node</code> is <code>node</code> plus every node that is a descendant of <code>node</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_2.png" style="width: 500px; height: 140px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,null,0,0,1]
|
||||
<strong>Output:</strong> [1,null,0,null,1]
|
||||
<strong>Explanation:</strong>
|
||||
Only the red nodes satisfy the property "every subtree not containing a 1".
|
||||
The diagram on the right represents the answer.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_1.png" style="width: 500px; height: 115px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,0,1,0,0,0,1]
|
||||
<strong>Output:</strong> [1,null,1,null,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/05/1028.png" style="width: 500px; height: 134px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,1,0,1,1,0,1,0]
|
||||
<strong>Output:</strong> [1,1,0,1,1,null,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 200]</code>.</li>
|
||||
<li><code>Node.val</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>Given the <code>root</code> of a binary tree, return <em>the <strong>maximum width</strong> of the given tree</em>.</p>
|
||||
|
||||
<p>The <strong>maximum width</strong> of a tree is the maximum <strong>width</strong> among all levels.</p>
|
||||
|
||||
<p>The <strong>width</strong> of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes that would be present in a complete binary tree extending down to that level are also counted into the length calculation.</p>
|
||||
|
||||
<p>It is <strong>guaranteed</strong> that the answer will in the range of a <strong>32-bit</strong> signed integer.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/width1-tree.jpg" style="width: 359px; height: 302px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The maximum width exists in the third level with length 4 (5,3,null,9).
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/14/maximum-width-of-binary-tree-v3.jpg" style="width: 442px; height: 422px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,3,2,5,null,null,9,6,null,7]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> The maximum width exists in the fourth level with length 7 (6,null,null,null,null,null,7).
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/width3-tree.jpg" style="width: 289px; height: 299px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,3,2,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The maximum width exists in the second level with length 2 (3,2).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 3000]</code>.</li>
|
||||
<li><code>-100 <= Node.val <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>Given the <code>root</code> of a binary tree, return <em>the sum of every tree node's <strong>tilt</strong>.</em></p>
|
||||
|
||||
<p>The <strong>tilt</strong> of a tree node is the <strong>absolute difference</strong> between the sum of all left subtree node <strong>values</strong> and all right subtree node <strong>values</strong>. If a node does not have a left child, then the sum of the left subtree node <strong>values</strong> is treated as <code>0</code>. The rule is similar if the node does not have a right child.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/20/tilt1.jpg" style="width: 712px; height: 182px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
Tilt of node 2 : |0-0| = 0 (no children)
|
||||
Tilt of node 3 : |0-0| = 0 (no children)
|
||||
Tilt of node 1 : |2-3| = 1 (left subtree is just left child, so sum is 2; right subtree is just right child, so sum is 3)
|
||||
Sum of every tilt : 0 + 0 + 1 = 1
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/20/tilt2.jpg" style="width: 800px; height: 203px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [4,2,9,3,5,null,7]
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong>
|
||||
Tilt of node 3 : |0-0| = 0 (no children)
|
||||
Tilt of node 5 : |0-0| = 0 (no children)
|
||||
Tilt of node 7 : |0-0| = 0 (no children)
|
||||
Tilt of node 2 : |3-5| = 2 (left subtree is just left child, so sum is 3; right subtree is just right child, so sum is 5)
|
||||
Tilt of node 9 : |0-7| = 7 (no left child, so sum is 0; right subtree is just right child, so sum is 7)
|
||||
Tilt of node 4 : |(3+5+2)-(9+7)| = |10-16| = 6 (left subtree values are 3, 5, and 2, which sums to 10; right subtree values are 9 and 7, which sums to 16)
|
||||
Sum of every tilt : 0 + 0 + 0 + 2 + 7 + 6 = 15
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/20/tilt3.jpg" style="width: 800px; height: 293px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [21,7,14,1,1,2,2,3,3]
|
||||
<strong>Output:</strong> 9
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>-1000 <= Node.val <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>Given the <code>root</code> of a binary tree, calculate the <strong>vertical order traversal</strong> of the binary tree.</p>
|
||||
|
||||
<p>For each node at position <code>(row, col)</code>, its left and right children will be at positions <code>(row + 1, col - 1)</code> and <code>(row + 1, col + 1)</code> respectively. The root of the tree is at <code>(0, 0)</code>.</p>
|
||||
|
||||
<p>The <strong>vertical order traversal</strong> of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values.</p>
|
||||
|
||||
<p>Return <em>the <strong>vertical order traversal</strong> of the binary tree</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree1.jpg" style="width: 431px; height: 304px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
|
||||
<strong>Output:</strong> [[9],[3,15],[20],[7]]
|
||||
<strong>Explanation:</strong>
|
||||
Column -1: Only node 9 is in this column.
|
||||
Column 0: Nodes 3 and 15 are in this column in that order from top to bottom.
|
||||
Column 1: Only node 20 is in this column.
|
||||
Column 2: Only node 7 is in this column.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree2.jpg" style="width: 512px; height: 304px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
|
||||
<strong>Output:</strong> [[4],[2],[1,5,6],[3],[7]]
|
||||
<strong>Explanation:</strong>
|
||||
Column -2: Only node 4 is in this column.
|
||||
Column -1: Only node 2 is in this column.
|
||||
Column 0: Nodes 1, 5, and 6 are in this column.
|
||||
1 is at the top, so it comes first.
|
||||
5 and 6 are at the same position (2, 0), so we order them by their value, 5 before 6.
|
||||
Column 1: Only node 3 is in this column.
|
||||
Column 2: Only node 7 is in this column.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree3.jpg" style="width: 512px; height: 304px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3,4,6,5,7]
|
||||
<strong>Output:</strong> [[4],[2],[1,5,6],[3],[7]]
|
||||
<strong>Explanation:</strong>
|
||||
This case is the exact same as example 2, but with nodes 5 and 6 swapped.
|
||||
Note that the solution remains the same since 5 and 6 are in the same location and should be ordered by their values.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>Given the <code>root</code> of a binary tree with unique values and the values of two different nodes of the tree <code>x</code> and <code>y</code>, return <code>true</code> <em>if the nodes corresponding to the values </em><code>x</code><em> and </em><code>y</code><em> in the tree are <strong>cousins</strong>, or </em><code>false</code><em> otherwise.</em></p>
|
||||
|
||||
<p>Two nodes of a binary tree are <strong>cousins</strong> if they have the same depth with different parents.</p>
|
||||
|
||||
<p>Note that in a binary tree, the root node is at the depth <code>0</code>, and children of each depth <code>k</code> node are at the depth <code>k + 1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/12/q1248-01.png" style="width: 304px; height: 270px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3,4], x = 4, y = 3
|
||||
<strong>Output:</strong> false
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/12/q1248-02.png" style="width: 334px; height: 266px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3,null,4,null,5], x = 5, y = 4
|
||||
<strong>Output:</strong> true
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/13/q1248-03.png" style="width: 267px; height: 258px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3,null,4], x = 2, y = 3
|
||||
<strong>Output:</strong> false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[2, 100]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 100</code></li>
|
||||
<li>Each node has a <strong>unique</strong> value.</li>
|
||||
<li><code>x != y</code></li>
|
||||
<li><code>x</code> and <code>y</code> are exist in the tree.</li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>Given the <code>root</code> of a binary tree, determine if it is a <em>complete binary tree</em>.</p>
|
||||
|
||||
<p>In a <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">complete binary tree</a></strong>, every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/complete-binary-tree-1.png" style="width: 180px; height: 145px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3,4,5,6]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Every level before the last is full (ie. levels with node-values {1} and {2, 3}), and all nodes in the last level ({4, 5, 6}) are as far left as possible.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/complete-binary-tree-2.png" style="width: 200px; height: 145px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The node with value 7 isn't as far left as possible.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 100]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,25 @@
|
||||
Given the <code>root</code> of a binary tree, return <em>the average value of the nodes on each level in the form of an array</em>. Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/avg1-tree.jpg" style="width: 277px; height: 302px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
|
||||
<strong>Output:</strong> [3.00000,14.50000,11.00000]
|
||||
Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
|
||||
Hence return [3, 14.5, 11].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/avg2-tree.jpg" style="width: 292px; height: 302px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [3,9,20,15,7]
|
||||
<strong>Output:</strong> [3.00000,14.50000,11.00000]
|
||||
</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>4</sup>]</code>.</li>
|
||||
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>Given two integers <code>left</code> and <code>right</code>, return <em>the <strong>count</strong> of numbers in the <strong>inclusive</strong> range </em><code>[left, right]</code><em> having a <strong>prime number of set bits</strong> in their binary representation</em>.</p>
|
||||
|
||||
<p>Recall that the <strong>number of set bits</strong> an integer has is the number of <code>1</code>'s present when written in binary.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>21</code> written in binary is <code>10101</code>, which has <code>3</code> set bits.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> left = 6, right = 10
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
6 -> 110 (2 set bits, 2 is prime)
|
||||
7 -> 111 (3 set bits, 3 is prime)
|
||||
8 -> 1000 (1 set bit, 1 is not prime)
|
||||
9 -> 1001 (2 set bits, 2 is prime)
|
||||
10 -> 1010 (2 set bits, 2 is prime)
|
||||
4 numbers have a prime number of set bits.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> left = 10, right = 15
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
10 -> 1010 (2 set bits, 2 is prime)
|
||||
11 -> 1011 (3 set bits, 3 is prime)
|
||||
12 -> 1100 (2 set bits, 2 is prime)
|
||||
13 -> 1101 (3 set bits, 3 is prime)
|
||||
14 -> 1110 (3 set bits, 3 is prime)
|
||||
15 -> 1111 (4 set bits, 4 is not prime)
|
||||
5 numbers have a prime number of set bits.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= left <= right <= 10<sup>6</sup></code></li>
|
||||
<li><code>0 <= right - left <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
40
算法题(国内版)/problem (English)/二进制间距(English) [binary-gap].html
Normal file
40
算法题(国内版)/problem (English)/二进制间距(English) [binary-gap].html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>Given a positive integer <code>n</code>, find and return <em>the <strong>longest distance</strong> between any two <strong>adjacent</strong> </em><code>1</code><em>'s in the binary representation of </em><code>n</code><em>. If there are no two adjacent </em><code>1</code><em>'s, return </em><code>0</code><em>.</em></p>
|
||||
|
||||
<p>Two <code>1</code>'s are <strong>adjacent</strong> if there are only <code>0</code>'s separating them (possibly no <code>0</code>'s). The <b>distance</b> between two <code>1</code>'s is the absolute difference between their bit positions. For example, the two <code>1</code>'s in <code>"1001"</code> have a distance of 3.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 22
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> 22 in binary is "10110".
|
||||
The first adjacent pair of 1's is "<u>1</u>0<u>1</u>10" with a distance of 2.
|
||||
The second adjacent pair of 1's is "10<u>11</u>0" with a distance of 1.
|
||||
The answer is the largest of these two distances, which is 2.
|
||||
Note that "<u>1</u>01<u>1</u>0" is not a valid pair since there is a 1 separating the two 1's underlined.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 8
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> 8 in binary is "1000".
|
||||
There are not any adjacent pairs of 1's in the binary representation of 8, so we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> 5 in binary is "101".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The binary representation of 5 is: 101
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The binary representation of 7 is: 111.</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 11
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The binary representation of 11 is: 1011.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>Given two strings <code>s</code> and <code>goal</code>, return <code>true</code><em> if you can swap two letters in </em><code>s</code><em> so the result is equal to </em><code>goal</code><em>, otherwise, return </em><code>false</code><em>.</em></p>
|
||||
|
||||
<p>Swapping letters is defined as taking two indices <code>i</code> and <code>j</code> (0-indexed) such that <code>i != j</code> and swapping the characters at <code>s[i]</code> and <code>s[j]</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, swapping at indices <code>0</code> and <code>2</code> in <code>"abcd"</code> results in <code>"cbad"</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "ab", goal = "ba"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "ab", goal = "ab"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The only letters you can swap are s[0] = 'a' and s[1] = 'b', which results in "ba" != goal.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aa", goal = "aa"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> You can swap s[0] = 'a' and s[1] = 'a' to get "aa", which is equal to goal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length, goal.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> and <code>goal</code> consist of lowercase letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>Given a string <code>s</code>, reverse the string according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>All the characters that are not English letters remain in the same position.</li>
|
||||
<li>All the English letters (lowercase or uppercase) should be reversed.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>s</code><em> after reversing it</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> s = "ab-cd"
|
||||
<strong>Output:</strong> "dc-ba"
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> s = "a-bC-dEf-ghIj"
|
||||
<strong>Output:</strong> "j-Ih-gfE-dCba"
|
||||
</pre><p><strong>Example 3:</strong></p>
|
||||
<pre><strong>Input:</strong> s = "Test1ng-Leet=code-Q!"
|
||||
<strong>Output:</strong> "Qedo1ct-eeLg=ntse-T!"
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> consists of characters with ASCII values in the range <code>[33, 122]</code>.</li>
|
||||
<li><code>s</code> does not contain <code>'\"'</code> or <code>'\\'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>We run a preorder depth-first search (DFS) on the <code>root</code> of a binary tree.</p>
|
||||
|
||||
<p>At each node in this traversal, we output <code>D</code> dashes (where <code>D</code> is the depth of this node), then we output the value of this node. If the depth of a node is <code>D</code>, the depth of its immediate child is <code>D + 1</code>. The depth of the <code>root</code> node is <code>0</code>.</p>
|
||||
|
||||
<p>If a node has only one child, that child is guaranteed to be <strong>the left child</strong>.</p>
|
||||
|
||||
<p>Given the output <code>traversal</code> of this traversal, recover the tree and return <em>its</em> <code>root</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/08/recover-a-tree-from-preorder-traversal.png" style="width: 320px; height: 200px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> traversal = "1-2--3--4-5--6--7"
|
||||
<strong>Output:</strong> [1,2,5,3,4,6,7]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/11/screen-shot-2019-04-10-at-114101-pm.png" style="width: 256px; height: 250px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> traversal = "1-2--3---4-5--6---7"
|
||||
<strong>Output:</strong> [1,2,5,3,null,6,null,4,null,7]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/11/screen-shot-2019-04-10-at-114955-pm.png" style="width: 276px; height: 250px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> traversal = "1-401--349---90--88"
|
||||
<strong>Output:</strong> [1,401,null,349,88,90]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the original tree is in the range <code>[1, 1000]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>You are given the <code>root</code> of a binary tree where each node has a value in the range <code>[0, 25]</code> representing the letters <code>'a'</code> to <code>'z'</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>lexicographically smallest</strong> string that starts at a leaf of this tree and ends at the root</em>.</p>
|
||||
|
||||
<p>As a reminder, any shorter prefix of a string is <strong>lexicographically smaller</strong>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>"ab"</code> is lexicographically smaller than <code>"aba"</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A leaf of a node is a node that has no children.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/01/30/tree1.png" style="width: 534px; height: 358px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [0,1,2,3,4,3,4]
|
||||
<strong>Output:</strong> "dba"
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/01/30/tree2.png" style="width: 534px; height: 358px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [25,1,3,1,3,0,2]
|
||||
<strong>Output:</strong> "adz"
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/01/tree3.png" style="height: 490px; width: 468px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [2,2,1,null,1,0,null,0]
|
||||
<strong>Output:</strong> "abc"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 8500]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 25</code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>You are given the <code>root</code> of a binary tree where each node has a value <code>0</code> or <code>1</code>. Each root-to-leaf path represents a binary number starting with the most significant bit.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if the path is <code>0 -> 1 -> 1 -> 0 -> 1</code>, then this could represent <code>01101</code> in binary, which is <code>13</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>For all leaves in the tree, consider the numbers represented by the path from the root to that leaf. Return <em>the sum of these numbers</em>.</p>
|
||||
|
||||
<p>The test cases are generated so that the answer fits in a <strong>32-bits</strong> integer.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/04/sum-of-root-to-leaf-binary-numbers.png" style="width: 400px; height: 263px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,0,1,0,1,0,1]
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation: </strong>(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [0]
|
||||
<strong>Output:</strong> 0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
|
||||
<li><code>Node.val</code> is <code>0</code> or <code>1</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>You have an initial <strong>power</strong> of <code>power</code>, an initial <strong>score</strong> of <code>0</code>, and a bag of <code>tokens</code> where <code>tokens[i]</code> is the value of the <code>i<sup>th</sup></code> token (0-indexed).</p>
|
||||
|
||||
<p>Your goal is to maximize your total <strong>score</strong> by potentially playing each token in one of two ways:</p>
|
||||
|
||||
<ul>
|
||||
<li>If your current <strong>power</strong> is at least <code>tokens[i]</code>, you may play the <code>i<sup>th</sup></code> token face up, losing <code>tokens[i]</code> <strong>power</strong> and gaining <code>1</code> <strong>score</strong>.</li>
|
||||
<li>If your current <strong>score</strong> is at least <code>1</code>, you may play the <code>i<sup>th</sup></code> token face down, gaining <code>tokens[i]</code> <strong>power</strong> and losing <code>1</code> <strong>score</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Each token may be played <strong>at most</strong> once and <strong>in any order</strong>. You do <strong>not</strong> have to play all the tokens.</p>
|
||||
|
||||
<p>Return <em>the largest possible <strong>score</strong> you can achieve after playing any number of tokens</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tokens = [100], power = 50
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation</strong><strong>:</strong> Playing the only token in the bag is impossible because you either have too little power or too little score.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tokens = [100,200], power = 150
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Play the 0<sup>th</sup> token (100) face up, your power becomes 50 and score becomes 1.
|
||||
There is no need to play the 1<sup>st</sup> token since you cannot play it face up to add to your score.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tokens = [100,200,300,400], power = 200
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Play the tokens in this order to get a score of 2:
|
||||
1. Play the 0<sup>th</sup> token (100) face up, your power becomes 100 and score becomes 1.
|
||||
2. Play the 3<sup>rd</sup> token (400) face down, your power becomes 500 and score becomes 0.
|
||||
3. Play the 1<sup>st</sup> token (200) face up, your power becomes 300 and score becomes 1.
|
||||
4. Play the 2<sup>nd </sup>token (300) face up, your power becomes 0 and score becomes 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= tokens.length <= 1000</code></li>
|
||||
<li><code>0 <= tokens[i], power < 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>Given a characters array <code>tasks</code>, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time. For each unit of time, the CPU could complete either one task or just be idle.</p>
|
||||
|
||||
<p>However, there is a non-negative integer <code>n</code> that represents the cooldown period between two <b>same tasks</b> (the same letter in the array), that is that there must be at least <code>n</code> units of time between any two same tasks.</p>
|
||||
|
||||
<p>Return <em>the least number of units of times that the CPU will take to finish all the given tasks</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = ["A","A","A","B","B","B"], n = 2
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
A -> B -> idle -> A -> B -> idle -> A -> B
|
||||
There is at least 2 units of time between any two same tasks.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = ["A","A","A","B","B","B"], n = 0
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> On this case any permutation of size 6 would work since n = 0.
|
||||
["A","A","A","B","B","B"]
|
||||
["A","B","A","B","A","B"]
|
||||
["B","B","B","A","A","A"]
|
||||
...
|
||||
And so on.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
One possible solution is
|
||||
A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= task.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>tasks[i]</code> is upper-case English letter.</li>
|
||||
<li>The integer <code>n</code> is in the range <code>[0, 100]</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,20 @@
|
||||
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> both of the same length. The <strong>advantage</strong> of <code>nums1</code> with respect to <code>nums2</code> is the number of indices <code>i</code> for which <code>nums1[i] > nums2[i]</code>.</p>
|
||||
|
||||
<p>Return <em>any permutation of </em><code>nums1</code><em> that maximizes its <strong>advantage</strong> with respect to </em><code>nums2</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> nums1 = [2,7,11,15], nums2 = [1,10,4,11]
|
||||
<strong>Output:</strong> [2,11,7,15]
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> nums1 = [12,24,8,32], nums2 = [13,25,32,11]
|
||||
<strong>Output:</strong> [24,32,8,12]
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums2.length == nums1.length</code></li>
|
||||
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>Given two integers <code>n</code> and <code>k</code>, construct a list <code>answer</code> that contains <code>n</code> different positive integers ranging from <code>1</code> to <code>n</code> and obeys the following requirement:</p>
|
||||
|
||||
<ul>
|
||||
<li>Suppose this list is <code>answer = [a<sub>1</sub>, a<sub>2</sub>, a<sub>3</sub>, ... , a<sub>n</sub>]</code>, then the list <code>[|a<sub>1</sub> - a<sub>2</sub>|, |a<sub>2</sub> - a<sub>3</sub>|, |a<sub>3</sub> - a<sub>4</sub>|, ... , |a<sub>n-1</sub> - a<sub>n</sub>|]</code> has exactly <code>k</code> distinct integers.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the list</em> <code>answer</code>. If there multiple valid answers, return <strong>any of them</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, k = 1
|
||||
<strong>Output:</strong> [1,2,3]
|
||||
Explanation: The [1,2,3] has three different positive integers ranging from 1 to 3, and the [1,1] has exactly 1 distinct integer: 1
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, k = 2
|
||||
<strong>Output:</strong> [1,3,2]
|
||||
Explanation: The [1,3,2] has three different positive integers ranging from 1 to 3, and the [2,1] has exactly 2 distinct integers: 1 and 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k < n <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>Table: <code>Stadium</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| id | int |
|
||||
| visit_date | date |
|
||||
| people | int |
|
||||
+---------------+---------+
|
||||
visit_date is the primary key for this table.
|
||||
Each row of this table contains the visit date and visit id to the stadium with the number of people during the visit.
|
||||
No two rows will have the same visit_date, and as the id increases, the dates increase as well.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write an SQL query to display the records with three or more rows with <strong>consecutive</strong> <code>id</code>'s, and the number of people is greater than or equal to 100 for each.</p>
|
||||
|
||||
<p>Return the result table ordered by <code>visit_date</code> in <strong>ascending 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>
|
||||
Stadium table:
|
||||
+------+------------+-----------+
|
||||
| id | visit_date | people |
|
||||
+------+------------+-----------+
|
||||
| 1 | 2017-01-01 | 10 |
|
||||
| 2 | 2017-01-02 | 109 |
|
||||
| 3 | 2017-01-03 | 150 |
|
||||
| 4 | 2017-01-04 | 99 |
|
||||
| 5 | 2017-01-05 | 145 |
|
||||
| 6 | 2017-01-06 | 1455 |
|
||||
| 7 | 2017-01-07 | 199 |
|
||||
| 8 | 2017-01-09 | 188 |
|
||||
+------+------------+-----------+
|
||||
<strong>Output:</strong>
|
||||
+------+------------+-----------+
|
||||
| id | visit_date | people |
|
||||
+------+------------+-----------+
|
||||
| 5 | 2017-01-05 | 145 |
|
||||
| 6 | 2017-01-06 | 1455 |
|
||||
| 7 | 2017-01-07 | 199 |
|
||||
| 8 | 2017-01-09 | 188 |
|
||||
+------+------------+-----------+
|
||||
<strong>Explanation:</strong>
|
||||
The four rows with ids 5, 6, 7, and 8 have consecutive ids and each of them has >= 100 people attended. Note that row 8 was included even though the visit_date was not the next day after row 7.
|
||||
The rows with ids 2 and 3 are not included because we need at least three consecutive ids.
|
||||
</pre>
|
@@ -0,0 +1,37 @@
|
||||
<p>You are given two integer arrays of the same length <code>nums1</code> and <code>nums2</code>. In one operation, you are allowed to swap <code>nums1[i]</code> with <code>nums2[i]</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>nums1 = [1,2,3,<u>8</u>]</code>, and <code>nums2 = [5,6,7,<u>4</u>]</code>, you can swap the element at <code>i = 3</code> to obtain <code>nums1 = [1,2,3,4]</code> and <code>nums2 = [5,6,7,8]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum number of needed operations to make </em><code>nums1</code><em> and </em><code>nums2</code><em> <strong>strictly increasing</strong></em>. The test cases are generated so that the given input always makes it possible.</p>
|
||||
|
||||
<p>An array <code>arr</code> is <strong>strictly increasing</strong> if and only if <code>arr[0] < arr[1] < arr[2] < ... < arr[arr.length - 1]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,3,5,4], nums2 = [1,2,3,7]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
Swap nums1[3] and nums2[3]. Then the sequences are:
|
||||
nums1 = [1, 3, 5, 7] and nums2 = [1, 2, 3, 4]
|
||||
which are both strictly increasing.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [0,3,5,8,9], nums2 = [2,1,4,6,9]
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums1.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums2.length == nums1.length</code></li>
|
||||
<li><code>0 <= nums1[i], nums2[i] <= 2 * 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>A parentheses string is valid if and only if:</p>
|
||||
|
||||
<ul>
|
||||
<li>It is the empty string,</li>
|
||||
<li>It can be written as <code>AB</code> (<code>A</code> concatenated with <code>B</code>), where <code>A</code> and <code>B</code> are valid strings, or</li>
|
||||
<li>It can be written as <code>(A)</code>, where <code>A</code> is a valid string.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are given a parentheses string <code>s</code>. In one move, you can insert a parenthesis at any position of the string.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>s = "()))"</code>, you can insert an opening parenthesis to be <code>"(<strong>(</strong>)))"</code> or a closing parenthesis to be <code>"())<strong>)</strong>)"</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum number of moves required to make </em><code>s</code><em> valid</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "())"
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "((("
|
||||
<strong>Output:</strong> 3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s[i]</code> is either <code>'('</code> or <code>')'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
<p>You are given an integer array <code>nums</code>. In one move, you can pick an index <code>i</code> where <code>0 <= i < nums.length</code> and increment <code>nums[i]</code> by <code>1</code>.</p>
|
||||
|
||||
<p>Return <em>the minimum number of moves to make every value in </em><code>nums</code><em> <strong>unique</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,2]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> After 1 move, the array could be [1, 2, 3].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,1,2,1,7]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> After 6 moves, the array could be [3, 4, 1, 2, 5, 7].
|
||||
It can be shown with 5 or less moves that it is impossible for the array to have all unique values.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given an integer array <code>cost</code> where <code>cost[i]</code> is the cost of <code>i<sup>th</sup></code> step on a staircase. Once you pay the cost, you can either climb one or two steps.</p>
|
||||
|
||||
<p>You can either start from the step with index <code>0</code>, or the step with index <code>1</code>.</p>
|
||||
|
||||
<p>Return <em>the minimum cost to reach the top of the floor</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> cost = [10,<u>15</u>,20]
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> You will start at index 1.
|
||||
- Pay 15 and climb two steps to reach the top.
|
||||
The total cost is 15.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> cost = [<u>1</u>,100,<u>1</u>,1,<u>1</u>,100,<u>1</u>,<u>1</u>,100,<u>1</u>]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> You will start at index 0.
|
||||
- Pay 1 and climb two steps to reach index 2.
|
||||
- Pay 1 and climb two steps to reach index 4.
|
||||
- Pay 1 and climb two steps to reach index 6.
|
||||
- Pay 1 and climb one step to reach index 7.
|
||||
- Pay 1 and climb two steps to reach index 9.
|
||||
- Pay 1 and climb one step to reach the top.
|
||||
The total cost is 6.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= cost.length <= 1000</code></li>
|
||||
<li><code>0 <= cost[i] <= 999</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>There is a city composed of <code>n x n</code> blocks, where each block contains a single building shaped like a vertical square prism. You are given a <strong>0-indexed</strong> <code>n x n</code> integer matrix <code>grid</code> where <code>grid[r][c]</code> represents the <strong>height</strong> of the building located in the block at row <code>r</code> and column <code>c</code>.</p>
|
||||
|
||||
<p>A city's <strong>skyline</strong> is the the outer contour formed by all the building when viewing the side of the city from a distance. The <strong>skyline</strong> from each cardinal direction north, east, south, and west may be different.</p>
|
||||
|
||||
<p>We are allowed to increase the height of <strong>any number of buildings by any amount</strong> (the amount can be different per building). The height of a <code>0</code>-height building can also be increased. However, increasing the height of a building should <strong>not</strong> affect the city's <strong>skyline</strong> from any cardinal direction.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum total sum</strong> that the height of the buildings can be increased by <strong>without</strong> changing the city's <strong>skyline</strong> from any cardinal direction</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/21/807-ex1.png" style="width: 700px; height: 603px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
|
||||
<strong>Output:</strong> 35
|
||||
<strong>Explanation:</strong> The building heights are shown in the center of the above image.
|
||||
The skylines when viewed from each cardinal direction are drawn in red.
|
||||
The grid after increasing the height of buildings without affecting skylines is:
|
||||
gridNew = [ [8, 4, 8, 7],
|
||||
[7, 4, 7, 7],
|
||||
[9, 4, 8, 7],
|
||||
[3, 3, 3, 3] ]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,0,0],[0,0,0],[0,0,0]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Increasing the height of any building will result in the skyline changing.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length</code></li>
|
||||
<li><code>n == grid[r].length</code></li>
|
||||
<li><code>2 <= n <= 50</code></li>
|
||||
<li><code>0 <= grid[r][c] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
<p>Given the <code>root</code> of a binary search tree and the lowest and highest boundaries as <code>low</code> and <code>high</code>, trim the tree so that all its elements lies in <code>[low, high]</code>. Trimming the tree should <strong>not</strong> change the relative structure of the elements that will remain in the tree (i.e., any node's descendant should remain a descendant). It can be proven that there is a <strong>unique answer</strong>.</p>
|
||||
|
||||
<p>Return <em>the root of the trimmed binary search tree</em>. Note that the root may change depending on the given bounds.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/09/trim1.jpg" style="width: 450px; height: 126px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,0,2], low = 1, high = 2
|
||||
<strong>Output:</strong> [1,null,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/09/trim2.jpg" style="width: 450px; height: 277px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [3,0,4,null,2,null,null,1], low = 1, high = 3
|
||||
<strong>Output:</strong> [3,2,null,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree in the range <code>[1, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
|
||||
<li>The value of each node in the tree is <strong>unique</strong>.</li>
|
||||
<li><code>root</code> is guaranteed to be a valid binary search tree.</li>
|
||||
<li><code>0 <= low <= high <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>Given a <code>wordlist</code>, we want to implement a spellchecker that converts a query word into a correct word.</p>
|
||||
|
||||
<p>For a given <code>query</code> word, the spell checker handles two categories of spelling mistakes:</p>
|
||||
|
||||
<ul>
|
||||
<li>Capitalization: If the query matches a word in the wordlist (<strong>case-insensitive</strong>), then the query word is returned with the same case as the case in the wordlist.
|
||||
|
||||
<ul>
|
||||
<li>Example: <code>wordlist = ["yellow"]</code>, <code>query = "YellOw"</code>: <code>correct = "yellow"</code></li>
|
||||
<li>Example: <code>wordlist = ["Yellow"]</code>, <code>query = "yellow"</code>: <code>correct = "Yellow"</code></li>
|
||||
<li>Example: <code>wordlist = ["yellow"]</code>, <code>query = "yellow"</code>: <code>correct = "yellow"</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>Vowel Errors: If after replacing the vowels <code>('a', 'e', 'i', 'o', 'u')</code> of the query word with any vowel individually, it matches a word in the wordlist (<strong>case-insensitive</strong>), then the query word is returned with the same case as the match in the wordlist.
|
||||
<ul>
|
||||
<li>Example: <code>wordlist = ["YellOw"]</code>, <code>query = "yollow"</code>: <code>correct = "YellOw"</code></li>
|
||||
<li>Example: <code>wordlist = ["YellOw"]</code>, <code>query = "yeellow"</code>: <code>correct = ""</code> (no match)</li>
|
||||
<li>Example: <code>wordlist = ["YellOw"]</code>, <code>query = "yllw"</code>: <code>correct = ""</code> (no match)</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>In addition, the spell checker operates under the following precedence rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>When the query exactly matches a word in the wordlist (<strong>case-sensitive</strong>), you should return the same word back.</li>
|
||||
<li>When the query matches a word up to capitlization, you should return the first such match in the wordlist.</li>
|
||||
<li>When the query matches a word up to vowel errors, you should return the first such match in the wordlist.</li>
|
||||
<li>If the query has no matches in the wordlist, you should return the empty string.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given some <code>queries</code>, return a list of words <code>answer</code>, where <code>answer[i]</code> is the correct word for <code>query = queries[i]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"]
|
||||
<strong>Output:</strong> ["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> wordlist = ["yellow"], queries = ["YellOw"]
|
||||
<strong>Output:</strong> ["yellow"]
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= wordlist.length, queries.length <= 5000</code></li>
|
||||
<li><code>1 <= wordlist[i].length, queries[i].length <= 7</code></li>
|
||||
<li><code>wordlist[i]</code> and <code>queries[i]</code> consist only of only English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>You are given an integer array <code>nums</code> of length <code>n</code> which represents a permutation of all the integers in the range <code>[0, n - 1]</code>.</p>
|
||||
|
||||
<p>The number of <strong>global inversions</strong> is the number of the different pairs <code>(i, j)</code> where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < j < n</code></li>
|
||||
<li><code>nums[i] > nums[j]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>The number of <strong>local inversions</strong> is the number of indices <code>i</code> where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < n - 1</code></li>
|
||||
<li><code>nums[i] > nums[i + 1]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>true</code> <em>if the number of <strong>global inversions</strong> is equal to the number of <strong>local inversions</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,0,2]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> There is 1 global inversion and 1 local inversion.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,0]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> There are 2 global inversions and 1 local inversion.
|
||||
</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>0 <= nums[i] < n</code></li>
|
||||
<li>All the integers of <code>nums</code> are <strong>unique</strong>.</li>
|
||||
<li><code>nums</code> is a permutation of all the numbers in the range <code>[0, n - 1]</code>.</li>
|
||||
</ul>
|
37
算法题(国内版)/problem (English)/公交路线(English) [bus-routes].html
Normal file
37
算法题(国内版)/problem (English)/公交路线(English) [bus-routes].html
Normal file
@@ -0,0 +1,37 @@
|
||||
<p>You are given an array <code>routes</code> representing bus routes where <code>routes[i]</code> is a bus route that the <code>i<sup>th</sup></code> bus repeats forever.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>routes[0] = [1, 5, 7]</code>, this means that the <code>0<sup>th</sup></code> bus travels in the sequence <code>1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...</code> forever.</li>
|
||||
</ul>
|
||||
|
||||
<p>You will start at the bus stop <code>source</code> (You are not on any bus initially), and you want to go to the bus stop <code>target</code>. You can travel between bus stops by buses only.</p>
|
||||
|
||||
<p>Return <em>the least number of buses you must take to travel from </em><code>source</code><em> to </em><code>target</code>. Return <code>-1</code> if it is not possible.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> routes = [[1,2,7],[3,6,7]], source = 1, target = 6
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
|
||||
<strong>Output:</strong> -1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= routes.length <= 500</code>.</li>
|
||||
<li><code>1 <= routes[i].length <= 10<sup>5</sup></code></li>
|
||||
<li>All the values of <code>routes[i]</code> are <strong>unique</strong>.</li>
|
||||
<li><code>sum(routes[i].length) <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= routes[i][j] < 10<sup>6</sup></code></li>
|
||||
<li><code>0 <= source, target < 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>Alice and Bob have a different total number of candies. You are given two integer arrays <code>aliceSizes</code> and <code>bobSizes</code> where <code>aliceSizes[i]</code> is the number of candies of the <code>i<sup>th</sup></code> box of candy that Alice has and <code>bobSizes[j]</code> is the number of candies of the <code>j<sup>th</sup></code> box of candy that Bob has.</p>
|
||||
|
||||
<p>Since they are friends, they would like to exchange one candy box each so that after the exchange, they both have the same total amount of candy. The total amount of candy a person has is the sum of the number of candies in each box they have.</p>
|
||||
|
||||
<p>Return a<em>n integer array </em><code>answer</code><em> where </em><code>answer[0]</code><em> is the number of candies in the box that Alice must exchange, and </em><code>answer[1]</code><em> is the number of candies in the box that Bob must exchange</em>. If there are multiple answers, you may <strong>return any</strong> one of them. It is guaranteed that at least one answer exists.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> aliceSizes = [1,1], bobSizes = [2,2]
|
||||
<strong>Output:</strong> [1,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> aliceSizes = [1,2], bobSizes = [2,3]
|
||||
<strong>Output:</strong> [1,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> aliceSizes = [2], bobSizes = [1,3]
|
||||
<strong>Output:</strong> [2,3]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= aliceSizes.length, bobSizes.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= aliceSizes[i], bobSizes[j] <= 10<sup>5</sup></code></li>
|
||||
<li>Alice and Bob have a different total number of candies.</li>
|
||||
<li>There will be at least one valid answer for the given input.</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>Given the <code>root</code> of a binary tree, the depth of each node is <strong>the shortest distance to the root</strong>.</p>
|
||||
|
||||
<p>Return <em>the smallest subtree</em> such that it contains <strong>all the deepest nodes</strong> in the original tree.</p>
|
||||
|
||||
<p>A node is called <strong>the deepest</strong> if it has the largest depth possible among any node in the entire tree.</p>
|
||||
|
||||
<p>The <strong>subtree</strong> of a node is a tree consisting of that node, plus the set of all descendants of that node.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/01/sketch1.png" style="width: 600px; height: 510px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4]
|
||||
<strong>Output:</strong> [2,7,4]
|
||||
<strong>Explanation:</strong> We return the node with value 2, colored in yellow in the diagram.
|
||||
The nodes coloured in blue are the deepest nodes of the tree.
|
||||
Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is the smallest subtree among them, so we return it.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1]
|
||||
<strong>Output:</strong> [1]
|
||||
<strong>Explanation:</strong> The root is the deepest node in the tree.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [0,1,3,null,2]
|
||||
<strong>Output:</strong> [2]
|
||||
<strong>Explanation:</strong> The deepest node in the tree is 2, the valid subtrees are the subtrees of nodes 2, 1 and 0 but the subtree of node 2 is the smallest.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree will be in the range <code>[1, 500]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 500</code></li>
|
||||
<li>The values of the nodes in the tree are <strong>unique</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Note:</strong> This question is the same as 1123: <a href="https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/" target="_blank">https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/</a></p>
|
@@ -0,0 +1,33 @@
|
||||
<p>In this problem, a rooted tree is a <b>directed</b> graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents.</p>
|
||||
|
||||
<p>The given input is a directed graph that started as a rooted tree with <code>n</code> nodes (with distinct values from <code>1</code> to <code>n</code>), with one additional directed edge added. The added edge has two different vertices chosen from <code>1</code> to <code>n</code>, and was not an edge that already existed.</p>
|
||||
|
||||
<p>The resulting graph is given as a 2D-array of <code>edges</code>. Each element of <code>edges</code> is a pair <code>[u<sub>i</sub>, v<sub>i</sub>]</code> that represents a <b>directed</b> edge connecting nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>, where <code>u<sub>i</sub></code> is a parent of child <code>v<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Return <em>an edge that can be removed so that the resulting graph is a rooted tree of</em> <code>n</code> <em>nodes</em>. If there are multiple answers, return the answer that occurs last in the given 2D-array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/graph1.jpg" style="width: 222px; height: 222px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[1,2],[1,3],[2,3]]
|
||||
<strong>Output:</strong> [2,3]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/graph2.jpg" style="width: 222px; height: 382px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[1,2],[2,3],[3,4],[4,1],[1,5]]
|
||||
<strong>Output:</strong> [4,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == edges.length</code></li>
|
||||
<li><code>3 <= n <= 1000</code></li>
|
||||
<li><code>edges[i].length == 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>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>In this problem, a tree is an <strong>undirected graph</strong> that is connected and has no cycles.</p>
|
||||
|
||||
<p>You are given a graph that started as a tree with <code>n</code> nodes labeled from <code>1</code> to <code>n</code>, with one additional edge added. The added edge has two <strong>different</strong> vertices chosen from <code>1</code> to <code>n</code>, and was not an edge that already existed. The graph is represented as an array <code>edges</code> of length <code>n</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the graph.</p>
|
||||
|
||||
<p>Return <em>an edge that can be removed so that the resulting graph is a tree of </em><code>n</code><em> nodes</em>. If there are multiple answers, return the answer that occurs last in the input.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/02/reduntant1-1-graph.jpg" style="width: 222px; height: 222px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[1,2],[1,3],[2,3]]
|
||||
<strong>Output:</strong> [2,3]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/02/reduntant1-2-graph.jpg" style="width: 382px; height: 222px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
|
||||
<strong>Output:</strong> [1,4]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == edges.length</code></li>
|
||||
<li><code>3 <= n <= 1000</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>1 <= a<sub>i</sub> < b<sub>i</sub> <= edges.length</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li>There are no repeated edges.</li>
|
||||
<li>The given graph is connected.</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given a string <code>s</code> of lowercase English letters and an array <code>widths</code> denoting <strong>how many pixels wide</strong> each lowercase English letter is. Specifically, <code>widths[0]</code> is the width of <code>'a'</code>, <code>widths[1]</code> is the width of <code>'b'</code>, and so on.</p>
|
||||
|
||||
<p>You are trying to write <code>s</code> across several lines, where <strong>each line is no longer than </strong><code>100</code><strong> pixels</strong>. Starting at the beginning of <code>s</code>, write as many letters on the first line such that the total width does not exceed <code>100</code> pixels. Then, from where you stopped in <code>s</code>, continue writing as many letters as you can on the second line. Continue this process until you have written all of <code>s</code>.</p>
|
||||
|
||||
<p>Return <em>an array </em><code>result</code><em> of length 2 where:</em></p>
|
||||
|
||||
<ul>
|
||||
<li><code>result[0]</code><em> is the total number of lines.</em></li>
|
||||
<li><code>result[1]</code><em> is the width of the last line in pixels.</em></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10], s = "abcdefghijklmnopqrstuvwxyz"
|
||||
<strong>Output:</strong> [3,60]
|
||||
<strong>Explanation:</strong> You can write s as follows:
|
||||
abcdefghij // 100 pixels wide
|
||||
klmnopqrst // 100 pixels wide
|
||||
uvwxyz // 60 pixels wide
|
||||
There are a total of 3 lines, and the last line is 60 pixels wide.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10], s = "bbbcccdddaaa"
|
||||
<strong>Output:</strong> [2,4]
|
||||
<strong>Explanation:</strong> You can write s as follows:
|
||||
bbbcccdddaa // 98 pixels wide
|
||||
a // 4 pixels wide
|
||||
There are a total of 2 lines, and the last line is 4 pixels wide.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>widths.length == 26</code></li>
|
||||
<li><code>2 <= widths[i] <= 10</code></li>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> contains only lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>There is an <code>m x n</code> grid with a ball. The ball is initially at the position <code>[startRow, startColumn]</code>. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply <strong>at most</strong> <code>maxMove</code> moves to the ball.</p>
|
||||
|
||||
<p>Given the five integers <code>m</code>, <code>n</code>, <code>maxMove</code>, <code>startRow</code>, <code>startColumn</code>, return the number of paths to move the ball out of the grid boundary. Since the answer can be very 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/04/28/out_of_boundary_paths_1.png" style="width: 500px; height: 296px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
|
||||
<strong>Output:</strong> 6
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_2.png" style="width: 500px; height: 293px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
|
||||
<strong>Output:</strong> 12
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m, n <= 50</code></li>
|
||||
<li><code>0 <= maxMove <= 50</code></li>
|
||||
<li><code>0 <= startRow < m</code></li>
|
||||
<li><code>0 <= startColumn < n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,63 @@
|
||||
<p>On a <strong>single-threaded</strong> CPU, we execute a program containing <code>n</code> functions. Each function has a unique ID between <code>0</code> and <code>n-1</code>.</p>
|
||||
|
||||
<p>Function calls are <strong>stored in a <a href="https://en.wikipedia.org/wiki/Call_stack">call stack</a></strong>: when a function call starts, its ID is pushed onto the stack, and when a function call ends, its ID is popped off the stack. The function whose ID is at the top of the stack is <strong>the current function being executed</strong>. Each time a function starts or ends, we write a log with the ID, whether it started or ended, and the timestamp.</p>
|
||||
|
||||
<p>You are given a list <code>logs</code>, where <code>logs[i]</code> represents the <code>i<sup>th</sup></code> log message formatted as a string <code>"{function_id}:{"start" | "end"}:{timestamp}"</code>. For example, <code>"0:start:3"</code> means a function call with function ID <code>0</code> <strong>started at the beginning</strong> of timestamp <code>3</code>, and <code>"1:end:2"</code> means a function call with function ID <code>1</code> <strong>ended at the end</strong> of timestamp <code>2</code>. Note that a function can be called <b>multiple times, possibly recursively</b>.</p>
|
||||
|
||||
<p>A function's <strong>exclusive time</strong> is the sum of execution times for all function calls in the program. For example, if a function is called twice, one call executing for <code>2</code> time units and another call executing for <code>1</code> time unit, the <strong>exclusive time</strong> is <code>2 + 1 = 3</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>exclusive time</strong> of each function in an array, where the value at the </em><code>i<sup>th</sup></code><em> index represents the exclusive time for the function with ID </em><code>i</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/05/diag1b.png" style="width: 550px; height: 239px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]
|
||||
<strong>Output:</strong> [3,4]
|
||||
<strong>Explanation:</strong>
|
||||
Function 0 starts at the beginning of time 0, then it executes 2 for units of time and reaches the end of time 1.
|
||||
Function 1 starts at the beginning of time 2, executes for 4 units of time, and ends at the end of time 5.
|
||||
Function 0 resumes execution at the beginning of time 6 and executes for 1 unit of time.
|
||||
So function 0 spends 2 + 1 = 3 units of total time executing, and function 1 spends 4 units of total time executing.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1, logs = ["0:start:0","0:start:2","0:end:5","0:start:6","0:end:6","0:end:7"]
|
||||
<strong>Output:</strong> [8]
|
||||
<strong>Explanation:</strong>
|
||||
Function 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.
|
||||
Function 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.
|
||||
Function 0 (initial call) resumes execution then immediately calls itself again.
|
||||
Function 0 (2nd recursive call) starts at the beginning of time 6 and executes for 1 unit of time.
|
||||
Function 0 (initial call) resumes execution at the beginning of time 7 and executes for 1 unit of time.
|
||||
So function 0 spends 2 + 4 + 1 + 1 = 8 units of total time executing.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, logs = ["0:start:0","0:start:2","0:end:5","1:start:6","1:end:6","0:end:7"]
|
||||
<strong>Output:</strong> [7,1]
|
||||
<strong>Explanation:</strong>
|
||||
Function 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.
|
||||
Function 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.
|
||||
Function 0 (initial call) resumes execution then immediately calls function 1.
|
||||
Function 1 starts at the beginning of time 6, executes 1 unit of time, and ends at the end of time 6.
|
||||
Function 0 resumes execution at the beginning of time 6 and executes for 2 units of time.
|
||||
So function 0 spends 2 + 4 + 1 = 7 units of total time executing, and function 1 spends 1 unit of total time executing.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= logs.length <= 500</code></li>
|
||||
<li><code>0 <= function_id < n</code></li>
|
||||
<li><code>0 <= timestamp <= 10<sup>9</sup></code></li>
|
||||
<li>No two start events will happen at the same timestamp.</li>
|
||||
<li>No two end events will happen at the same timestamp.</li>
|
||||
<li>Each function has an <code>"end"</code> log for each <code>"start"</code> log.</li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>Given an integer array <code>nums</code>, partition it into two (contiguous) subarrays <code>left</code> and <code>right</code> so that:</p>
|
||||
|
||||
<ul>
|
||||
<li>Every element in <code>left</code> is less than or equal to every element in <code>right</code>.</li>
|
||||
<li><code>left</code> and <code>right</code> are non-empty.</li>
|
||||
<li><code>left</code> has the smallest possible size.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the length of </em><code>left</code><em> after such a partitioning</em>.</p>
|
||||
|
||||
<p>Test cases are generated such that partitioning exists.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,0,3,8,6]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> left = [5,0,3], right = [8,6]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1,0,6,12]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> left = [1,1,1,0], right = [6,12]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
<li>There is at least one valid answer for the given input.</li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>You are given an integer array <code>nums</code> that is <strong>sorted in non-decreasing order</strong>.</p>
|
||||
|
||||
<p>Determine if it is possible to split <code>nums</code> into <strong>one or more subsequences</strong> such that <strong>both</strong> of the following conditions are true:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each subsequence is a <strong>consecutive increasing sequence</strong> (i.e. each integer is <strong>exactly one</strong> more than the previous integer).</li>
|
||||
<li>All subsequences have a length of <code>3</code><strong> or more</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>true</code><em> if you can split </em><code>nums</code><em> according to the above conditions, or </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of an array is a new array that is formed from the original array by deleting some (can be none) of the elements without disturbing the relative positions of the remaining elements. (i.e., <code>[1,3,5]</code> is a subsequence of <code>[<u>1</u>,2,<u>3</u>,4,<u>5</u>]</code> while <code>[1,3,2]</code> is not).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,3,4,5]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> nums can be split into the following subsequences:
|
||||
[<strong><u>1</u></strong>,<strong><u>2</u></strong>,<strong><u>3</u></strong>,3,4,5] --> 1, 2, 3
|
||||
[1,2,3,<strong><u>3</u></strong>,<strong><u>4</u></strong>,<strong><u>5</u></strong>] --> 3, 4, 5
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,3,4,4,5,5]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> nums can be split into the following subsequences:
|
||||
[<strong><u>1</u></strong>,<strong><u>2</u></strong>,<strong><u>3</u></strong>,3,<strong><u>4</u></strong>,4,<strong><u>5</u></strong>,5] --> 1, 2, 3, 4, 5
|
||||
[1,2,3,<strong><u>3</u></strong>,4,<strong><u>4</u></strong>,5,<strong><u>5</u></strong>] --> 3, 4, 5
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,4,5]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is impossible to split nums into consecutive increasing subsequences of length 3 or more.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-1000 <= nums[i] <= 1000</code></li>
|
||||
<li><code>nums</code> is sorted in <strong>non-decreasing</strong> order.</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>Given a string <code>expression</code> representing an expression of fraction addition and subtraction, return the calculation result in string format.</p>
|
||||
|
||||
<p>The final result should be an <a href="https://en.wikipedia.org/wiki/Irreducible_fraction" target="_blank">irreducible fraction</a>. If your final result is an integer, change it to the format of a fraction that has a denominator <code>1</code>. So in this case, <code>2</code> should be converted to <code>2/1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> expression = "-1/2+1/2"
|
||||
<strong>Output:</strong> "0/1"
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> expression = "-1/2+1/2+1/3"
|
||||
<strong>Output:</strong> "1/3"
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> expression = "1/3-1/2"
|
||||
<strong>Output:</strong> "-1/6"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The input string only contains <code>'0'</code> to <code>'9'</code>, <code>'/'</code>, <code>'+'</code> and <code>'-'</code>. So does the output.</li>
|
||||
<li>Each fraction (input and output) has the format <code>±numerator/denominator</code>. If the first input fraction or the output is positive, then <code>'+'</code> will be omitted.</li>
|
||||
<li>The input only contains valid <strong>irreducible fractions</strong>, where the <strong>numerator</strong> and <strong>denominator</strong> of each fraction will always be in the range <code>[1, 10]</code>. If the denominator is <code>1</code>, it means this fraction is actually an integer in a fraction format defined above.</li>
|
||||
<li>The number of given fractions will be in the range <code>[1, 10]</code>.</li>
|
||||
<li>The numerator and denominator of the <strong>final result</strong> are guaranteed to be valid and in the range of <strong>32-bit</strong> int.</li>
|
||||
</ul>
|
40
算法题(国内版)/problem (English)/分汤(English) [soup-servings].html
Normal file
40
算法题(国内版)/problem (English)/分汤(English) [soup-servings].html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>There are two types of soup: <strong>type A</strong> and <strong>type B</strong>. Initially, we have <code>n</code> ml of each type of soup. There are four kinds of operations:</p>
|
||||
|
||||
<ol>
|
||||
<li>Serve <code>100</code> ml of <strong>soup A</strong> and <code>0</code> ml of <strong>soup B</strong>,</li>
|
||||
<li>Serve <code>75</code> ml of <strong>soup A</strong> and <code>25</code> ml of <strong>soup B</strong>,</li>
|
||||
<li>Serve <code>50</code> ml of <strong>soup A</strong> and <code>50</code> ml of <strong>soup B</strong>, and</li>
|
||||
<li>Serve <code>25</code> ml of <strong>soup A</strong> and <code>75</code> ml of <strong>soup B</strong>.</li>
|
||||
</ol>
|
||||
|
||||
<p>When we serve some soup, we give it to someone, and we no longer have it. Each turn, we will choose from the four operations with an equal probability <code>0.25</code>. If the remaining volume of soup is not enough to complete the operation, we will serve as much as possible. We stop once we no longer have some quantity of both types of soup.</p>
|
||||
|
||||
<p><strong>Note</strong> that we do not have an operation where all <code>100</code> ml's of <strong>soup B</strong> are used first.</p>
|
||||
|
||||
<p>Return <em>the probability that <strong>soup A</strong> will be empty first, plus half the probability that <strong>A</strong> and <strong>B</strong> become empty at the same time</em>. Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 50
|
||||
<strong>Output:</strong> 0.62500
|
||||
<strong>Explanation:</strong> If we choose the first two operations, A will become empty first.
|
||||
For the third operation, A and B will become empty at the same time.
|
||||
For the fourth operation, B will become empty first.
|
||||
So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 100
|
||||
<strong>Output:</strong> 0.71875
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>Alice has <code>n</code> candies, where the <code>i<sup>th</sup></code> candy is of type <code>candyType[i]</code>. Alice noticed that she started to gain weight, so she visited a doctor.</p>
|
||||
|
||||
<p>The doctor advised Alice to only eat <code>n / 2</code> of the candies she has (<code>n</code> is always even). Alice likes her candies very much, and she wants to eat the maximum number of different types of candies while still following the doctor's advice.</p>
|
||||
|
||||
<p>Given the integer array <code>candyType</code> of length <code>n</code>, return <em>the <strong>maximum</strong> number of different types of candies she can eat if she only eats </em><code>n / 2</code><em> of them</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candyType = [1,1,2,2,3,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Alice can only eat 6 / 2 = 3 candies. Since there are only 3 types, she can eat one of each type.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candyType = [1,1,2,3]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Alice can only eat 4 / 2 = 2 candies. Whether she eats types [1,2], [1,3], or [2,3], she still can only eat 2 different types.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candyType = [6,6,6,6]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Alice can only eat 4 / 2 = 2 candies. Even though she can eat 2 candies, she only has 1 type.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == candyType.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>n</code> is even.</li>
|
||||
<li><code>-10<sup>5</sup> <= candyType[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>Given the <code>head</code> of a singly linked list and an integer <code>k</code>, split the linked list into <code>k</code> consecutive linked list parts.</p>
|
||||
|
||||
<p>The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null.</p>
|
||||
|
||||
<p>The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later.</p>
|
||||
|
||||
<p>Return <em>an array of the </em><code>k</code><em> parts</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/13/split1-lc.jpg" style="width: 400px; height: 134px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1,2,3], k = 5
|
||||
<strong>Output:</strong> [[1],[2],[3],[],[]]
|
||||
<strong>Explanation:</strong>
|
||||
The first element output[0] has output[0].val = 1, output[0].next = null.
|
||||
The last element output[4] is null, but its string representation as a ListNode is [].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/13/split2-lc.jpg" style="width: 600px; height: 60px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1,2,3,4,5,6,7,8,9,10], k = 3
|
||||
<strong>Output:</strong> [[1,2,3,4],[5,6,7],[8,9,10]]
|
||||
<strong>Explanation:</strong>
|
||||
The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the list is in the range <code>[0, 1000]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 1000</code></li>
|
||||
<li><code>1 <= k <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,26 @@
|
||||
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> if it is possible to divide this array into <code>k</code> non-empty subsets whose sums are all equal.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,3,2,3,5,2,1], k = 4
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4], k = 3
|
||||
<strong>Output:</strong> false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= nums.length <= 16</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
<li>The frequency of each element is in the range <code>[1, 4]</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>You are given a string <code>s</code>. We want to partition the string into as many parts as possible so that each letter appears in at most one part.</p>
|
||||
|
||||
<p>Note that the partition is done so that after concatenating all the parts in order, the resultant string should be <code>s</code>.</p>
|
||||
|
||||
<p>Return <em>a list of integers representing the size of these parts</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "ababcbacadefegdehijhklij"
|
||||
<strong>Output:</strong> [9,7,8]
|
||||
<strong>Explanation:</strong>
|
||||
The partition is "ababcbaca", "defegde", "hijhklij".
|
||||
This is a partition so that each letter appears in at most one part.
|
||||
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "eccbbbbdec"
|
||||
<strong>Output:</strong> [10]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 500</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given an array of <code>n</code> strings <code>strs</code>, all of the same length.</p>
|
||||
|
||||
<p>We may choose any deletion indices, and we delete all the characters in those indices for each string.</p>
|
||||
|
||||
<p>For example, if we have <code>strs = ["abcdef","uvwxyz"]</code> and deletion indices <code>{0, 2, 3}</code>, then the final array after deletions is <code>["bef", "vyz"]</code>.</p>
|
||||
|
||||
<p>Suppose we chose a set of deletion indices <code>answer</code> such that after deletions, the final array has its elements in <strong>lexicographic</strong> order (i.e., <code>strs[0] <= strs[1] <= strs[2] <= ... <= strs[n - 1]</code>). Return <em>the minimum possible value of</em> <code>answer.length</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> strs = ["ca","bb","ac"]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
After deleting the first column, strs = ["a", "b", "c"].
|
||||
Now strs is in lexicographic order (ie. strs[0] <= strs[1] <= strs[2]).
|
||||
We require at least 1 deletion since initially strs was not in lexicographic order, so the answer is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> strs = ["xc","yb","za"]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
strs is already in lexicographic order, so we do not need to delete anything.
|
||||
Note that the rows of strs are not necessarily in lexicographic order:
|
||||
i.e., it is NOT necessarily true that (strs[0][0] <= strs[0][1] <= ...)
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> strs = ["zyx","wvu","tsr"]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We have to delete every column.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == strs.length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= strs[i].length <= 100</code></li>
|
||||
<li><code>strs[i]</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given an array of <code>n</code> strings <code>strs</code>, all of the same length.</p>
|
||||
|
||||
<p>We may choose any deletion indices, and we delete all the characters in those indices for each string.</p>
|
||||
|
||||
<p>For example, if we have <code>strs = ["abcdef","uvwxyz"]</code> and deletion indices <code>{0, 2, 3}</code>, then the final array after deletions is <code>["bef", "vyz"]</code>.</p>
|
||||
|
||||
<p>Suppose we chose a set of deletion indices <code>answer</code> such that after deletions, the final array has <strong>every string (row) in lexicographic</strong> order. (i.e., <code>(strs[0][0] <= strs[0][1] <= ... <= strs[0][strs[0].length - 1])</code>, and <code>(strs[1][0] <= strs[1][1] <= ... <= strs[1][strs[1].length - 1])</code>, and so on). Return <em>the minimum possible value of</em> <code>answer.length</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> strs = ["babca","bbazb"]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> After deleting columns 0, 1, and 4, the final array is strs = ["bc", "az"].
|
||||
Both these rows are individually in lexicographic order (ie. strs[0][0] <= strs[0][1] and strs[1][0] <= strs[1][1]).
|
||||
Note that strs[0] > strs[1] - the array strs is not necessarily in lexicographic order.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> strs = ["edcba"]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> If we delete less than 4 columns, the only row will not be lexicographically sorted.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> strs = ["ghi","def","abc"]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> All rows are already lexicographically sorted.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == strs.length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= strs[i].length <= 100</code></li>
|
||||
<li><code>strs[i]</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
<li> </li>
|
||||
</ul>
|
@@ -0,0 +1,59 @@
|
||||
<p>You are given an array of <code>n</code> strings <code>strs</code>, all of the same length.</p>
|
||||
|
||||
<p>The strings can be arranged such that there is one on each line, making a grid. For example, <code>strs = ["abc", "bce", "cae"]</code> can be arranged as:</p>
|
||||
|
||||
<pre>
|
||||
abc
|
||||
bce
|
||||
cae
|
||||
</pre>
|
||||
|
||||
<p>You want to <strong>delete</strong> the columns that are <strong>not sorted lexicographically</strong>. In the above example (0-indexed), columns 0 (<code>'a'</code>, <code>'b'</code>, <code>'c'</code>) and 2 (<code>'c'</code>, <code>'e'</code>, <code>'e'</code>) are sorted while column 1 (<code>'b'</code>, <code>'c'</code>, <code>'a'</code>) is not, so you would delete column 1.</p>
|
||||
|
||||
<p>Return <em>the number of columns that you will delete</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> strs = ["cba","daf","ghi"]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The grid looks as follows:
|
||||
cba
|
||||
daf
|
||||
ghi
|
||||
Columns 0 and 2 are sorted, but column 1 is not, so you only need to delete 1 column.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> strs = ["a","b"]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The grid looks as follows:
|
||||
a
|
||||
b
|
||||
Column 0 is the only column and is sorted, so you will not delete any columns.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> strs = ["zyx","wvu","tsr"]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The grid looks as follows:
|
||||
zyx
|
||||
wvu
|
||||
tsr
|
||||
All 3 columns are not sorted, so you will delete all 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == strs.length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= strs[i].length <= 1000</code></li>
|
||||
<li><code>strs[i]</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given an integer array <code>nums</code>. You want to maximize the number of points you get by performing the following operation any number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick any <code>nums[i]</code> and delete it to earn <code>nums[i]</code> points. Afterwards, you must delete <b>every</b> element equal to <code>nums[i] - 1</code> and <strong>every</strong> element equal to <code>nums[i] + 1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum number of points</strong> you can earn by applying the above operation some number of times</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,4,2]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> You can perform the following operations:
|
||||
- Delete 4 to earn 4 points. Consequently, 3 is also deleted. nums = [2].
|
||||
- Delete 2 to earn 2 points. nums = [].
|
||||
You earn a total of 6 points.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,2,3,3,3,4]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> You can perform the following operations:
|
||||
- Delete a 3 to earn 3 points. All 2's and 4's are also deleted. nums = [3,3].
|
||||
- Delete a 3 again to earn 3 points. nums = [3].
|
||||
- Delete a 3 once more to earn 3 points. nums = [].
|
||||
You earn a total of 9 points.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>A valid parentheses string is either empty <code>""</code>, <code>"(" + A + ")"</code>, or <code>A + B</code>, where <code>A</code> and <code>B</code> are valid parentheses strings, and <code>+</code> represents string concatenation.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>""</code>, <code>"()"</code>, <code>"(())()"</code>, and <code>"(()(()))"</code> are all valid parentheses strings.</li>
|
||||
</ul>
|
||||
|
||||
<p>A valid parentheses string <code>s</code> is primitive if it is nonempty, and there does not exist a way to split it into <code>s = A + B</code>, with <code>A</code> and <code>B</code> nonempty valid parentheses strings.</p>
|
||||
|
||||
<p>Given a valid parentheses string <code>s</code>, consider its primitive decomposition: <code>s = P<sub>1</sub> + P<sub>2</sub> + ... + P<sub>k</sub></code>, where <code>P<sub>i</sub></code> are primitive valid parentheses strings.</p>
|
||||
|
||||
<p>Return <code>s</code> <em>after removing the outermost parentheses of every primitive string in the primitive decomposition of </em><code>s</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "(()())(())"
|
||||
<strong>Output:</strong> "()()()"
|
||||
<strong>Explanation:</strong>
|
||||
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
|
||||
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "(()())(())(()(()))"
|
||||
<strong>Output:</strong> "()()()()(())"
|
||||
<strong>Explanation:</strong>
|
||||
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
|
||||
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "()()"
|
||||
<strong>Output:</strong> ""
|
||||
<strong>Explanation:</strong>
|
||||
The input string is "()()", with primitive decomposition "()" + "()".
|
||||
After removing outer parentheses of each part, this is "" + "" = "".
|
||||
</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>'('</code> or <code>')'</code>.</li>
|
||||
<li><code>s</code> is a valid parentheses string.</li>
|
||||
</ul>
|
@@ -0,0 +1,78 @@
|
||||
<p>Given a C++ program, remove comments from it. The program source is an array of strings <code>source</code> where <code>source[i]</code> is the <code>i<sup>th</sup></code> line of the source code. This represents the result of splitting the original source code string by the newline character <code>'\n'</code>.</p>
|
||||
|
||||
<p>In C++, there are two types of comments, line comments, and block comments.</p>
|
||||
|
||||
<ul>
|
||||
<li>The string <code>"//"</code> denotes a line comment, which represents that it and the rest of the characters to the right of it in the same line should be ignored.</li>
|
||||
<li>The string <code>"/*"</code> denotes a block comment, which represents that all characters until the next (non-overlapping) occurrence of <code>"*/"</code> should be ignored. (Here, occurrences happen in reading order: line by line from left to right.) To be clear, the string <code>"/*/"</code> does not yet end the block comment, as the ending would be overlapping the beginning.</li>
|
||||
</ul>
|
||||
|
||||
<p>The first effective comment takes precedence over others.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if the string <code>"//"</code> occurs in a block comment, it is ignored.</li>
|
||||
<li>Similarly, if the string <code>"/*"</code> occurs in a line or block comment, it is also ignored.</li>
|
||||
</ul>
|
||||
|
||||
<p>If a certain line of code is empty after removing comments, you must not output that line: each string in the answer list will be non-empty.</p>
|
||||
|
||||
<p>There will be no control characters, single quote, or double quote characters.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>source = "string s = "/* Not a comment. */";"</code> will not be a test case.</li>
|
||||
</ul>
|
||||
|
||||
<p>Also, nothing else such as defines or macros will interfere with the comments.</p>
|
||||
|
||||
<p>It is guaranteed that every open block comment will eventually be closed, so <code>"/*"</code> outside of a line or block comment always starts a new comment.</p>
|
||||
|
||||
<p>Finally, implicit newline characters can be deleted by block comments. Please see the examples below for details.</p>
|
||||
|
||||
<p>After removing the comments from the source code, return <em>the source code in the same format</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> source = ["/*Test program */", "int main()", "{ ", " // variable declaration ", "int a, b, c;", "/* This is a test", " multiline ", " comment for ", " testing */", "a = b + c;", "}"]
|
||||
<strong>Output:</strong> ["int main()","{ "," ","int a, b, c;","a = b + c;","}"]
|
||||
<strong>Explanation:</strong> The line by line code is visualized as below:
|
||||
/*Test program */
|
||||
int main()
|
||||
{
|
||||
// variable declaration
|
||||
int a, b, c;
|
||||
/* This is a test
|
||||
multiline
|
||||
comment for
|
||||
testing */
|
||||
a = b + c;
|
||||
}
|
||||
The string /* denotes a block comment, including line 1 and lines 6-9. The string // denotes line 4 as comments.
|
||||
The line by line output code is visualized as below:
|
||||
int main()
|
||||
{
|
||||
|
||||
int a, b, c;
|
||||
a = b + c;
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> source = ["a/*comment", "line", "more_comment*/b"]
|
||||
<strong>Output:</strong> ["ab"]
|
||||
<strong>Explanation:</strong> The original source string is "a/*comment\nline\nmore_comment*/b", where we have bolded the newline characters. After deletion, the implicit newline characters are deleted, leaving the string "ab", which when delimited by newline characters becomes ["ab"].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= source.length <= 100</code></li>
|
||||
<li><code>0 <= source[i].length <= 80</code></li>
|
||||
<li><code>source[i]</code> consists of printable <strong>ASCII</strong> characters.</li>
|
||||
<li>Every open block comment is eventually closed.</li>
|
||||
<li>There are no single-quote or double-quote in the input.</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