mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
update
This commit is contained in:
parent
f8425bb3ee
commit
9d367dc01f
@ -1,6 +1,6 @@
|
|||||||
# 力扣题库(完整版)
|
# 力扣题库(完整版)
|
||||||
|
|
||||||
> 最后更新日期: **2023.01.14**
|
> 最后更新日期: **2023.01.23**
|
||||||
>
|
>
|
||||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||||
|
|
||||||
|
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
165
leetcode-cn/originData/alternating-digit-sum.json
Normal file
165
leetcode-cn/originData/alternating-digit-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
166
leetcode-cn/originData/check-if-point-is-reachable.json
Normal file
166
leetcode-cn/originData/check-if-point-is-reachable.json
Normal file
File diff suppressed because one or more lines are too long
185
leetcode-cn/originData/count-the-number-of-good-subarrays.json
Normal file
185
leetcode-cn/originData/count-the-number-of-good-subarrays.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
184
leetcode-cn/originData/increment-submatrices-by-one.json
Normal file
184
leetcode-cn/originData/increment-submatrices-by-one.json
Normal file
File diff suppressed because one or more lines are too long
165
leetcode-cn/originData/maximum-subsequence-score.json
Normal file
165
leetcode-cn/originData/maximum-subsequence-score.json
Normal file
File diff suppressed because one or more lines are too long
164
leetcode-cn/originData/minimum-common-value.json
Normal file
164
leetcode-cn/originData/minimum-common-value.json
Normal file
File diff suppressed because one or more lines are too long
166
leetcode-cn/originData/minimum-cost-to-split-an-array.json
Normal file
166
leetcode-cn/originData/minimum-cost-to-split-an-array.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
164
leetcode-cn/originData/sort-the-students-by-their-kth-score.json
Normal file
164
leetcode-cn/originData/sort-the-students-by-their-kth-score.json
Normal file
File diff suppressed because one or more lines are too long
@ -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>
|
@ -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>
|
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
162
leetcode/originData/alternating-digit-sum.json
Normal file
162
leetcode/originData/alternating-digit-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
163
leetcode/originData/check-if-point-is-reachable.json
Normal file
163
leetcode/originData/check-if-point-is-reachable.json
Normal file
File diff suppressed because one or more lines are too long
182
leetcode/originData/count-the-number-of-good-subarrays.json
Normal file
182
leetcode/originData/count-the-number-of-good-subarrays.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
181
leetcode/originData/increment-submatrices-by-one.json
Normal file
181
leetcode/originData/increment-submatrices-by-one.json
Normal file
File diff suppressed because one or more lines are too long
162
leetcode/originData/maximum-subsequence-score.json
Normal file
162
leetcode/originData/maximum-subsequence-score.json
Normal file
File diff suppressed because one or more lines are too long
161
leetcode/originData/minimum-common-value.json
Normal file
161
leetcode/originData/minimum-common-value.json
Normal file
File diff suppressed because one or more lines are too long
163
leetcode/originData/minimum-cost-to-split-an-array.json
Normal file
163
leetcode/originData/minimum-cost-to-split-an-array.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
161
leetcode/originData/sort-the-students-by-their-kth-score.json
Normal file
161
leetcode/originData/sort-the-students-by-their-kth-score.json
Normal file
File diff suppressed because one or more lines are too long
49
leetcode/problem/alternating-digit-sum.html
Normal file
49
leetcode/problem/alternating-digit-sum.html
Normal file
@ -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,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>
|
36
leetcode/problem/check-if-point-is-reachable.html
Normal file
36
leetcode/problem/check-if-point-is-reachable.html
Normal file
@ -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>
|
34
leetcode/problem/count-the-number-of-good-subarrays.html
Normal file
34
leetcode/problem/count-the-number-of-good-subarrays.html
Normal file
@ -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>
|
@ -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>
|
39
leetcode/problem/increment-submatrices-by-one.html
Normal file
39
leetcode/problem/increment-submatrices-by-one.html
Normal file
@ -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>
|
46
leetcode/problem/maximum-subsequence-score.html
Normal file
46
leetcode/problem/maximum-subsequence-score.html
Normal file
@ -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>
|
29
leetcode/problem/minimum-common-value.html
Normal file
29
leetcode/problem/minimum-common-value.html
Normal file
@ -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>
|
70
leetcode/problem/minimum-cost-to-split-an-array.html
Normal file
70
leetcode/problem/minimum-cost-to-split-an-array.html
Normal file
@ -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,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>
|
39
leetcode/problem/sort-the-students-by-their-kth-score.html
Normal file
39
leetcode/problem/sort-the-students-by-their-kth-score.html
Normal file
@ -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>
|
Loading…
Reference in New Issue
Block a user