mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-03 06:22:54 +08:00
update
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
161
leetcode/originData/construct-product-matrix.json
Normal file
161
leetcode/originData/construct-product-matrix.json
Normal file
File diff suppressed because one or more lines are too long
163
leetcode/originData/count-of-sub-multisets-with-bounded-sum.json
Normal file
163
leetcode/originData/count-of-sub-multisets-with-bounded-sum.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
160
leetcode/originData/last-visited-integers.json
Normal file
160
leetcode/originData/last-visited-integers.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
180
leetcode/originData/minimum-processing-time.json
Normal file
180
leetcode/originData/minimum-processing-time.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,44 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>You can do the following operation on the array <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose any two distinct indices <code>i</code> and <code>j</code> and <strong>simultaneously</strong> update the values of <code>nums[i]</code> to <code>(nums[i] AND nums[j])</code> and <code>nums[j]</code> to <code>(nums[i] OR nums[j])</code>. Here, <code>OR</code> denotes the bitwise <code>OR</code> operation, and <code>AND</code> denotes the bitwise <code>AND</code> operation.</li>
|
||||
</ul>
|
||||
|
||||
<p>You have to choose <code>k</code> elements from the final array and calculate the sum of their <strong>squares</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> sum of squares you can achieve</em>.</p>
|
||||
|
||||
<p>Since the answer can be very 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> nums = [2,6,5,8], k = 2
|
||||
<strong>Output:</strong> 261
|
||||
<strong>Explanation:</strong> We can do the following operations on the array:
|
||||
- Choose i = 0 and j = 3, then change nums[0] to (2 AND 8) = 0 and nums[3] to (2 OR 8) = 10. The resulting array is nums = [0,6,5,10].
|
||||
- Choose i = 2 and j = 3, then change nums[2] to (5 AND 10) = 0 and nums[3] to (5 OR 10) = 15. The resulting array is nums = [0,6,0,15].
|
||||
We can choose the elements 15 and 6 from the final array. The sum of squares is 15<sup>2</sup> + 6<sup>2</sup> = 261.
|
||||
It can be shown that this is the maximum value we can get.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,5,4,7], k = 3
|
||||
<strong>Output:</strong> 90
|
||||
<strong>Explanation:</strong> We do not need to apply any operations.
|
||||
We can choose the elements 7, 5, and 4 with a sum of squares: 7<sup>2</sup> + 5<sup>2</sup> + 4<sup>2</sup> = 90.
|
||||
It can be shown that this is the maximum value we can get.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given two <strong>0-indexed</strong> binary strings <code>s1</code> and <code>s2</code>, both of length <code>n</code>, and a positive integer <code>x</code>.</p>
|
||||
|
||||
<p>You can perform any of the following operations on the string <code>s1</code> <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose two indices <code>i</code> and <code>j</code>, and flip both <code>s1[i]</code> and <code>s1[j]</code>. The cost of this operation is <code>x</code>.</li>
|
||||
<li>Choose an index <code>i</code> such that <code>i < n - 1</code> and flip both <code>s1[i]</code> and <code>s1[i + 1]</code>. The cost of this operation is <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> cost needed to make the strings </em><code>s1</code><em> and </em><code>s2</code><em> equal, or return </em><code>-1</code><em> if it is impossible.</em></p>
|
||||
|
||||
<p><strong>Note</strong> that flipping a character means changing it from <code>0</code> to <code>1</code> or vice-versa.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "1100011000", s2 = "0101001010", x = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We can do the following operations:
|
||||
- Choose i = 3 and apply the second operation. The resulting string is s1 = "110<u><strong>11</strong></u>11000".
|
||||
- Choose i = 4 and apply the second operation. The resulting string is s1 = "1101<strong><u>00</u></strong>1000".
|
||||
- Choose i = 0 and j = 8 and apply the first operation. The resulting string is s1 = "<u><strong>0</strong></u>1010010<u><strong>1</strong></u>0" = s2.
|
||||
The total cost is 1 + 1 + 2 = 4. It can be shown that it is the minimum cost possible.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "10110", s2 = "00011", x = 4
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It is not possible to make the two strings equal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == s1.length == s2.length</code></li>
|
||||
<li><code>1 <= n, x <= 500</code></li>
|
||||
<li><code>s1</code> and <code>s2</code> consist only of the characters <code>'0'</code> and <code>'1'</code>.</li>
|
||||
</ul>
|
39
leetcode/problem/construct-product-matrix.html
Normal file
39
leetcode/problem/construct-product-matrix.html
Normal file
@@ -0,0 +1,39 @@
|
||||
<p>Given a <strong>0-indexed</strong> 2D integer matrix <code><font face="monospace">grid</font></code><font face="monospace"> </font>of size <code>n * m</code>, we define a <strong>0-indexed</strong> 2D matrix <code>p</code> of size <code>n * m</code> as the <strong>product</strong> matrix of <code>grid</code> if the following condition is met:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each element <code>p[i][j]</code> is calculated as the product of all elements in <code>grid</code> except for the element <code>grid[i][j]</code>. This product is then taken modulo <code><font face="monospace">12345</font></code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the product matrix of</em> <code><font face="monospace">grid</font></code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,2],[3,4]]
|
||||
<strong>Output:</strong> [[24,12],[8,6]]
|
||||
<strong>Explanation:</strong> p[0][0] = grid[0][1] * grid[1][0] * grid[1][1] = 2 * 3 * 4 = 24
|
||||
p[0][1] = grid[0][0] * grid[1][0] * grid[1][1] = 1 * 3 * 4 = 12
|
||||
p[1][0] = grid[0][0] * grid[0][1] * grid[1][1] = 1 * 2 * 4 = 8
|
||||
p[1][1] = grid[0][0] * grid[0][1] * grid[1][0] = 1 * 2 * 3 = 6
|
||||
So the answer is [[24,12],[8,6]].</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[12345],[2],[1]]
|
||||
<strong>Output:</strong> [[2],[0],[0]]
|
||||
<strong>Explanation:</strong> p[0][0] = grid[0][1] * grid[0][2] = 2 * 1 = 2.
|
||||
p[0][1] = grid[0][0] * grid[0][2] = 12345 * 1 = 12345. 12345 % 12345 = 0. So p[0][1] = 0.
|
||||
p[0][2] = grid[0][0] * grid[0][1] = 12345 * 2 = 24690. 24690 % 12345 = 0. So p[0][2] = 0.
|
||||
So the answer is [[2],[0],[0]].</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == grid.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m == grid[i].length <= 10<sup>5</sup></code></li>
|
||||
<li><code>2 <= n * m <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= grid[i][j] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of non-negative integers, and two integers <code>l</code> and <code>r</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>count of sub-multisets</strong> within</em> <code>nums</code> <em>where the sum of elements in each subset falls within the inclusive range of</em> <code>[l, r]</code>.</p>
|
||||
|
||||
<p>Since the answer may be large, return it modulo <code>10<sup>9 </sup>+ 7</code>.</p>
|
||||
|
||||
<p>A <strong>sub-multiset</strong> is an <strong>unordered</strong> collection of elements of the array in which a given value <code>x</code> can occur <code>0, 1, ..., occ[x]</code> times, where <code>occ[x]</code> is the number of occurrences of <code>x</code> in the array.</p>
|
||||
|
||||
<p><strong>Note</strong> that:</p>
|
||||
|
||||
<ul>
|
||||
<li>Two <strong>sub-multisets</strong> are the same if sorting both sub-multisets results in identical multisets.</li>
|
||||
<li>The sum of an <strong>empty</strong> multiset is <code>0</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,2,3], l = 6, r = 6
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The only subset of nums that has a sum of 6 is {1, 2, 3}.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,4,2,7], l = 1, r = 5
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> The subsets of nums that have a sum within the range [1, 5] are {1}, {2}, {4}, {2, 2}, {1, 2}, {1, 4}, and {1, 2, 2}.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1,3,5,2], l = 3, r = 5
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> The subsets of nums that have a sum within the range [3, 5] are {3}, {5}, {1, 2}, {1, 3}, {2, 2}, {2, 3}, {1, 1, 2}, {1, 1, 3}, and {1, 2, 2}.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
|
||||
<li>Sum of <code>nums</code> does not exceed <code>2 * 10<sup>4</sup></code>.</li>
|
||||
<li><code>0 <= l <= r <= 2 * 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>You are given positive integers <code>n</code> and <code>m</code>.</p>
|
||||
|
||||
<p>Define two integers, <code>num1</code> and <code>num2</code>, as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>num1</code>: The sum of all integers in the range <code>[1, n]</code> that are <strong>not divisible</strong> by <code>m</code>.</li>
|
||||
<li><code>num2</code>: The sum of all integers in the range <code>[1, n]</code> that are <strong>divisible</strong> by <code>m</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the integer</em> <code>num1 - num2</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 10, m = 3
|
||||
<strong>Output:</strong> 19
|
||||
<strong>Explanation:</strong> In the given example:
|
||||
- Integers in the range [1, 10] that are not divisible by 3 are [1,2,4,5,7,8,10], num1 is the sum of those integers = 37.
|
||||
- Integers in the range [1, 10] that are divisible by 3 are [3,6,9], num2 is the sum of those integers = 18.
|
||||
We return 37 - 18 = 19 as the answer.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, m = 6
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> In the given example:
|
||||
- Integers in the range [1, 5] that are not divisible by 6 are [1,2,3,4,5], num1 is the sum of those integers = 15.
|
||||
- Integers in the range [1, 5] that are divisible by 6 are [], num2 is the sum of those integers = 0.
|
||||
We return 15 - 0 = 15 as the answer.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, m = 1
|
||||
<strong>Output:</strong> -15
|
||||
<strong>Explanation:</strong> In the given example:
|
||||
- Integers in the range [1, 5] that are not divisible by 1 are [], num1 is the sum of those integers = 0.
|
||||
- Integers in the range [1, 5] that are divisible by 1 are [1,2,3,4,5], num2 is the sum of those integers = 15.
|
||||
We return 0 - 15 = -15 as the answer.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n, m <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> having length <code>n</code>, an integer <code>indexDifference</code>, and an integer <code>valueDifference</code>.</p>
|
||||
|
||||
<p>Your task is to find <strong>two</strong> indices <code>i</code> and <code>j</code>, both in the range <code>[0, n - 1]</code>, that satisfy the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>abs(i - j) >= indexDifference</code>, and</li>
|
||||
<li><code>abs(nums[i] - nums[j]) >= valueDifference</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer array</em> <code>answer</code>, <em>where</em> <code>answer = [i, j]</code> <em>if there are two such indices</em>, <em>and</em> <code>answer = [-1, -1]</code> <em>otherwise</em>. If there are multiple choices for the two indices, return <em>any of them</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong> <code>i</code> and <code>j</code> may be <strong>equal</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
|
||||
<strong>Output:</strong> [0,3]
|
||||
<strong>Explanation:</strong> In this example, i = 0 and j = 3 can be selected.
|
||||
abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.
|
||||
Hence, a valid answer is [0,3].
|
||||
[3,0] is also a valid answer.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1], indexDifference = 0, valueDifference = 0
|
||||
<strong>Output:</strong> [0,0]
|
||||
<strong>Explanation:</strong> In this example, i = 0 and j = 0 can be selected.
|
||||
abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.
|
||||
Hence, a valid answer is [0,0].
|
||||
Other valid answers are [0,1], [1,0], and [1,1].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3], indexDifference = 2, valueDifference = 4
|
||||
<strong>Output:</strong> [-1,-1]
|
||||
<strong>Explanation:</strong> In this example, it can be shown that it is impossible to find two indices that satisfy both conditions.
|
||||
Hence, [-1,-1] is returned.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 100</code></li>
|
||||
<li><code>0 <= nums[i] <= 50</code></li>
|
||||
<li><code>0 <= indexDifference <= 100</code></li>
|
||||
<li><code>0 <= valueDifference <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> having length <code>n</code>, an integer <code>indexDifference</code>, and an integer <code>valueDifference</code>.</p>
|
||||
|
||||
<p>Your task is to find <strong>two</strong> indices <code>i</code> and <code>j</code>, both in the range <code>[0, n - 1]</code>, that satisfy the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>abs(i - j) >= indexDifference</code>, and</li>
|
||||
<li><code>abs(nums[i] - nums[j]) >= valueDifference</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer array</em> <code>answer</code>, <em>where</em> <code>answer = [i, j]</code> <em>if there are two such indices</em>, <em>and</em> <code>answer = [-1, -1]</code> <em>otherwise</em>. If there are multiple choices for the two indices, return <em>any of them</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong> <code>i</code> and <code>j</code> may be <strong>equal</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
|
||||
<strong>Output:</strong> [0,3]
|
||||
<strong>Explanation:</strong> In this example, i = 0 and j = 3 can be selected.
|
||||
abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.
|
||||
Hence, a valid answer is [0,3].
|
||||
[3,0] is also a valid answer.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1], indexDifference = 0, valueDifference = 0
|
||||
<strong>Output:</strong> [0,0]
|
||||
<strong>Explanation:</strong> In this example, i = 0 and j = 0 can be selected.
|
||||
abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.
|
||||
Hence, a valid answer is [0,0].
|
||||
Other valid answers are [0,1], [1,0], and [1,1].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3], indexDifference = 2, valueDifference = 4
|
||||
<strong>Output:</strong> [-1,-1]
|
||||
<strong>Explanation:</strong> In this example, it can be shown that it is impossible to find two indices that satisfy both conditions.
|
||||
Hence, [-1,-1] is returned.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= indexDifference <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= valueDifference <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
41
leetcode/problem/last-visited-integers.html
Normal file
41
leetcode/problem/last-visited-integers.html
Normal file
@@ -0,0 +1,41 @@
|
||||
<p>Given a <strong>0-indexed</strong> array of strings <code>words</code> where <code>words[i]</code> is either a positive integer represented as a string or the string <code>"prev"</code>.</p>
|
||||
|
||||
<p>Start iterating from the beginning of the array; for every <code>"prev"</code> string seen in <code>words</code>, find the <strong>last visited integer</strong> in <code>words</code> which is defined as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>Let <code>k</code> be the number of consecutive <code>"prev"</code> strings seen so far (containing the current string). Let <code>nums</code> be the <strong>0-indexed </strong>array of <strong>integers</strong> seen so far and <code>nums_reverse</code> be the reverse of <code>nums</code>, then the integer at <code>(k - 1)<sup>th</sup></code> index of <code>nums_reverse</code> will be the <strong>last visited integer</strong> for this <code>"prev"</code>.</li>
|
||||
<li>If <code>k</code> is <strong>greater</strong> than the total visited integers, then the last visited integer will be <code>-1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer array containing the last visited integers.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["1","2","prev","prev","prev"]
|
||||
<strong>Output:</strong> [2,1,-1]
|
||||
<strong>Explanation:</strong>
|
||||
For "prev" at index = 2, last visited integer will be 2 as here the number of consecutive "prev" strings is 1, and in the array reverse_nums, 2 will be the first element.
|
||||
For "prev" at index = 3, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.
|
||||
For "prev" at index = 4, last visited integer will be -1 as there are a total of three consecutive "prev" strings including this "prev" which are visited, but the total number of integers visited is two.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["1","prev","2","prev","prev"]
|
||||
<strong>Output:</strong> [1,2,1]
|
||||
<strong>Explanation:</strong>
|
||||
For "prev" at index = 1, last visited integer will be 1.
|
||||
For "prev" at index = 3, last visited integer will be 2.
|
||||
For "prev" at index = 4, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 100</code></li>
|
||||
<li><code>words[i] == "prev"</code> or <code>1 <= int(words[i]) <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>You are given an integer <code>n</code>, a <strong>0-indexed</strong> string array <code>words</code>, and a <strong>0-indexed</strong> <strong>binary</strong> array <code>groups</code>, both arrays having length <code>n</code>.</p>
|
||||
|
||||
<p>You need to select the <strong>longest</strong> <strong>subsequence</strong> from an array of indices <code>[0, 1, ..., n - 1]</code>, such that for the subsequence denoted as <code>[i<sub>0</sub>, i<sub>1</sub>, ..., i<sub>k - 1</sub>]</code> having length <code>k</code>, <code>groups[i<sub>j</sub>] != groups[i<sub>j + 1</sub>]</code>, for each <code>j</code> where <code>0 < j + 1 < k</code>.</p>
|
||||
|
||||
<p>Return <em>a string array containing the words corresponding to the indices <strong>(in order)</strong> in the selected subsequence</em>. If there are multiple answers, return<em> any of them</em>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of an array is a new array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.</p>
|
||||
|
||||
<p><strong>Note:</strong> strings in <code>words</code> may be <strong>unequal</strong> in length.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, words = ["e","a","b"], groups = [0,0,1]
|
||||
<strong>Output:</strong> ["e","b"]
|
||||
<strong>Explanation: </strong>A subsequence that can be selected is [0,2] because groups[0] != groups[2].
|
||||
So, a valid answer is [words[0],words[2]] = ["e","b"].
|
||||
Another subsequence that can be selected is [1,2] because groups[1] != groups[2].
|
||||
This results in [words[1],words[2]] = ["a","b"].
|
||||
It is also a valid answer.
|
||||
It can be shown that the length of the longest subsequence of indices that satisfies the condition is 2.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, words = ["a","b","c","d"], groups = [1,0,1,1]
|
||||
<strong>Output:</strong> ["a","b","c"]
|
||||
<strong>Explanation:</strong> A subsequence that can be selected is [0,1,2] because groups[0] != groups[1] and groups[1] != groups[2].
|
||||
So, a valid answer is [words[0],words[1],words[2]] = ["a","b","c"].
|
||||
Another subsequence that can be selected is [0,1,3] because groups[0] != groups[1] and groups[1] != groups[3].
|
||||
This results in [words[0],words[1],words[3]] = ["a","b","d"].
|
||||
It is also a valid answer.
|
||||
It can be shown that the length of the longest subsequence of indices that satisfies the condition is 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == words.length == groups.length <= 100</code></li>
|
||||
<li><code>1 <= words[i].length <= 10</code></li>
|
||||
<li><code>0 <= groups[i] < 2</code></li>
|
||||
<li><code>words</code> consists of <strong>distinct</strong> strings.</li>
|
||||
<li><code>words[i]</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>You are given an integer <code>n</code>, a <strong>0-indexed</strong> string array <code>words</code>, and a <strong>0-indexed</strong> array <code>groups</code>, both arrays having length <code>n</code>.</p>
|
||||
|
||||
<p>The <strong>hamming distance</strong> between two strings of equal length is the number of positions at which the corresponding characters are <strong>different</strong>.</p>
|
||||
|
||||
<p>You need to select the <strong>longest</strong> <strong>subsequence</strong> from an array of indices <code>[0, 1, ..., n - 1]</code>, such that for the subsequence denoted as <code>[i<sub>0</sub>, i<sub>1</sub>, ..., i<sub>k - 1</sub>]</code> having length <code>k</code>, the following holds:</p>
|
||||
|
||||
<ul>
|
||||
<li>For <strong>adjacent</strong> indices in the subsequence, their corresponding groups are <strong>unequal</strong>, i.e., <code>groups[i<sub>j</sub>] != groups[i<sub>j + 1</sub>]</code>, for each <code>j</code> where <code>0 < j + 1 < k</code>.</li>
|
||||
<li><code>words[i<sub>j</sub>]</code> and <code>words[i<sub>j + 1</sub>]</code> are <strong>equal</strong> in length, and the <strong>hamming distance</strong> between them is <code>1</code>, where <code>0 < j + 1 < k</code>, for all indices in the subsequence.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>a string array containing the words corresponding to the indices <strong>(in order)</strong> in the selected subsequence</em>. If there are multiple answers, return <em>any of them</em>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of an array is a new array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.</p>
|
||||
|
||||
<p><strong>Note:</strong> strings in <code>words</code> may be <strong>unequal</strong> in length.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, words = ["bab","dab","cab"], groups = [1,2,2]
|
||||
<strong>Output:</strong> ["bab","cab"]
|
||||
<strong>Explanation:</strong> A subsequence that can be selected is [0,2].
|
||||
- groups[0] != groups[2]
|
||||
- words[0].length == words[2].length, and the hamming distance between them is 1.
|
||||
So, a valid answer is [words[0],words[2]] = ["bab","cab"].
|
||||
Another subsequence that can be selected is [0,1].
|
||||
- groups[0] != groups[1]
|
||||
- words[0].length == words[1].length, and the hamming distance between them is 1.
|
||||
So, another valid answer is [words[0],words[1]] = ["bab","dab"].
|
||||
It can be shown that the length of the longest subsequence of indices that satisfies the conditions is 2. </pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, words = ["a","b","c","d"], groups = [1,2,3,4]
|
||||
<strong>Output:</strong> ["a","b","c","d"]
|
||||
<strong>Explanation:</strong> We can select the subsequence [0,1,2,3].
|
||||
It satisfies both conditions.
|
||||
Hence, the answer is [words[0],words[1],words[2],words[3]] = ["a","b","c","d"].
|
||||
It has the longest length among all subsequences of indices that satisfy the conditions.
|
||||
Hence, it is the only answer.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == words.length == groups.length <= 1000</code></li>
|
||||
<li><code>1 <= words[i].length <= 10</code></li>
|
||||
<li><code>1 <= groups[i] <= n</code></li>
|
||||
<li><code>words</code> consists of <strong>distinct</strong> strings.</li>
|
||||
<li><code>words[i]</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
40
leetcode/problem/minimum-processing-time.html
Normal file
40
leetcode/problem/minimum-processing-time.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>You have <code>n</code> processors each having <code>4</code> cores and <code>n * 4</code> tasks that need to be executed such that each core should perform only <strong>one</strong> task.</p>
|
||||
|
||||
<p>Given a <strong>0-indexed</strong> integer array <code>processorTime</code> representing the time at which each processor becomes available for the first time and a <strong>0-indexed </strong>integer array <code>tasks</code> representing the time it takes to execute each task, return <em>the <strong>minimum</strong> time when all of the tasks have been executed by the processors.</em></p>
|
||||
|
||||
<p><strong>Note: </strong>Each core executes the task independently of the others.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5]
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
It's optimal to assign the tasks at indexes 4, 5, 6, 7 to the first processor which becomes available at time = 8, and the tasks at indexes 0, 1, 2, 3 to the second processor which becomes available at time = 10.
|
||||
Time taken by the first processor to finish execution of all tasks = max(8 + 8, 8 + 7, 8 + 4, 8 + 5) = 16.
|
||||
Time taken by the second processor to finish execution of all tasks = max(10 + 2, 10 + 2, 10 + 3, 10 + 1) = 13.
|
||||
Hence, it can be shown that the minimum time taken to execute all the tasks is 16.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3]
|
||||
<strong>Output:</strong> 23
|
||||
<strong>Explanation:</strong>
|
||||
It's optimal to assign the tasks at indexes 1, 4, 5, 6 to the first processor which becomes available at time = 10, and the tasks at indexes 0, 2, 3, 7 to the second processor which becomes available at time = 20.
|
||||
Time taken by the first processor to finish execution of all tasks = max(10 + 3, 10 + 5, 10 + 8, 10 + 4) = 18.
|
||||
Time taken by the second processor to finish execution of all tasks = max(20 + 2, 20 + 1, 20 + 2, 20 + 3) = 23.
|
||||
Hence, it can be shown that the minimum time taken to execute all the tasks is 23.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == processorTime.length <= 25000</code></li>
|
||||
<li><code>1 <= tasks.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= processorTime[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= tasks[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>tasks.length == 4 * n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,60 @@
|
||||
<p>You are given a binary string <code>s</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>A substring of <code>s</code> is <strong>beautiful</strong> if the number of <code>1</code>'s in it is exactly <code>k</code>.</p>
|
||||
|
||||
<p>Let <code>len</code> be the length of the <strong>shortest</strong> beautiful substring.</p>
|
||||
|
||||
<p>Return <em>the lexicographically <strong>smallest</strong> beautiful substring of string </em><code>s</code><em> with length equal to </em><code>len</code>. If <code>s</code> doesn't contain a beautiful substring, return <em>an <strong>empty</strong> string</em>.</p>
|
||||
|
||||
<p>A string <code>a</code> is lexicographically <strong>larger</strong> than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, <code>a</code> has a character strictly larger than the corresponding character in <code>b</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>"abcd"</code> is lexicographically larger than <code>"abcc"</code> because the first position they differ is at the fourth character, and <code>d</code> is greater than <code>c</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "100011001", k = 3
|
||||
<strong>Output:</strong> "11001"
|
||||
<strong>Explanation:</strong> There are 7 beautiful substrings in this example:
|
||||
1. The substring "<u>100011</u>001".
|
||||
2. The substring "<u>1000110</u>01".
|
||||
3. The substring "<u>10001100</u>1".
|
||||
4. The substring "1<u>00011001</u>".
|
||||
5. The substring "10<u>0011001</u>".
|
||||
6. The substring "100<u>011001</u>".
|
||||
7. The substring "1000<u>11001</u>".
|
||||
The length of the shortest beautiful substring is 5.
|
||||
The lexicographically smallest beautiful substring with length 5 is the substring "11001".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "1011", k = 2
|
||||
<strong>Output:</strong> "11"
|
||||
<strong>Explanation:</strong> There are 3 beautiful substrings in this example:
|
||||
1. The substring "<u>101</u>1".
|
||||
2. The substring "1<u>011</u>".
|
||||
3. The substring "10<u>11</u>".
|
||||
The length of the shortest beautiful substring is 2.
|
||||
The lexicographically smallest beautiful substring with length 2 is the substring "11".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "000", k = 1
|
||||
<strong>Output:</strong> ""
|
||||
<strong>Explanation:</strong> There are no beautiful substrings in this example.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>1 <= k <= s.length</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user