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,35 @@
|
||||
<p>You are given two positive integer arrays <code>nums</code> and <code>numsDivide</code>. You can delete any number of elements from <code>nums</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of deletions such that the <strong>smallest</strong> element in </em><code>nums</code><em> <strong>divides</strong> all the elements of </em><code>numsDivide</code>. If this is not possible, return <code>-1</code>.</p>
|
||||
|
||||
<p>Note that an integer <code>x</code> divides <code>y</code> if <code>y % x == 0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,2,4,3], numsDivide = [9,6,9,3,15]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
The smallest element in [2,3,2,4,3] is 2, which does not divide all the elements of numsDivide.
|
||||
We use 2 deletions to delete the elements in nums that are equal to 2 which makes nums = [3,4,3].
|
||||
The smallest element in [3,4,3] is 3, which divides all the elements of numsDivide.
|
||||
It can be shown that 2 is the minimum number of deletions needed.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,3,6], numsDivide = [8,2,6,10]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong>
|
||||
We want the smallest element in nums to divide all the elements of numsDivide.
|
||||
There is no way to delete elements from nums to allow this.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length, numsDivide.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], numsDivide[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <strong>positive</strong> integers. You can choose two indices <code>i</code> and <code>j</code>, such that <code>i != j</code>, and the sum of digits of the number <code>nums[i]</code> is equal to that of <code>nums[j]</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> value of </em><code>nums[i] + nums[j]</code><em> that you can obtain over all possible indices </em><code>i</code><em> and </em><code>j</code><em> that satisfy the conditions.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [18,43,36,13,7]
|
||||
<strong>Output:</strong> 54
|
||||
<strong>Explanation:</strong> The pairs (i, j) that satisfy the conditions are:
|
||||
- (0, 2), both numbers have a sum of digits equal to 9, and their sum is 18 + 36 = 54.
|
||||
- (1, 4), both numbers have a sum of digits equal to 7, and their sum is 43 + 7 = 50.
|
||||
So the maximum sum that we can obtain is 54.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,12,19,14]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> There are no two numbers that satisfy the conditions, so we return -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, you may do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose <strong>two</strong> integers in <code>nums</code> that are <strong>equal</strong>.</li>
|
||||
<li>Remove both integers from <code>nums</code>, forming a <strong>pair</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The operation is done on <code>nums</code> as many times as possible.</p>
|
||||
|
||||
<p>Return <em>a <strong>0-indexed</strong> integer array </em><code>answer</code><em> of size </em><code>2</code><em> where </em><code>answer[0]</code><em> is the number of pairs that are formed and </em><code>answer[1]</code><em> is the number of leftover integers in </em><code>nums</code><em> after doing the operation as many times as possible</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,2,1,3,2,2]
|
||||
<strong>Output:</strong> [3,1]
|
||||
<strong>Explanation:</strong>
|
||||
Form a pair with nums[0] and nums[3] and remove them from nums. Now, nums = [3,2,3,2,2].
|
||||
Form a pair with nums[0] and nums[2] and remove them from nums. Now, nums = [2,2,2].
|
||||
Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [2].
|
||||
No more pairs can be formed. A total of 3 pairs have been formed, and there is 1 number leftover in nums.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1]
|
||||
<strong>Output:</strong> [1,0]
|
||||
<strong>Explanation:</strong> Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [].
|
||||
No more pairs can be formed. A total of 1 pair has been formed, and there are 0 numbers leftover in nums.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0]
|
||||
<strong>Output:</strong> [0,1]
|
||||
<strong>Explanation:</strong> No pairs can be formed, and there is 1 number leftover in nums.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>0 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,57 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of strings <code>nums</code>, where each string is of <strong>equal length</strong> and consists of only digits.</p>
|
||||
|
||||
<p>You are also given a <strong>0-indexed</strong> 2D integer array <code>queries</code> where <code>queries[i] = [k<sub>i</sub>, trim<sub>i</sub>]</code>. For each <code>queries[i]</code>, you need to:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Trim</strong> each number in <code>nums</code> to its <strong>rightmost</strong> <code>trim<sub>i</sub></code> digits.</li>
|
||||
<li>Determine the <strong>index</strong> of the <code>k<sub>i</sub><sup>th</sup></code> smallest trimmed number in <code>nums</code>. If two trimmed numbers are equal, the number with the <strong>lower</strong> index is considered to be smaller.</li>
|
||||
<li>Reset each number in <code>nums</code> to its original length.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an array </em><code>answer</code><em> of the same length as </em><code>queries</code>,<em> where </em><code>answer[i]</code><em> is the answer to the </em><code>i<sup>th</sup></code><em> query.</em></p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>To trim to the rightmost <code>x</code> digits means to keep removing the leftmost digit, until only <code>x</code> digits remain.</li>
|
||||
<li>Strings in <code>nums</code> may contain leading zeros.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = ["102","473","251","814"], queries = [[1,1],[2,3],[4,2],[1,2]]
|
||||
<strong>Output:</strong> [2,2,1,0]
|
||||
<strong>Explanation:</strong>
|
||||
1. After trimming to the last digit, nums = ["2","3","1","4"]. The smallest number is 1 at index 2.
|
||||
2. Trimmed to the last 3 digits, nums is unchanged. The 2<sup>nd</sup> smallest number is 251 at index 2.
|
||||
3. Trimmed to the last 2 digits, nums = ["02","73","51","14"]. The 4<sup>th</sup> smallest number is 73.
|
||||
4. Trimmed to the last 2 digits, the smallest number is 2 at index 0.
|
||||
Note that the trimmed number "02" is evaluated as 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = ["24","37","96","04"], queries = [[2,1],[2,2]]
|
||||
<strong>Output:</strong> [3,0]
|
||||
<strong>Explanation:</strong>
|
||||
1. Trimmed to the last digit, nums = ["4","7","6","4"]. The 2<sup>nd</sup> smallest number is 4 at index 3.
|
||||
There are two occurrences of 4, but the one at index 0 is considered smaller than the one at index 3.
|
||||
2. Trimmed to the last 2 digits, nums is unchanged. The 2<sup>nd</sup> smallest number is 24.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i].length <= 100</code></li>
|
||||
<li><code>nums[i]</code> consists of only digits.</li>
|
||||
<li>All <code>nums[i].length</code> are <strong>equal</strong>.</li>
|
||||
<li><code>1 <= queries.length <= 100</code></li>
|
||||
<li><code>queries[i].length == 2</code></li>
|
||||
<li><code>1 <= k<sub>i</sub> <= nums.length</code></li>
|
||||
<li><code>1 <= trim<sub>i</sub> <= nums[i].length</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user