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,49 @@
|
||||
<p>You are given a positive integer <code>n</code>. Each digit of <code>n</code> has a sign according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>most significant digit</strong> is assigned a <strong>positive</strong> sign.</li>
|
||||
<li>Each other digit has an opposite sign to its adjacent digits.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the sum of all digits with their corresponding sign</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 521
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> (+5) + (-2) + (+1) = 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 111
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> (+1) + (-1) + (+1) = 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 886996
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> (+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
|
||||
}
|
||||
.spoiler {overflow:hidden;}
|
||||
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
|
||||
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
|
||||
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
|
||||
</style>
|
@@ -0,0 +1,38 @@
|
||||
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and an integer <code>k</code>. You can perform the following operation on <code>nums1</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose two indexes <code>i</code> and <code>j</code> and increment <code>nums1[i]</code> by <code>k</code> and decrement <code>nums1[j]</code> by <code>k</code>. In other words, <code>nums1[i] = nums1[i] + k</code> and <code>nums1[j] = nums1[j] - k</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><code>nums1</code> is said to be <strong>equal</strong> to <code>nums2</code> if for all indices <code>i</code> such that <code>0 <= i < n</code>, <code>nums1[i] == nums2[i]</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations required to make </em><code>nums1</code><em> equal to </em><code>nums2</code>. If it is impossible to make them equal, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [4,3,1,4], nums2 = [1,3,7,1], k = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In 2 operations, we can transform nums1 to nums2.
|
||||
1<sup>st</sup> operation: i = 2, j = 0. After applying the operation, nums1 = [1,3,4,4].
|
||||
2<sup>nd</sup> operation: i = 2, j = 3. After applying the operation, nums1 = [1,3,7,1].
|
||||
One can prove that it is impossible to make arrays equal in fewer operations.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,8,5,2], nums2 = [2,4,1,6], k = 1
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be proved that it is impossible to make the two arrays equal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums1[i], nums2[j] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>There exists an infinitely large grid. You are currently at point <code>(1, 1)</code>, and you need to reach the point <code>(targetX, targetY)</code> using a finite number of steps.</p>
|
||||
|
||||
<p>In one <strong>step</strong>, you can move from point <code>(x, y)</code> to any one of the following points:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>(x, y - x)</code></li>
|
||||
<li><code>(x - y, y)</code></li>
|
||||
<li><code>(2 * x, y)</code></li>
|
||||
<li><code>(x, 2 * y)</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Given two integers <code>targetX</code> and <code>targetY</code> representing the X-coordinate and Y-coordinate of your final position, return <code>true</code> <em>if you can reach the point from</em> <code>(1, 1)</code> <em>using some number of steps, and </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> targetX = 6, targetY = 9
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is impossible to reach (6,9) from (1,1) using any sequence of moves, so false is returned.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> targetX = 4, targetY = 7
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> You can follow the path (1,1) -> (1,2) -> (1,4) -> (1,8) -> (1,7) -> (2,7) -> (4,7).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= targetX, targetY <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given a positive integer <code>n</code>, indicating that we initially have an <code>n x n</code> <strong>0-indexed</strong> integer matrix <code>mat</code> filled with zeroes.</p>
|
||||
|
||||
<p>You are also given a 2D integer array <code>query</code>. For each <code>query[i] = [row1<sub>i</sub>, col1<sub>i</sub>, row2<sub>i</sub>, col2<sub>i</sub>]</code>, you should do the following operation:</p>
|
||||
|
||||
<ul>
|
||||
<li>Add <code>1</code> to <strong>every element</strong> in the submatrix with the <strong>top left</strong> corner <code>(row1<sub>i</sub>, col1<sub>i</sub>)</code> and the <strong>bottom right</strong> corner <code>(row2<sub>i</sub>, col2<sub>i</sub>)</code>. That is, add <code>1</code> to <code>mat[x][y]</code> for for all <code>row1<sub>i</sub> <= x <= row2<sub>i</sub></code> and <code>col1<sub>i</sub> <= y <= col2<sub>i</sub></code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the matrix</em> <code>mat</code><em> after performing every query.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/11/24/p2example11.png" style="width: 531px; height: 121px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, queries = [[1,1,2,2],[0,0,1,1]]
|
||||
<strong>Output:</strong> [[1,1,0],[1,2,1],[0,1,1]]
|
||||
<strong>Explanation:</strong> The diagram above shows the initial matrix, the matrix after the first query, and the matrix after the second query.
|
||||
- In the first query, we add 1 to every element in the submatrix with the top left corner (1, 1) and bottom right corner (2, 2).
|
||||
- In the second query, we add 1 to every element in the submatrix with the top left corner (0, 0) and bottom right corner (1, 1).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/11/24/p2example22.png" style="width: 261px; height: 82px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, queries = [[0,0,1,1]]
|
||||
<strong>Output:</strong> [[1,1],[1,1]]
|
||||
<strong>Explanation:</strong> The diagram above shows the initial matrix and the matrix after the first query.
|
||||
- In the first query we add 1 to every element in the matrix.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 500</code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= row1<sub>i</sub> <= row2<sub>i</sub> < n</code></li>
|
||||
<li><code>0 <= col1<sub>i</sub> <= col2<sub>i</sub> < n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given two <strong>0-indexed binary</strong> strings <code>s</code> and <code>target</code> of the same length <code>n</code>. You can do the following operation on <code>s</code> <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose two <strong>different</strong> indices <code>i</code> and <code>j</code> where <code>0 <= i, j < n</code>.</li>
|
||||
<li>Simultaneously, replace <code>s[i]</code> with (<code>s[i]</code> <strong>OR</strong> <code>s[j]</code>) and <code>s[j]</code> with (<code>s[i]</code> <strong>XOR</strong> <code>s[j]</code>).</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, if <code>s = "0110"</code>, you can choose <code>i = 0</code> and <code>j = 2</code>, then simultaneously replace <code>s[0]</code> with (<code>s[0]</code> <strong>OR</strong> <code>s[2]</code> = <code>0</code> <strong>OR</strong> <code>1</code> = <code>1</code>), and <code>s[2]</code> with (<code>s[0]</code> <strong>XOR</strong> <code>s[2]</code> = <code>0</code> <strong>XOR</strong> <code>1</code> = <code>1</code>), so we will have <code>s = "1110"</code>.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if you can make the string </em><code>s</code><em> equal to </em><code>target</code><em>, or </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "1010", target = "0110"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We can do the following operations:
|
||||
- Choose i = 2 and j = 0. We have now s = "<strong><u>0</u></strong>0<strong><u>1</u></strong>0".
|
||||
- Choose i = 2 and j = 1. We have now s = "0<strong><u>11</u></strong>0".
|
||||
Since we can make s equal to target, we return true.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "11", target = "00"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is not possible to make s equal to target with any number of operations.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == s.length == target.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> and <code>target</code> consist of only the digits <code>0</code> and <code>1</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,70 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>Split the array into some number of non-empty subarrays. The <strong>cost</strong> of a split is the sum of the <strong>importance value</strong> of each subarray in the split.</p>
|
||||
|
||||
<p>Let <code>trimmed(subarray)</code> be the version of the subarray where all numbers which appear only once are removed.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>trimmed([3,1,2,4,3,4]) = [3,4,3,4].</code></li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>importance value</strong> of a subarray is <code>k + trimmed(subarray).length</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if a subarray is <code>[1,2,3,3,3,4,4]</code>, then <font face="monospace">trimmed(</font><code>[1,2,3,3,3,4,4]) = [3,3,3,4,4].</code>The importance value of this subarray will be <code>k + 5</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum possible cost of a split of </em><code>nums</code>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1,2,1,3,3], k = 2
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> We split nums to have two subarrays: [1,2], [1,2,1,3,3].
|
||||
The importance value of [1,2] is 2 + (0) = 2.
|
||||
The importance value of [1,2,1,3,3] is 2 + (2 + 2) = 6.
|
||||
The cost of the split is 2 + 6 = 8. It can be shown that this is the minimum possible cost among all the possible splits.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1,2,1], k = 2
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> We split nums to have two subarrays: [1,2], [1,2,1].
|
||||
The importance value of [1,2] is 2 + (0) = 2.
|
||||
The importance value of [1,2,1] is 2 + (2) = 4.
|
||||
The cost of the split is 2 + 4 = 6. It can be shown that this is the minimum possible cost among all the possible splits.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1,2,1], k = 5
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> We split nums to have one subarray: [1,2,1,2,1].
|
||||
The importance value of [1,2,1,2,1] is 5 + (3 + 2) = 10.
|
||||
The cost of the split is 10. It can be shown that this is the minimum possible cost among all the possible splits.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>0 <= nums[i] < nums.length</code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
|
||||
}
|
||||
.spoiler {overflow:hidden;}
|
||||
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
|
||||
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
|
||||
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
|
||||
</style>
|
@@ -0,0 +1,41 @@
|
||||
<p>You are given a positive integer array <code>nums</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>element sum</strong> is the sum of all the elements in <code>nums</code>.</li>
|
||||
<li>The <strong>digit sum</strong> is the sum of all the digits (not necessarily distinct) that appear in <code>nums</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>absolute</strong> difference between the <strong>element sum</strong> and <strong>digit sum</strong> of </em><code>nums</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the absolute difference between two integers <code>x</code> and <code>y</code> is defined as <code>|x - y|</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,15,6,3]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong>
|
||||
The element sum of nums is 1 + 15 + 6 + 3 = 25.
|
||||
The digit sum of nums is 1 + 1 + 5 + 6 + 3 = 16.
|
||||
The absolute difference between the element sum and digit sum is |25 - 16| = 9.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
The element sum of nums is 1 + 2 + 3 + 4 = 10.
|
||||
The digit sum of nums is 1 + 2 + 3 + 4 = 10.
|
||||
The absolute difference between the element sum and digit sum is |10 - 10| = 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2000</code></li>
|
||||
<li><code>1 <= nums[i] <= 2000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>There exists an undirected and initially unrooted tree with <code>n</code> nodes indexed from <code>0</code> to <code>n - 1</code>. You are given the integer <code>n</code> and 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>Each node has an associated price. You are given an integer array <code>price</code>, where <code>price[i]</code> is the price of the <code>i<sup>th</sup></code> node.</p>
|
||||
|
||||
<p>The <strong>price sum</strong> of a given path is the sum of the prices of all nodes lying on that path.</p>
|
||||
|
||||
<p>The tree can be rooted at any node <code>root</code> of your choice. The incurred <strong>cost</strong> after choosing <code>root</code> is the difference between the maximum and minimum <strong>price sum</strong> amongst all paths starting at <code>root</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible <strong>cost</strong></em> <em>amongst all possible root choices</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/01/example14.png" style="width: 556px; height: 231px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6, edges = [[0,1],[1,2],[1,3],[3,4],[3,5]], price = [9,8,7,6,10,5]
|
||||
<strong>Output:</strong> 24
|
||||
<strong>Explanation:</strong> The diagram above denotes the tree after rooting it at node 2. The first part (colored in red) shows the path with the maximum price sum. The second part (colored in blue) shows the path with the minimum price sum.
|
||||
- The first path contains nodes [2,1,3,4]: the prices are [7,8,6,10], and the sum of the prices is 31.
|
||||
- The second path contains the node [2] with the price [7].
|
||||
The difference between the maximum and minimum price sum is 24. It can be proved that 24 is the maximum cost.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/11/24/p1_example2.png" style="width: 352px; height: 184px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, edges = [[0,1],[1,2]], price = [1,1,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The diagram above denotes the tree after rooting it at node 0. The first part (colored in red) shows the path with the maximum price sum. The second part (colored in blue) shows the path with the minimum price sum.
|
||||
- The first path contains nodes [0,1,2]: the prices are [1,1,1], and the sum of the prices is 3.
|
||||
- The second path contains node [0] with a price [1].
|
||||
The difference between the maximum and minimum price sum is 2. It can be proved that 2 is the maximum cost.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
<li><code>price.length == n</code></li>
|
||||
<li><code>1 <= price[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
|
||||
|
||||
<p>For chosen indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, ..., <code>i<sub>k - 1</sub></code>, your <strong>score</strong> is defined as:</p>
|
||||
|
||||
<ul>
|
||||
<li>The sum of the selected elements from <code>nums1</code> multiplied with the <strong>minimum</strong> of the selected elements from <code>nums2</code>.</li>
|
||||
<li>It can defined simply as: <code>(nums1[i<sub>0</sub>] + nums1[i<sub>1</sub>] +...+ nums1[i<sub>k - 1</sub>]) * min(nums2[i<sub>0</sub>] , nums2[i<sub>1</sub>], ... ,nums2[i<sub>k - 1</sub>])</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible score.</em></p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of indices of an array is a set that can be derived from the set <code>{0, 1, ..., n-1}</code> by deleting some or no elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation:</strong>
|
||||
The four possible subsequence scores are:
|
||||
- We choose the indices 0, 1, and 2 with score = (1+3+3) * min(2,1,3) = 7.
|
||||
- We choose the indices 0, 1, and 3 with score = (1+3+2) * min(2,1,4) = 6.
|
||||
- We choose the indices 0, 2, and 3 with score = (1+3+2) * min(2,3,4) = 12.
|
||||
- We choose the indices 1, 2, and 3 with score = (3+3+2) * min(1,3,4) = 8.
|
||||
Therefore, we return the max score, which is 12.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1
|
||||
<strong>Output:</strong> 30
|
||||
<strong>Explanation:</strong>
|
||||
Choosing index 2 is optimal: nums1[2] * nums2[2] = 3 * 10 = 30 is the maximum possible score.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums1[i], nums2[j] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
<p>Given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in non-decreasing order, return <em>the <strong>minimum integer common</strong> to both arrays</em>. If there is no common integer amongst <code>nums1</code> and <code>nums2</code>, return <code>-1</code>.</p>
|
||||
|
||||
<p>Note that an integer is said to be <strong>common</strong> to <code>nums1</code> and <code>nums2</code> if both arrays have <strong>at least one</strong> occurrence of that integer.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2,3], nums2 = [2,4]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The smallest element common to both arrays is 2, so we return 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2,3,6], nums2 = [2,3,4,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums1[i], nums2[j] <= 10<sup>9</sup></code></li>
|
||||
<li>Both <code>nums1</code> and <code>nums2</code> are sorted in <strong>non-decreasing</strong> order.</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>There is a class with <code>m</code> students and <code>n</code> exams. You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>score</code>, where each row represents one student and <code>score[i][j]</code> denotes the score the <code>i<sup>th</sup></code> student got in the <code>j<sup>th</sup></code> exam. The matrix <code>score</code> contains <strong>distinct</strong> integers only.</p>
|
||||
|
||||
<p>You are also given an integer <code>k</code>. Sort the students (i.e., the rows of the matrix) by their scores in the <code>k<sup>th</sup></code> (<strong>0-indexed</strong>) exam from the highest to the lowest.</p>
|
||||
|
||||
<p>Return <em>the matrix after sorting it.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/11/30/example1.png" style="width: 600px; height: 136px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> score = [[10,6,9,1],[7,5,11,2],[4,8,3,15]], k = 2
|
||||
<strong>Output:</strong> [[7,5,11,2],[10,6,9,1],[4,8,3,15]]
|
||||
<strong>Explanation:</strong> In the above diagram, S denotes the student, while E denotes the exam.
|
||||
- The student with index 1 scored 11 in exam 2, which is the highest score, so they got first place.
|
||||
- The student with index 0 scored 9 in exam 2, which is the second highest score, so they got second place.
|
||||
- The student with index 2 scored 3 in exam 2, which is the lowest score, so they got third place.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/11/30/example2.png" style="width: 486px; height: 121px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> score = [[3,4],[5,6]], k = 0
|
||||
<strong>Output:</strong> [[5,6],[3,4]]
|
||||
<strong>Explanation:</strong> In the above diagram, S denotes the student, while E denotes the exam.
|
||||
- The student with index 1 scored 5 in exam 0, which is the highest score, so they got first place.
|
||||
- The student with index 0 scored 3 in exam 0, which is the lowest score, so they got second place.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == score.length</code></li>
|
||||
<li><code>n == score[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 250</code></li>
|
||||
<li><code>1 <= score[i][j] <= 10<sup>5</sup></code></li>
|
||||
<li><code>score</code> consists of <strong>distinct</strong> integers.</li>
|
||||
<li><code>0 <= k < n</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</strong> subarrays of</em> <code>nums</code>.</p>
|
||||
|
||||
<p>A subarray <code>arr</code> is <strong>good</strong> if it there are <strong>at least </strong><code>k</code> pairs of indices <code>(i, j)</code> such that <code>i < j</code> and <code>arr[i] == arr[j]</code>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1,1,1], k = 10
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The only good subarray is the array nums itself.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,1,4,3,2,2,4], k = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> There are 4 different good subarrays:
|
||||
- [3,1,4,3,2,2] that has 2 pairs.
|
||||
- [3,1,4,3,2,2,4] that has 3 pairs.
|
||||
- [1,4,3,2,2,4] that has 2 pairs.
|
||||
- [4,3,2,2,4] that has 2 pairs.
|
||||
</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], k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user