mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-02 05:13:29 +08:00
update
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length.</p>
|
||||
|
||||
<p>As long as <code>nums</code> is <strong>not</strong> empty, you must repetitively:</p>
|
||||
|
||||
<ul>
|
||||
<li>Find the minimum number in <code>nums</code> and remove it.</li>
|
||||
<li>Find the maximum number in <code>nums</code> and remove it.</li>
|
||||
<li>Calculate the average of the two removed numbers.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>average</strong> of two numbers <code>a</code> and <code>b</code> is <code>(a + b) / 2</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the average of <code>2</code> and <code>3</code> is <code>(2 + 3) / 2 = 2.5</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the number of <strong>distinct</strong> averages calculated using the above process</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that when there is a tie for a minimum or maximum number, any can be removed.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,1,4,0,3,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
1. Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums = [4,1,4,3].
|
||||
2. Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums = [4,3].
|
||||
3. Remove 3 and 4, and the average is (3 + 4) / 2 = 3.5.
|
||||
Since there are 2 distinct numbers among 2.5, 2.5, and 3.5, we return 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,100]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
There is only one average to be calculated after removing 1 and 100, so we return 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>nums.length</code> is even.</li>
|
||||
<li><code>0 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given a string <code>s</code> and a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>Select a set of <strong>non-overlapping</strong> substrings from the string <code>s</code> that satisfy the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>length</strong> of each substring is <strong>at least</strong> <code>k</code>.</li>
|
||||
<li>Each substring is a <strong>palindrome</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of substrings in an optimal selection</em>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abaccdbbd", k = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can select the substrings underlined in s = "<u><strong>aba</strong></u>cc<u><strong>dbbd</strong></u>". Both "aba" and "dbbd" are palindromes and have a length of at least k = 3.
|
||||
It can be shown that we cannot find a selection with more than two valid substrings.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "adbcda", k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no palindrome substring of length at least 2 in the string.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= s.length <= 2000</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</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>subarrays</strong> of </em><code>nums</code><em> where the least common multiple of the subarray's elements is </em><code>k</code>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
|
||||
|
||||
<p>The <strong>least common multiple of an array</strong> is the smallest positive integer that is divisible by all the array elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,6,2,7,1], k = 6
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The subarrays of nums where 6 is the least common multiple of all the subarray's elements are:
|
||||
- [<u><strong>3</strong></u>,<u><strong>6</strong></u>,2,7,1]
|
||||
- [<u><strong>3</strong></u>,<u><strong>6</strong></u>,<u><strong>2</strong></u>,7,1]
|
||||
- [3,<u><strong>6</strong></u>,2,7,1]
|
||||
- [3,<u><strong>6</strong></u>,<u><strong>2</strong></u>,7,1]
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3], k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no subarrays of nums where 2 is the least common multiple of all the subarray's elements.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i], k <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,69 @@
|
||||
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</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 tree.</p>
|
||||
|
||||
<p>At every node <code>i</code>, there is a gate. You are also given an array of even integers <code>amount</code>, where <code>amount[i]</code> represents:</p>
|
||||
|
||||
<ul>
|
||||
<li>the price needed to open the gate at node <code>i</code>, if <code>amount[i]</code> is negative, or,</li>
|
||||
<li>the cash reward obtained on opening the gate at node <code>i</code>, otherwise.</li>
|
||||
</ul>
|
||||
|
||||
<p>The game goes on as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>Initially, Alice is at node <code>0</code> and Bob is at node <code>bob</code>.</li>
|
||||
<li>At every second, Alice and Bob <b>each</b> move to an adjacent node. Alice moves towards some <strong>leaf node</strong>, while Bob moves towards node <code>0</code>.</li>
|
||||
<li>For <strong>every</strong> node along their path, Alice and Bob either spend money to open the gate at that node, or accept the reward. Note that:
|
||||
<ul>
|
||||
<li>If the gate is <strong>already open</strong>, no price will be required, nor will there be any cash reward.</li>
|
||||
<li>If Alice and Bob reach the node <strong>simultaneously</strong>, they share the price/reward for opening the gate there. In other words, if the price to open the gate is <code>c</code>, then both Alice and Bob pay <code>c / 2</code> each. Similarly, if the reward at the gate is <code>c</code>, both of them receive <code>c / 2</code> each.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>If Alice reaches a leaf node, she stops moving. Similarly, if Bob reaches node <code>0</code>, he stops moving. Note that these events are <strong>independent</strong> of each other.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the <strong>maximum</strong> net income Alice can have if she travels towards the optimal leaf node.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg1.png" style="width: 275px; height: 275px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
The above diagram represents the given tree. The game goes as follows:
|
||||
- Alice is initially on node 0, Bob on node 3. They open the gates of their respective nodes.
|
||||
Alice's net income is now -2.
|
||||
- Both Alice and Bob move to node 1.
|
||||
Since they reach here simultaneously, they open the gate together and share the reward.
|
||||
Alice's net income becomes -2 + (4 / 2) = 0.
|
||||
- Alice moves on to node 3. Since Bob already opened its gate, Alice's income remains unchanged.
|
||||
Bob moves on to node 0, and stops moving.
|
||||
- Alice moves on to node 4 and opens the gate there. Her net income becomes 0 + 6 = 6.
|
||||
Now, neither Alice nor Bob can make any further moves, and the game ends.
|
||||
It is not possible for Alice to get a higher net income.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg2.png" style="width: 250px; height: 78px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1]], bob = 1, amount = [-7280,2350]
|
||||
<strong>Output:</strong> -7280
|
||||
<strong>Explanation:</strong>
|
||||
Alice follows the path 0->1 whereas Bob follows the path 1->0.
|
||||
Thus, Alice opens the gate at node 0 only. Hence, her net income is -7280.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
<li><code>1 <= bob < n</code></li>
|
||||
<li><code>amount.length == n</code></li>
|
||||
<li><code>amount[i]</code> is an <strong>even</strong> integer in the range <code>[-10<sup>4</sup>, 10<sup>4</sup>]</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a string, <code>message</code>, and a positive integer, <code>limit</code>.</p>
|
||||
|
||||
<p>You must <strong>split</strong> <code>message</code> into one or more <strong>parts</strong> based on <code>limit</code>. Each resulting part should have the suffix <code>"<a/b>"</code>, where <code>"b"</code> is to be <strong>replaced</strong> with the total number of parts and <code>"a"</code> is to be <strong>replaced</strong> with the index of the part, starting from <code>1</code> and going up to <code>b</code>. Additionally, the length of each resulting part (including its suffix) should be <strong>equal</strong> to <code>limit</code>, except for the last part whose length can be <strong>at most</strong> <code>limit</code>.</p>
|
||||
|
||||
<p>The resulting parts should be formed such that when their suffixes are removed and they are all concatenated <strong>in order</strong>, they should be equal to <code>message</code>. Also, the result should contain as few parts as possible.</p>
|
||||
|
||||
<p>Return<em> the parts </em><code>message</code><em> would be split into as an array of strings</em>. If it is impossible to split <code>message</code> as required, return<em> an empty array</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> message = "this is really a very awesome message", limit = 9
|
||||
<strong>Output:</strong> ["thi<1/14>","s i<2/14>","s r<3/14>","eal<4/14>","ly <5/14>","a v<6/14>","ery<7/14>"," aw<8/14>","eso<9/14>","me<10/14>"," m<11/14>","es<12/14>","sa<13/14>","ge<14/14>"]
|
||||
<strong>Explanation:</strong>
|
||||
The first 9 parts take 3 characters each from the beginning of message.
|
||||
The next 5 parts take 2 characters each to finish splitting message.
|
||||
In this example, each part, including the last, has length 9.
|
||||
It can be shown it is not possible to split message into less than 14 parts.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> message = "short message", limit = 15
|
||||
<strong>Output:</strong> ["short mess<1/2>","age<2/2>"]
|
||||
<strong>Explanation:</strong>
|
||||
Under the given constraints, the string can be split into two parts:
|
||||
- The first part comprises of the first 10 characters, and has a length 15.
|
||||
- The next part comprises of the last 3 characters, and has a length 8.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= message.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>message</code> consists only of lowercase English letters and <code>' '</code>.</li>
|
||||
<li><code>1 <= limit <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>You are given a non-negative floating point number rounded to two decimal places <code>celsius</code>, that denotes the <strong>temperature in Celsius</strong>.</p>
|
||||
|
||||
<p>You should convert Celsius into <strong>Kelvin</strong> and <strong>Fahrenheit</strong> and return it as an array <code>ans = [kelvin, fahrenheit]</code>.</p>
|
||||
|
||||
<p>Return <em>the array <code>ans</code>. </em>Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.</p>
|
||||
|
||||
<p><strong>Note that:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>Kelvin = Celsius + 273.15</code></li>
|
||||
<li><code>Fahrenheit = Celsius * 1.80 + 32.00</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> celsius = 36.50
|
||||
<strong>Output:</strong> [309.65000,97.70000]
|
||||
<strong>Explanation:</strong> Temperature at 36.50 Celsius converted in Kelvin is 309.65 and converted in Fahrenheit is 97.70.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> celsius = 122.11
|
||||
<strong>Output:</strong> [395.26000,251.79800]
|
||||
<strong>Explanation:</strong> Temperature at 122.11 Celsius converted in Kelvin is 395.26 and converted in Fahrenheit is 251.798.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= celsius <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
|
||||
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
|
||||
</ul>
|
||||
|
||||
<p>This can be performed any number of times.</p>
|
||||
|
||||
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
|
||||
|
||||
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
One possible valid good string is "011".
|
||||
It can be constructed as follows: "" -> "0" -> "01" -> "011".
|
||||
All binary strings from "000" to "111" are good strings in this example.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= zero, one <= low</code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>You are given the <code>root</code> of a binary tree with <strong>unique values</strong>.</p>
|
||||
|
||||
<p>In one operation, you can choose any two nodes <strong>at the same level</strong> and swap their values.</p>
|
||||
|
||||
<p>Return <em>the minimum number of operations needed to make the values at each level sorted in a <strong>strictly increasing order</strong></em>.</p>
|
||||
|
||||
<p>The <strong>level</strong> of a node is the number of edges along the path between it and the root node<em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png" style="width: 500px; height: 324px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
- Swap 4 and 3. The 2<sup>nd</sup> level becomes [3,4].
|
||||
- Swap 7 and 5. The 3<sup>rd</sup> level becomes [5,6,8,7].
|
||||
- Swap 8 and 7. The 3<sup>rd</sup> level becomes [5,6,7,8].
|
||||
We used 3 operations so return 3.
|
||||
It can be proven that 3 is the minimum number of operations needed.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png" style="width: 400px; height: 303px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,3,2,7,6,5,4]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
- Swap 3 and 2. The 2<sup>nd</sup> level becomes [2,3].
|
||||
- Swap 7 and 4. The 3<sup>rd</sup> level becomes [4,6,5,7].
|
||||
- Swap 6 and 5. The 3<sup>rd</sup> level becomes [4,5,6,7].
|
||||
We used 3 operations so return 3.
|
||||
It can be proven that 3 is the minimum number of operations needed.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png" style="width: 400px; height: 274px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3,4,5,6]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Each level is already sorted in increasing order so return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
<li>All the values of the tree are <strong>unique</strong>.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user