mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 07:21:40 +08:00
update
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<p>给你一个正整数 <code>n</code> 。<code>n</code> 中的每一位数字都会按下述规则分配一个符号:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>最高有效位</strong> 上的数字分配到 <strong>正</strong> 号。</li>
|
||||
<li>剩余每位上数字的符号都与其相邻数字相反。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回所有数字及其对应符号的和。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 521
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>(+5) + (-2) + (+1) = 4</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 111
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>(+1) + (-1) + (+1) = 1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 886996
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>(+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,38 @@
|
||||
<p>给你两个整数数组 <code>nums1</code> 和 <code>nums2</code> ,两个数组长度都是 <code>n</code> ,再给你一个整数 <code>k</code> 。你可以对数组 <code>nums1</code> 进行以下操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择两个下标 <code>i</code> 和 <code>j</code> ,将 <code>nums1[i]</code> 增加 <code>k</code> ,将 <code>nums1[j]</code> 减少 <code>k</code> 。换言之,<code>nums1[i] = nums1[i] + k</code> 且 <code>nums1[j] = nums1[j] - k</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果对于所有满足 <code>0 <= i < n</code> 都有 <code>num1[i] == nums2[i]</code> ,那么我们称 <code>nums1</code> <strong>等于</strong> <code>nums2</code> 。</p>
|
||||
|
||||
<p>请你返回使<em> </em><code>nums1</code><em> </em>等于<em> </em><code>nums2</code> 的 <strong>最少</strong> 操作数。如果没办法让它们相等,请你返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums1 = [4,3,1,4], nums2 = [1,3,7,1], k = 3
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>我们可以通过 2 个操作将 nums1 变成 nums2 。
|
||||
第 1 个操作:i = 2 ,j = 0 。操作后得到 nums1 = [1,3,4,4] 。
|
||||
第 2 个操作:i = 2 ,j = 3 。操作后得到 nums1 = [1,3,7,1] 。
|
||||
无法用更少操作使两个数组相等。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums1 = [3,8,5,2], nums2 = [2,4,1,6], k = 1
|
||||
<b>输出:</b>-1
|
||||
<b>解释:</b>无法使两个数组相等。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</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>给你一个无穷大的网格图。一开始你在 <code>(1, 1)</code> ,你需要通过有限步移动到达点 <code>(targetX, targetY)</code> 。</p>
|
||||
|
||||
<p><b>每一步</b> ,你可以从点 <code>(x, y)</code> 移动到以下点之一:</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>给你两个整数 <code>targetX</code> 和 <code>targetY</code> ,分别表示你最后需要到达点的 X 和 Y 坐标。如果你可以从 <code>(1, 1)</code> 出发到达这个点,请你返回<code>true</code> ,否则返回<em> </em><code>false</code><em> </em>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>targetX = 6, targetY = 9
|
||||
<b>输出:</b>false
|
||||
<b>解释:</b>没法从 (1,1) 出发到达 (6,9) ,所以返回 false 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>targetX = 4, targetY = 7
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>你可以按照以下路径到达:(1,1) -> (1,2) -> (1,4) -> (1,8) -> (1,7) -> (2,7) -> (4,7) 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= targetX, targetY <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>给你一个正整数 <code>n</code> ,表示最初有一个 <code>n x n</code> 、下标从 <strong>0</strong> 开始的整数矩阵 <code>mat</code> ,矩阵中填满了 0 。</p>
|
||||
|
||||
<p>另给你一个二维整数数组 <code>query</code> 。针对每个查询 <code>query[i] = [row1<sub>i</sub>, col1<sub>i</sub>, row2<sub>i</sub>, col2<sub>i</sub>]</code> ,请你执行下述操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>找出 <strong>左上角</strong> 为 <code>(row1<sub>i</sub>, col1<sub>i</sub>)</code> 且 <strong>右下角</strong> 为 <code>(row2<sub>i</sub>, col2<sub>i</sub>)</code> 的子矩阵,将子矩阵中的 <strong>每个元素</strong> 加 <code>1</code> 。也就是给所有满足 <code>row1<sub>i</sub> <= x <= row2<sub>i</sub></code> 和 <code>col1<sub>i</sub> <= y <= col2<sub>i</sub></code> 的 <code>mat[x][y]</code> 加 <code>1</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回执行完所有操作后得到的矩阵 <code>mat</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/11/24/p2example11.png" style="width: 531px; height: 121px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3, queries = [[1,1,2,2],[0,0,1,1]]
|
||||
<strong>输出:</strong>[[1,1,0],[1,2,1],[0,1,1]]
|
||||
<strong>解释:</strong>上图所展示的分别是:初始矩阵、执行完第一个操作后的矩阵、执行完第二个操作后的矩阵。
|
||||
- 第一个操作:将左上角为 (1, 1) 且右下角为 (2, 2) 的子矩阵中的每个元素加 1 。
|
||||
- 第二个操作:将左上角为 (0, 0) 且右下角为 (1, 1) 的子矩阵中的每个元素加 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/11/24/p2example22.png" style="width: 261px; height: 82px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 2, queries = [[0,0,1,1]]
|
||||
<strong>输出:</strong>[[1,1],[1,1]]
|
||||
<strong>解释:</strong>上图所展示的分别是:初始矩阵、执行完第一个操作后的矩阵。
|
||||
- 第一个操作:将矩阵中的每个元素加 1 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</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>给你两个下标从 <strong>0</strong> 开始的 <strong>二元</strong> 字符串 <code>s</code> 和 <code>target</code> ,两个字符串的长度均为 <code>n</code> 。你可以对 <code>s</code> 执行下述操作 <strong>任意</strong> 次:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择两个 <strong>不同</strong> 的下标 <code>i</code> 和 <code>j</code> ,其中 <code>0 <= i, j < n</code> 。</li>
|
||||
<li>同时,将 <code>s[i]</code> 替换为 (<code>s[i]</code> <strong>OR</strong> <code>s[j]</code>) ,<code>s[j]</code> 替换为 (<code>s[i]</code> <strong>XOR</strong> <code>s[j]</code>) 。</li>
|
||||
</ul>
|
||||
|
||||
<p>例如,如果 <code>s = "0110"</code> ,你可以选择 <code>i = 0</code> 和 <code>j = 2</code>,然后同时将 <code>s[0]</code> 替换为 (<code>s[0]</code> <strong>OR</strong> <code>s[2]</code> = <code>0</code> <strong>OR</strong> <code>1</code> = <code>1</code>),并将 <code>s[2]</code> 替换为 (<code>s[0]</code> <strong>XOR</strong> <code>s[2]</code> = <code>0</code> <strong>XOR</strong> <code>1</code> = <code>1</code>),最终得到 <code>s = "1110"</code> 。</p>
|
||||
|
||||
<p>如果可以使 <code>s</code> 等于 <code>target</code> ,返回 <code>true</code> ,否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "1010", target = "0110"
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>可以执行下述操作:
|
||||
- 选择 i = 2 和 j = 0 ,得到 s = "<em><strong>0</strong></em>0<em><strong>1</strong></em>0".
|
||||
- 选择 i = 2 和 j = 1 ,得到 s = "0<em><strong>11</strong></em>0".
|
||||
可以使 s 等于 target ,返回 true 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "11", target = "00"
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>执行任意次操作都无法使 s 等于 target 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</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> 和 <code>target</code> 仅由数字 <code>0</code> 和 <code>1</code> 组成</li>
|
||||
</ul>
|
@@ -0,0 +1,65 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>将数组拆分成一些非空子数组。拆分的 <strong>代价</strong> 是每个子数组中的 <strong>重要性</strong> 之和。</p>
|
||||
|
||||
<p>令 <code>trimmed(subarray)</code> 作为子数组的一个特征,其中所有仅出现一次的数字将会被移除。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>trimmed([3,1,2,4,3,4]) = [3,4,3,4]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>子数组的 <strong>重要性</strong> 定义为 <code>k + trimmed(subarray).length</code> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,如果一个子数组是 <code>[1,2,3,3,3,4,4]</code> ,<code>trimmed([1,2,3,3,3,4,4]) = [3,3,3,4,4]</code> 。这个子数组的重要性就是 <code>k + 5</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>找出并返回拆分 <code>nums</code> 的所有可行方案中的最小代价。</p>
|
||||
|
||||
<p><strong>子数组</strong> 是数组的一个连续 <strong>非空</strong> 元素序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,1,2,1,3,3], k = 2
|
||||
<strong>输出:</strong>8
|
||||
<strong>解释:</strong>将 nums 拆分成两个子数组:[1,2], [1,2,1,3,3]
|
||||
[1,2] 的重要性是 2 + (0) = 2 。
|
||||
[1,2,1,3,3] 的重要性是 2 + (2 + 2) = 6 。
|
||||
拆分的代价是 2 + 6 = 8 ,可以证明这是所有可行的拆分方案中的最小代价。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,1,2,1], k = 2
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>将 nums 拆分成两个子数组:[1,2], [1,2,1] 。
|
||||
[1,2] 的重要性是 2 + (0) = 2 。
|
||||
[1,2,1] 的重要性是 2 + (2) = 4 。
|
||||
拆分的代价是 2 + 4 = 6 ,可以证明这是所有可行的拆分方案中的最小代价。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,1,2,1], k = 5
|
||||
<strong>输出:</strong>10
|
||||
<strong>解释:</strong>将 nums 拆分成一个子数组:[1,2,1,2,1].
|
||||
[1,2,1,2,1] 的重要性是 5 + (3 + 2) = 10 。
|
||||
拆分的代价是 10 ,可以证明这是所有可行的拆分方案中的最小代价。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</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>
|
@@ -0,0 +1,43 @@
|
||||
<p>给你一个正整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>元素和</strong> 是 <code>nums</code> 中的所有元素相加求和。</li>
|
||||
<li><strong>数字和</strong> 是 <code>nums</code> 中每一个元素的每一数位(重复数位需多次求和)相加求和。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回 <strong>元素和</strong> 与 <strong>数字和</strong> 的绝对差。</p>
|
||||
|
||||
<p><strong>注意:</strong>两个整数 <code>x</code> 和 <code>y</code> 的绝对差定义为 <code>|x - y|</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,15,6,3]
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>
|
||||
nums 的元素和是 1 + 15 + 6 + 3 = 25 。
|
||||
nums 的数字和是 1 + 1 + 5 + 6 + 3 = 16 。
|
||||
元素和与数字和的绝对差是 |25 - 16| = 9 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,4]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>
|
||||
nums 的元素和是 1 + 2 + 3 + 4 = 10 。
|
||||
nums 的数字和是 1 + 2 + 3 + 4 = 10 。
|
||||
元素和与数字和的绝对差是 |10 - 10| = 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2000</code></li>
|
||||
<li><code>1 <= nums[i] <= 2000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>给你一个 <code>n</code> 个节点的无向无根图,节点编号为 <code>0</code> 到 <code>n - 1</code> 。给你一个整数 <code>n</code> 和一个长度为 <code>n - 1</code> 的二维整数数组 <code>edges</code> ,其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示树中节点 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间有一条边。</p>
|
||||
|
||||
<p>每个节点都有一个价值。给你一个整数数组 <code>price</code> ,其中 <code>price[i]</code> 是第 <code>i</code> 个节点的价值。</p>
|
||||
|
||||
<p>一条路径的 <strong>价值和</strong> 是这条路径上所有节点的价值之和。</p>
|
||||
|
||||
<p>你可以选择树中任意一个节点作为根节点 <code>root</code> 。选择 <code>root</code> 为根的 <strong>开销</strong> 是以 <code>root</code> 为起点的所有路径中,<strong>价值和</strong> 最大的一条路径与最小的一条路径的差值。</p>
|
||||
|
||||
<p>请你返回所有节点作为根节点的选择中,<strong>最大</strong> 的 <strong>开销</strong> 为多少。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/01/example14.png" style="width: 556px; height: 231px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 6, edges = [[0,1],[1,2],[1,3],[3,4],[3,5]], price = [9,8,7,6,10,5]
|
||||
<b>输出:</b>24
|
||||
<b>解释:</b>上图展示了以节点 2 为根的树。左图(红色的节点)是最大价值和路径,右图(蓝色的节点)是最小价值和路径。
|
||||
- 第一条路径节点为 [2,1,3,4]:价值为 [7,8,6,10] ,价值和为 31 。
|
||||
- 第二条路径节点为 [2] ,价值为 [7] 。
|
||||
最大路径和与最小路径和的差值为 24 。24 是所有方案中的最大开销。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/11/24/p1_example2.png" style="width: 352px; height: 184px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 3, edges = [[0,1],[1,2]], price = [1,1,1]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>上图展示了以节点 0 为根的树。左图(红色的节点)是最大价值和路径,右图(蓝色的节点)是最小价值和路径。
|
||||
- 第一条路径包含节点 [0,1,2]:价值为 [1,1,1] ,价值和为 3 。
|
||||
- 第二条路径节点为 [0] ,价值为 [1] 。
|
||||
最大路径和与最小路径和的差值为 2 。2 是所有方案中的最大开销。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</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> 表示一棵符合题面要求的树。</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>给你两个下标从 <strong>0</strong> 开始的整数数组 <code>nums1</code> 和 <code>nums2</code> ,两者长度都是 <code>n</code> ,再给你一个正整数 <code>k</code> 。你必须从 <code>nums1</code> 中选一个长度为 <code>k</code> 的 <strong>子序列</strong> 对应的下标。</p>
|
||||
|
||||
<p>对于选择的下标 <code>i<sub>0</sub></code> ,<code>i<sub>1</sub></code> ,..., <code>i<sub>k - 1</sub></code> ,你的 <strong>分数</strong> 定义如下:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums1</code> 中下标对应元素求和,乘以 <code>nums2</code> 中下标对应元素的 <strong>最小值</strong> 。</li>
|
||||
<li>用公示表示: <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>请你返回 <strong>最大</strong> 可能的分数。</p>
|
||||
|
||||
<p>一个数组的 <strong>子序列</strong> 下标是集合 <code>{0, 1, ..., n-1}</code> 中删除若干元素得到的剩余集合,也可以不删除任何元素。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3
|
||||
<b>输出:</b>12
|
||||
<b>解释:</b>
|
||||
四个可能的子序列分数为:
|
||||
- 选择下标 0 ,1 和 2 ,得到分数 (1+3+3) * min(2,1,3) = 7 。
|
||||
- 选择下标 0 ,1 和 3 ,得到分数 (1+3+2) * min(2,1,4) = 6 。
|
||||
- 选择下标 0 ,2 和 3 ,得到分数 (1+3+2) * min(2,3,4) = 12 。
|
||||
- 选择下标 1 ,2 和 3 ,得到分数 (3+3+2) * min(1,3,4) = 8 。
|
||||
所以最大分数为 12 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1
|
||||
<b>输出:</b>30
|
||||
<b>解释:</b>
|
||||
选择下标 2 最优:nums1[2] * nums2[2] = 3 * 10 = 30 是最大可能分数。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</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>给你两个整数数组 <code>nums1</code> 和 <code>nums2</code> ,它们已经按非降序排序,请你返回两个数组的 <strong>最小公共整数</strong> 。如果两个数组 <code>nums1</code> 和 <code>nums2</code> 没有公共整数,请你返回 <code>-1</code> 。</p>
|
||||
|
||||
<p>如果一个整数在两个数组中都 <strong>至少出现一次</strong> ,那么这个整数是数组 <code>nums1</code> 和 <code>nums2</code> <strong>公共</strong> 的。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums1 = [1,2,3], nums2 = [2,4]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>两个数组的最小公共元素是 2 ,所以我们返回 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums1 = [1,2,3,6], nums2 = [2,3,4,5]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>两个数组中的公共元素是 2 和 3 ,2 是较小值,所以返回 2 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</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><code>nums1</code> 和 <code>nums2</code> 都是 <strong>非降序</strong> 的。</li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>班里有 <code>m</code> 位学生,共计划组织 <code>n</code> 场考试。给你一个下标从 <strong>0</strong> 开始、大小为 <code>m x n</code> 的整数矩阵 <code>score</code> ,其中每一行对应一位学生,而 <code>score[i][j]</code> 表示第 <code>i</code> 位学生在第 <code>j</code> 场考试取得的分数。矩阵 <code>score</code> 包含的整数 <strong>互不相同</strong> 。</p>
|
||||
|
||||
<p>另给你一个整数 <code>k</code> 。请你按第 <code>k</code> 场考试分数从高到低完成对这些学生(矩阵中的行)的排序。</p>
|
||||
|
||||
<p>返回排序后的矩阵。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/11/30/example1.png" style="width: 600px; height: 136px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>score = [[10,6,9,1],[7,5,11,2],[4,8,3,15]], k = 2
|
||||
<strong>输出:</strong>[[7,5,11,2],[10,6,9,1],[4,8,3,15]]
|
||||
<strong>解释:</strong>在上图中,S 表示学生,E 表示考试。
|
||||
- 下标为 1 的学生在第 2 场考试取得的分数为 11 ,这是考试的最高分,所以 TA 需要排在第一。
|
||||
- 下标为 0 的学生在第 2 场考试取得的分数为 9 ,这是考试的第二高分,所以 TA 需要排在第二。
|
||||
- 下标为 2 的学生在第 2 场考试取得的分数为 3 ,这是考试的最低分,所以 TA 需要排在第三。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/11/30/example2.png" style="width: 486px; height: 121px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>score = [[3,4],[5,6]], k = 0
|
||||
<strong>输出:</strong>[[5,6],[3,4]]
|
||||
<strong>解释:</strong>在上图中,S 表示学生,E 表示考试。
|
||||
- 下标为 1 的学生在第 0 场考试取得的分数为 5 ,这是考试的最高分,所以 TA 需要排在第一。
|
||||
- 下标为 0 的学生在第 0 场考试取得的分数为 3 ,这是考试的最低分,所以 TA 需要排在第二。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</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> 由 <strong>不同</strong> 的整数组成</li>
|
||||
<li><code>0 <= k < n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> ,请你返回 <code>nums</code> 中 <strong>好</strong> 子数组的数目。</p>
|
||||
|
||||
<p>一个子数组 <code>arr</code> 如果有 <strong>至少</strong> <code>k</code> 对下标 <code>(i, j)</code> 满足 <code>i < j</code> 且 <code>arr[i] == arr[j]</code> ,那么称它是一个 <strong>好</strong> 子数组。</p>
|
||||
|
||||
<p><strong>子数组</strong> 是原数组中一段连续 <strong>非空</strong> 的元素序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,1,1,1,1], k = 10
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>唯一的好子数组是这个数组本身。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [3,1,4,3,2,2,4], k = 2
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>总共有 4 个不同的好子数组:
|
||||
- [3,1,4,3,2,2] 有 2 对。
|
||||
- [3,1,4,3,2,2,4] 有 3 对。
|
||||
- [1,4,3,2,2,4] 有 2 对。
|
||||
- [4,3,2,2,4] 有 2 对。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</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