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,37 @@
|
||||
<p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
|
||||
|
||||
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,2,-3,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> There is no a single valid k, we return -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>-1000 <= nums[i] <= 1000</code></li>
|
||||
<li><code>nums[i] != 0</code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>Given a positive integer <code>n</code>, there exists a <strong>0-indexed</strong> array called <code>powers</code>, composed of the <strong>minimum</strong> number of powers of <code>2</code> that sum to <code>n</code>. The array is sorted in <strong>non-decreasing</strong> order, and there is <strong>only one</strong> way to form the array.</p>
|
||||
|
||||
<p>You are also given a <strong>0-indexed</strong> 2D integer array <code>queries</code>, where <code>queries[i] = [left<sub>i</sub>, right<sub>i</sub>]</code>. Each <code>queries[i]</code> represents a query where you have to find the product of all <code>powers[j]</code> with <code>left<sub>i</sub> <= j <= right<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Return<em> an array </em><code>answers</code><em>, equal in length to </em><code>queries</code><em>, where </em><code>answers[i]</code><em> is the answer to the </em><code>i<sup>th</sup></code><em> query</em>. Since the answer to the <code>i<sup>th</sup></code> query may be too large, each <code>answers[i]</code> should be returned <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> n = 15, queries = [[0,1],[2,2],[0,3]]
|
||||
<strong>Output:</strong> [2,4,64]
|
||||
<strong>Explanation:</strong>
|
||||
For n = 15, powers = [1,2,4,8]. It can be shown that powers cannot be a smaller size.
|
||||
Answer to 1st query: powers[0] * powers[1] = 1 * 2 = 2.
|
||||
Answer to 2nd query: powers[2] = 4.
|
||||
Answer to 3rd query: powers[0] * powers[1] * powers[2] * powers[3] = 1 * 2 * 4 * 8 = 64.
|
||||
Each answer modulo 10<sup>9</sup> + 7 yields the same answer, so [2,4,64] is returned.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, queries = [[0,0]]
|
||||
<strong>Output:</strong> [2]
|
||||
<strong>Explanation:</strong>
|
||||
For n = 2, powers = [2].
|
||||
The answer to the only query is powers[0] = 2. The answer modulo 10<sup>9</sup> + 7 is the same, so [2] is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> < powers.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>.</p>
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code><font face="monospace">nums</font></code> of length <code>n</code> where <code>nums[i]</code> represents the value of the <code>i<sup>th</sup></code> node. You are also 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>You are allowed to <strong>delete</strong> some edges, splitting the tree into multiple connected components. Let the <strong>value</strong> of a component be the sum of <strong>all</strong> <code>nums[i]</code> for which node <code>i</code> is in the component.</p>
|
||||
|
||||
<p>Return<em> the <strong>maximum</strong> number of edges you can delete, such that every connected component in the tree has the same value.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/26/diagramdrawio.png" style="width: 441px; height: 351px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,2,2,2,6], edges = [[0,1],[1,2],[1,3],[3,4]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The above figure shows how we can delete the edges [0,1] and [3,4]. The created components are nodes [0], [1,2,3] and [4]. The sum of the values in each component equals 6. It can be proven that no better deletion exists, so the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2], edges = []
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no edges to be deleted.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>nums.length == n</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= edges[i][0], edges[i][1] <= n - 1</code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>You are given an array <code>nums</code> consisting of <strong>positive</strong> integers.</p>
|
||||
|
||||
<p>You have to take each integer in the array, <strong>reverse its digits</strong>, and add it to the end of the array. You should apply this operation to the original integers in <code>nums</code>.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>distinct</strong> integers in the final array</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,13,10,12,31]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> After including the reverse of each number, the resulting array is [1,13,10,12,31,<u>1,31,1,21,13</u>].
|
||||
The reversed integers that were added to the end of the array are underlined. Note that for the integer 10, after reversing it, it becomes 01 which is just 1.
|
||||
The number of distinct integers in this array is 6 (The numbers 1, 10, 12, 13, 21, and 31).</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,2,2]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> After including the reverse of each number, the resulting array is [2,2,2,<u>2,2,2</u>].
|
||||
The number of distinct integers in this array is 1 (The number 2).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>Given a <strong>non-negative</strong> integer <code>num</code>, return <code>true</code><em> if </em><code>num</code><em> can be expressed as the sum of any <strong>non-negative</strong> integer and its reverse, or </em><code>false</code><em> otherwise.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 443
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> 172 + 271 = 443 so we return true.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 63
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> 63 cannot be expressed as the sum of a non-negative integer and its reverse so we return false.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 181
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> 140 + 041 = 181 so we return true. Note that when a number is reversed, there may be leading zeros.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= num <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> comprising of <code>n</code> non-negative integers.</p>
|
||||
|
||||
<p>In one operation, you must:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose an integer <code>i</code> such that <code>1 <= i < n</code> and <code>nums[i] > 0</code>.</li>
|
||||
<li>Decrease <code>nums[i]</code> by 1.</li>
|
||||
<li>Increase <code>nums[i - 1]</code> by 1.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the <strong>minimum</strong> possible value of the <strong>maximum</strong> integer of </em><code>nums</code><em> after performing <strong>any</strong> number of operations</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,7,1,6]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
One set of optimal operations is as follows:
|
||||
1. Choose i = 1, and nums becomes [4,6,1,6].
|
||||
2. Choose i = 3, and nums becomes [4,6,2,5].
|
||||
3. Choose i = 1, and nums becomes [5,5,2,5].
|
||||
The maximum integer of nums is 5. It can be shown that the maximum number cannot be less than 5.
|
||||
Therefore, we return 5.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,1]
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong>
|
||||
It is optimal to leave nums as is, and since 10 is the maximum value, we return 10.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a string of length <code>5</code> called <code>time</code>, representing the current time on a digital clock in the format <code>"hh:mm"</code>. The <strong>earliest</strong> possible time is <code>"00:00"</code> and the <strong>latest</strong> possible time is <code>"23:59"</code>.</p>
|
||||
|
||||
<p>In the string <code>time</code>, the digits represented by the <code>?</code> symbol are <strong>unknown</strong>, and must be <strong>replaced</strong> with a digit from <code>0</code> to <code>9</code>.</p>
|
||||
|
||||
<p>Return<em> an integer </em><code>answer</code><em>, the number of valid clock times that can be created by replacing every </em><code>?</code><em> with a digit from </em><code>0</code><em> to </em><code>9</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> time = "?5:00"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can replace the ? with either a 0 or 1, producing "05:00" or "15:00". Note that we cannot replace it with a 2, since the time "25:00" is invalid. In total, we have two choices.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> time = "0?:0?"
|
||||
<strong>Output:</strong> 100
|
||||
<strong>Explanation:</strong> Each ? can be replaced by any digit from 0 to 9, so we have 100 total choices.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> time = "??:??"
|
||||
<strong>Output:</strong> 1440
|
||||
<strong>Explanation:</strong> There are 24 possible choices for the hours, and 60 possible choices for the minutes. In total, we have 24 * 60 = 1440 choices.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>time</code> is a valid string of length <code>5</code> in the format <code>"hh:mm"</code>.</li>
|
||||
<li><code>"00" <= hh <= "23"</code></li>
|
||||
<li><code>"00" <= mm <= "59"</code></li>
|
||||
<li>Some of the digits might be replaced with <code>'?'</code> and need to be replaced with digits from <code>0</code> to <code>9</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>You are given an integer array <code>nums</code> and two integers <code>minK</code> and <code>maxK</code>.</p>
|
||||
|
||||
<p>A <strong>fixed-bound subarray</strong> of <code>nums</code> is a subarray that satisfies the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>minimum</strong> value in the subarray is equal to <code>minK</code>.</li>
|
||||
<li>The <strong>maximum</strong> value in the subarray is equal to <code>maxK</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>number</strong> of fixed-bound subarrays</em>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,5,2,7,5], minK = 1, maxK = 5
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1,1], minK = 1, maxK = 1
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], minK, maxK <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user