mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-13 03:11:42 +08:00
add leetcode problem-cn part4
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<p>表: <code>Logins</code></p>
|
||||
|
||||
<pre>
|
||||
+----------------+----------+
|
||||
| 列名 | 类型 |
|
||||
+----------------+----------+
|
||||
| user_id | int |
|
||||
| time_stamp | datetime |
|
||||
+----------------+----------+
|
||||
(user_id, time_stamp) 是这个表的主键。
|
||||
每一行包含的信息是user_id 这个用户的登录时间。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写一个 SQL 查询,该查询可以获取在 <code>2020</code> 年登录过的所有用户的本年度 <strong>最后一次 </strong>登录时间。结果集 <strong>不</strong> 包含 <code>2020</code> 年没有登录过的用户。</p>
|
||||
|
||||
<p>返回的结果集可以按 <strong>任意顺序 </strong>排列。</p>
|
||||
|
||||
<p>查询结果格式如下例。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Logins 表:
|
||||
+---------+---------------------+
|
||||
| user_id | time_stamp |
|
||||
+---------+---------------------+
|
||||
| 6 | 2020-06-30 15:06:07 |
|
||||
| 6 | 2021-04-21 14:06:06 |
|
||||
| 6 | 2019-03-07 00:18:15 |
|
||||
| 8 | 2020-02-01 05:10:53 |
|
||||
| 8 | 2020-12-30 00:46:50 |
|
||||
| 2 | 2020-01-16 02:49:50 |
|
||||
| 2 | 2019-08-25 07:59:08 |
|
||||
| 14 | 2019-07-14 09:00:00 |
|
||||
| 14 | 2021-01-06 11:59:59 |
|
||||
+---------+---------------------+
|
||||
<strong>输出:</strong>
|
||||
+---------+---------------------+
|
||||
| user_id | last_stamp |
|
||||
+---------+---------------------+
|
||||
| 6 | 2020-06-30 15:06:07 |
|
||||
| 8 | 2020-12-30 00:46:50 |
|
||||
| 2 | 2020-01-16 02:49:50 |
|
||||
+---------+---------------------+
|
||||
<strong>解释:</strong>
|
||||
6号用户登录了3次,但是在2020年仅有一次,所以结果集应包含此次登录。
|
||||
8号用户在2020年登录了2次,一次在2月,一次在12月,所以,结果集应该包含12月的这次登录。
|
||||
2号用户登录了2次,但是在2020年仅有一次,所以结果集应包含此次登录。
|
||||
14号用户在2020年没有登录,所以结果集不应包含。</pre>
|
@@ -0,0 +1,36 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>每一步操作中,你需要从数组中选出和为 <code>k</code> 的两个整数,并将它们移出数组。</p>
|
||||
|
||||
<p>返回你可以对数组执行的最大操作数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,4], k = 5
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>开始时 nums = [1,2,3,4]:
|
||||
- 移出 1 和 4 ,之后 nums = [2,3]
|
||||
- 移出 2 和 3 ,之后 nums = []
|
||||
不再有和为 5 的数对,因此最多执行 2 次操作。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,1,3,4,3], k = 6
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>开始时 nums = [3,1,3,4,3]:
|
||||
- 移出前两个 3 ,之后nums = [1,4,3]
|
||||
不再有和为 6 的数对,因此最多执行 1 次操作。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>给你一个整数 <code>n</code>(<code>10</code> 进制)和一个基数 <code>k</code> ,请你将 <code>n</code> 从 <code>10</code> 进制表示转换为 <code>k</code> 进制表示,计算并返回转换后各位数字的 <strong>总和</strong> 。</p>
|
||||
|
||||
<p>转换后,各位数字应当视作是 <code>10</code> 进制数字,且它们的总和也应当按 <code>10</code> 进制表示返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 34, k = 6
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>34 (10 进制) 在 6 进制下表示为 54 。5 + 4 = 9 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 10, k = 10
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>n 本身就是 10 进制。 1 + 0 = 1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>2 <= k <= 10</code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>给你 <code>nums</code> ,它是一个大小为 <code>2 * n</code> 的正整数数组。你必须对这个数组执行 <code>n</code> 次操作。</p>
|
||||
|
||||
<p>在第 <code>i</code> 次操作时(操作编号从 <strong>1</strong> 开始),你需要:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择两个元素 <code>x</code> 和 <code>y</code> 。</li>
|
||||
<li>获得分数 <code>i * gcd(x, y)</code> 。</li>
|
||||
<li>将 <code>x</code> 和 <code>y</code> 从 <code>nums</code> 中删除。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回 <code>n</code> 次操作后你能获得的分数和最大为多少。</p>
|
||||
|
||||
<p>函数 <code>gcd(x, y)</code> 是 <code>x</code> 和 <code>y</code> 的最大公约数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,2]
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>最优操作是:
|
||||
(1 * gcd(1, 2)) = 1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [3,4,6,8]
|
||||
<b>输出:</b>11
|
||||
<b>解释:</b>最优操作是:
|
||||
(1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,2,3,4,5,6]
|
||||
<b>输出:</b>14
|
||||
<b>解释:</b>最优操作是:
|
||||
(1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 7</code></li>
|
||||
<li><code>nums.length == 2 * n</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>给你一个无向图,整数 <code>n</code> 表示图中节点的数目,<code>edges</code> 数组表示图中的边,其中 <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> ,表示 <code>u<sub>i</sub></code> 和 <code>v<sub>i</sub></code><sub> </sub>之间有一条无向边。</p>
|
||||
|
||||
<p>一个 <strong>连通三元组</strong> 指的是 <strong>三个</strong> 节点组成的集合且这三个点之间 <strong>两两</strong> 有边。</p>
|
||||
|
||||
<p><strong>连通三元组的度数</strong> 是所有满足此条件的边的数目:一个顶点在这个三元组内,而另一个顶点不在这个三元组内。</p>
|
||||
|
||||
<p>请你返回所有连通三元组中度数的 <strong>最小值</strong> ,如果图中没有连通三元组,那么返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/02/14/trios1.png" style="width: 388px; height: 164px;" />
|
||||
<pre>
|
||||
<b>输入:</b>n = 6, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]]
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>只有一个三元组 [1,2,3] 。构成度数的边在上图中已被加粗。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/02/14/trios2.png" style="width: 388px; height: 164px;" />
|
||||
<pre>
|
||||
<b>输入:</b>n = 7, edges = [[1,3],[4,1],[4,3],[2,5],[5,6],[6,7],[7,5],[2,6]]
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>有 3 个三元组:
|
||||
1) [1,4,3],度数为 0 。
|
||||
2) [2,5,6],度数为 2 。
|
||||
3) [5,6,7],度数为 2 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 400</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>1 <= edges.length <= n * (n-1) / 2</code></li>
|
||||
<li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li>
|
||||
<li><code>u<sub>i </sub>!= v<sub>i</sub></code></li>
|
||||
<li>图中没有重复的边。</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>给你两个 <strong>非递增</strong> 的整数数组 <code>nums1</code> 和 <code>nums2</code> ,数组下标均 <strong>从 0 开始</strong> 计数。</p>
|
||||
|
||||
<p>下标对 <code>(i, j)</code> 中 <code>0 <= i < nums1.length</code> 且 <code>0 <= j < nums2.length</code> 。如果该下标对同时满足 <code>i <= j</code> 且 <code>nums1[i] <= nums2[j]</code> ,则称之为 <strong>有效</strong> 下标对,该下标对的 <strong>距离</strong> 为 <code>j - i</code> 。</p>
|
||||
|
||||
<p>返回所有 <strong>有效</strong> 下标对<em> </em><code>(i, j)</code><em> </em>中的 <strong>最大距离</strong> 。如果不存在有效下标对,返回 <code>0</code> 。</p>
|
||||
|
||||
<p>一个数组 <code>arr</code> ,如果每个 <code>1 <= i < arr.length</code> 均有 <code>arr[i-1] >= arr[i]</code> 成立,那么该数组是一个 <strong>非递增</strong> 数组。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>有效下标对是 (0,0), (2,2), (2,3), (2,4), (3,3), (3,4) 和 (4,4) 。
|
||||
最大距离是 2 ,对应下标对 (2,4) 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [2,2,2], nums2 = [10,10,1]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>有效下标对是 (0,0), (0,1) 和 (1,1) 。
|
||||
最大距离是 1 ,对应下标对 (0,1) 。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [30,29,19,5], nums2 = [25,25,25,25,25]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>有效下标对是 (2,2), (2,3), (2,4), (3,3) 和 (3,4) 。
|
||||
最大距离是 2 ,对应下标对 (2,4) 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums1[i], nums2[j] <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums1</code> 和 <code>nums2</code> 都是 <strong>非递增</strong> 数组</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>给你一个由非负整数组成的数组 <code>nums</code> 。另有一个查询数组 <code>queries</code> ,其中 <code>queries[i] = [x<sub>i</sub>, m<sub>i</sub>]</code> 。</p>
|
||||
|
||||
<p>第 <code>i</code> 个查询的答案是 <code>x<sub>i</sub></code> 和任何 <code>nums</code> 数组中不超过 <code>m<sub>i</sub></code> 的元素按位异或(<code>XOR</code>)得到的最大值。换句话说,答案是 <code>max(nums[j] XOR x<sub>i</sub>)</code> ,其中所有 <code>j</code> 均满足 <code>nums[j] <= m<sub>i</sub></code> 。如果 <code>nums</code> 中的所有元素都大于 <code>m<sub>i</sub></code>,最终答案就是 <code>-1</code> 。</p>
|
||||
|
||||
<p>返回一个整数数组<em> </em><code>answer</code><em> </em>作为查询的答案,其中<em> </em><code>answer.length == queries.length</code><em> </em>且<em> </em><code>answer[i]</code><em> </em>是第<em> </em><code>i</code><em> </em>个查询的答案。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]
|
||||
<strong>输出:</strong>[3,3,7]
|
||||
<strong>解释:</strong>
|
||||
1) 0 和 1 是仅有的两个不超过 1 的整数。0 XOR 3 = 3 而 1 XOR 3 = 2 。二者中的更大值是 3 。
|
||||
2) 1 XOR 2 = 3.
|
||||
3) 5 XOR 2 = 7.
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [5,2,4,6,6,3], queries = [[12,4],[8,1],[6,3]]
|
||||
<strong>输出:</strong>[15,-1,5]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length, queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[i].length == 2</code></li>
|
||||
<li><code>0 <= nums[j], x<sub>i</sub>, m<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>两个数对 <code>(a, b)</code> 和 <code>(c, d)</code> 之间的 <strong>乘积差</strong> 定义为 <code>(a * b) - (c * d)</code> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>(5, 6)</code> 和 <code>(2, 7)</code> 之间的乘积差是 <code>(5 * 6) - (2 * 7) = 16</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个整数数组 <code>nums</code> ,选出四个 <strong>不同的</strong> 下标 <code>w</code>、<code>x</code>、<code>y</code> 和 <code>z</code> ,使数对 <code>(nums[w], nums[x])</code> 和 <code>(nums[y], nums[z])</code> 之间的 <strong>乘积差</strong> 取到 <strong>最大值</strong> 。</p>
|
||||
|
||||
<p>返回以这种方式取得的乘积差中的 <strong>最大值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [5,6,2,7,4]
|
||||
<strong>输出:</strong>34
|
||||
<strong>解释:</strong>可以选出下标为 1 和 3 的元素构成第一个数对 (6, 7) 以及下标 2 和 4 构成第二个数对 (2, 4)
|
||||
乘积差是 (6 * 7) - (2 * 4) = 34
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [4,2,5,9,7,4,8]
|
||||
<strong>输出:</strong>64
|
||||
<strong>解释:</strong>可以选出下标为 3 和 6 的元素构成第一个数对 (9, 8) 以及下标 1 和 5 构成第二个数对 (2, 4)
|
||||
乘积差是 (9 * 8) - (2 * 4) = 64
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>4 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>给你两个整数数组 <code>nums1</code> 和 <code>nums2</code> ,它们长度都为 <code>n</code> 。</p>
|
||||
|
||||
<p>两个数组的 <strong>异或值之和</strong> 为 <code>(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])</code> (<strong>下标从 0 开始</strong>)。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,<code>[1,2,3]</code> 和 <code>[3,2,1]</code> 的 <strong>异或值之和</strong> 等于 <code>(1 XOR 3) + (2 XOR 2) + (3 XOR 1) = 2 + 0 + 2 = 4</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你将 <code>nums2</code> 中的元素重新排列,使得 <strong>异或值之和</strong> <strong>最小</strong> 。</p>
|
||||
|
||||
<p>请你返回重新排列之后的 <strong>异或值之和</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums1 = [1,2], nums2 = [2,3]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>将 <code>nums2</code> 重新排列得到 <code>[3,2] 。</code>
|
||||
异或值之和为 (1 XOR 3) + (2 XOR 2) = 2 + 0 = 2 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums1 = [1,0,3], nums2 = [5,3,4]
|
||||
<b>输出:</b>8
|
||||
<b>解释:</b>将 <code>nums2 重新排列得到</code> <code>[5,4,3] 。</code>
|
||||
异或值之和为 (1 XOR 5) + (0 XOR 4) + (3 XOR 3) = 4 + 4 + 0 = 8 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length</code></li>
|
||||
<li><code>n == nums2.length</code></li>
|
||||
<li><code>1 <= n <= 14</code></li>
|
||||
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>给你一个字符串 <code>s</code>,请你返回 <strong>两个相同字符之间的最长子字符串的长度</strong> <em>,</em>计算长度时不含这两个字符。如果不存在这样的子字符串,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p><strong>子字符串</strong> 是字符串中的一个连续字符序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "aa"
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>最优的子字符串是两个 'a' 之间的空子字符串。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "abca"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>最优的子字符串是 "bc" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "cbzxy"
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>s 中不存在出现出现两次的字符,所以返回 -1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "cabbac"
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>最优的子字符串是 "abba" ,其他的非最优解包括 "bb" 和 "" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 300</code></li>
|
||||
<li><code>s</code> 只含小写英文字母</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>给你 <code>n</code> 个二维平面上的点 <code>points</code> ,其中 <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> ,请你返回两点之间内部不包含任何点的 <strong>最宽垂直面积</strong> 的宽度。</p>
|
||||
|
||||
<p><strong>垂直面积</strong> 的定义是固定宽度,而 y 轴上无限延伸的一块区域(也就是高度为无穷大)。 <strong>最宽垂直面积</strong> 为宽度最大的一个垂直面积。</p>
|
||||
|
||||
<p>请注意,垂直区域 <strong>边上</strong> 的点 <strong>不在</strong> 区域内。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/10/31/points3.png" style="width: 276px; height: 371px;" />
|
||||
<pre>
|
||||
<b>输入:</b>points = [[8,7],[9,9],[7,4],[9,7]]
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>红色区域和蓝色区域都是最优区域。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>points = [[3,1],[9,0],[1,0],[1,4],[5,3],[8,8]]
|
||||
<b>输出:</b>3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == points.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>在代号为 C-137 的地球上,Rick 发现如果他将两个球放在他新发明的篮子里,它们之间会形成特殊形式的磁力。Rick 有 <code>n</code> 个空的篮子,第 <code>i</code> 个篮子的位置在 <code>position[i]</code> ,Morty 想把 <code>m</code> 个球放到这些篮子里,使得任意两球间 <strong>最小磁力</strong> 最大。</p>
|
||||
|
||||
<p>已知两个球如果分别位于 <code>x</code> 和 <code>y</code> ,那么它们之间的磁力为 <code>|x - y|</code> 。</p>
|
||||
|
||||
<p>给你一个整数数组 <code>position</code> 和一个整数 <code>m</code> ,请你返回最大化的最小磁力。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/16/q3v1.jpg" style="height: 195px; width: 562px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>position = [1,2,3,4,7], m = 3
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>将 3 个球分别放入位于 1,4 和 7 的三个篮子,两球间的磁力分别为 [3, 3, 6]。最小磁力为 3 。我们没办法让最小磁力大于 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>position = [5,4,3,2,1,1000000000], m = 2
|
||||
<strong>输出:</strong>999999999
|
||||
<strong>解释:</strong>我们使用位于 1 和 1000000000 的篮子时最小磁力最大。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == position.length</code></li>
|
||||
<li><code>2 <= n <= 10^5</code></li>
|
||||
<li><code>1 <= position[i] <= 10^9</code></li>
|
||||
<li>所有 <code>position</code> 中的整数 <strong>互不相同</strong> 。</li>
|
||||
<li><code>2 <= m <= position.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一个整数数组 <code>nums</code> ,请你求出乘积为正数的最长子数组的长度。</p>
|
||||
|
||||
<p>一个数组的子数组是由原数组中零个或者更多个连续数字组成的数组。</p>
|
||||
|
||||
<p>请你返回乘积为正数的最长子数组长度。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,-2,-3,4]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>数组本身乘积就是正数,值为 24 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0,1,-2,-3,-4]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>最长乘积为正数的子数组为 [1,-2,-3] ,乘积为 6 。
|
||||
注意,我们不能把 0 也包括到子数组中,因为这样乘积为 0 ,不是正数。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [-1,-2,-3,0,1]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>乘积为正数的最长子数组是 [-1,-2] 或者 [-2,-3] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10^5</code></li>
|
||||
<li><code>-10^9 <= nums[i] <= 10^9</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,49 @@
|
||||
<p>给你一个二维字符网格数组 <code>grid</code> ,大小为 <code>m x n</code> ,你需要检查 <code>grid</code> 中是否存在 <strong>相同值</strong> 形成的环。</p>
|
||||
|
||||
<p>一个环是一条开始和结束于同一个格子的长度 <strong>大于等于 4</strong> 的路径。对于一个给定的格子,你可以移动到它上、下、左、右四个方向相邻的格子之一,可以移动的前提是这两个格子有 <strong>相同的值 </strong>。</p>
|
||||
|
||||
<p>同时,你也不能回到上一次移动时所在的格子。比方说,环 <code>(1, 1) -> (1, 2) -> (1, 1)</code> 是不合法的,因为从 <code>(1, 2)</code> 移动到 <code>(1, 1)</code> 回到了上一次移动时的格子。</p>
|
||||
|
||||
<p>如果 <code>grid</code> 中有相同值形成的环,请你返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/22/5482e1.png" style="height: 152px; width: 231px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>grid = [["a","a","a","a"],["a","b","b","a"],["a","b","b","a"],["a","a","a","a"]]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>如下图所示,有 2 个用不同颜色标出来的环:
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/22/5482e11.png" style="height: 163px; width: 225px;">
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/22/5482e2.png" style="height: 154px; width: 236px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>grid = [["c","c","c","a"],["c","d","c","c"],["c","c","e","c"],["f","c","c","c"]]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>如下图所示,只有高亮所示的一个合法环:
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/22/5482e22.png" style="height: 157px; width: 229px;">
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/22/5482e3.png" style="height: 120px; width: 183px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>grid = [["a","b","b"],["b","z","b"],["b","b","a"]]
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m <= 500</code></li>
|
||||
<li><code>1 <= n <= 500</code></li>
|
||||
<li><code>grid</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>给你一个大小为 <code>rows x cols</code> 的矩阵 <code>mat</code>,其中 <code>mat[i][j]</code> 是 <code>0</code> 或 <code>1</code>,请返回 <strong>矩阵 <em><code>mat</code></em> 中特殊位置的数目</strong> 。</p>
|
||||
|
||||
<p><strong>特殊位置</strong> 定义:如果 <code>mat[i][j] == 1</code> 并且第 <code>i</code> 行和第 <code>j</code> 列中的所有其他元素均为 <code>0</code>(行和列的下标均 <strong>从 0 开始</strong> ),则位置 <code>(i, j)</code> 被称为特殊位置。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>mat = [[1,0,0],
|
||||
[0,0,<strong>1</strong>],
|
||||
[1,0,0]]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>(1,2) 是一个特殊位置,因为 mat[1][2] == 1 且所处的行和列上所有其他元素都是 0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>mat = [[<strong>1</strong>,0,0],
|
||||
[0,<strong>1</strong>,0],
|
||||
[0,0,<strong>1</strong>]]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>(0,0), (1,1) 和 (2,2) 都是特殊位置
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>mat = [[0,0,0,<strong>1</strong>],
|
||||
[<strong>1</strong>,0,0,0],
|
||||
[0,1,1,0],
|
||||
[0,0,0,0]]
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>mat = [[0,0,0,0,0],
|
||||
[<strong>1</strong>,0,0,0,0],
|
||||
[0,<strong>1</strong>,0,0,0],
|
||||
[0,0,<strong>1</strong>,0,0],
|
||||
[0,0,0,1,1]]
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>rows == mat.length</code></li>
|
||||
<li><code>cols == mat[i].length</code></li>
|
||||
<li><code>1 <= rows, cols <= 100</code></li>
|
||||
<li><code>mat[i][j]</code> 是 <code>0</code> 或 <code>1</code></li>
|
||||
</ul>
|
48
算法题(国内版)/problem (Chinese)/互质树 [tree-of-coprimes].html
Normal file
48
算法题(国内版)/problem (Chinese)/互质树 [tree-of-coprimes].html
Normal file
@@ -0,0 +1,48 @@
|
||||
<p>给你一个 <code>n</code> 个节点的树(也就是一个无环连通无向图),节点编号从 <code>0</code> 到 <code>n - 1</code> ,且恰好有 <code>n - 1</code> 条边,每个节点有一个值。树的 <strong>根节点</strong> 为 0 号点。</p>
|
||||
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个二维数组 <code>edges</code> 来表示这棵树。<code>nums[i]</code> 表示第 <code>i</code> 个点的值,<code>edges[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> 表示节点 <code>u<sub>j</sub></code> 和节点 <code>v<sub>j</sub></code> 在树中有一条边。</p>
|
||||
|
||||
<p>当 <code>gcd(x, y) == 1</code> ,我们称两个数 <code>x</code> 和 <code>y</code> 是 <strong>互质的</strong> ,其中 <code>gcd(x, y)</code> 是 <code>x</code> 和 <code>y</code> 的 <strong>最大公约数</strong> 。</p>
|
||||
|
||||
<p>从节点 <code>i</code> 到 <strong>根</strong> 最短路径上的点都是节点 <code>i</code> 的祖先节点。一个节点 <strong>不是</strong> 它自己的祖先节点。</p>
|
||||
|
||||
<p>请你返回一个大小为 <code>n</code> 的数组 <code>ans</code> ,其中<em> </em><code>ans[i]</code>是离节点 <code>i</code> 最近的祖先节点且满足<em> </em><code>nums[i]</code> 和<em> </em><code>nums[ans[i]]</code> 是 <strong>互质的</strong> ,如果不存在这样的祖先节点,<code>ans[i]</code> 为 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/02/20/untitled-diagram.png" style="width: 191px; height: 281px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,3,3,2], edges = [[0,1],[1,2],[1,3]]
|
||||
<b>输出:</b>[-1,0,0,1]
|
||||
<b>解释:</b>上图中,每个节点的值在括号中表示。
|
||||
- 节点 0 没有互质祖先。
|
||||
- 节点 1 只有一个祖先节点 0 。它们的值是互质的(gcd(2,3) == 1)。
|
||||
- 节点 2 有两个祖先节点,分别是节点 1 和节点 0 。节点 1 的值与它的值不是互质的(gcd(3,3) == 3)但节点 0 的值是互质的(gcd(2,3) == 1),所以节点 0 是最近的符合要求的祖先节点。
|
||||
- 节点 3 有两个祖先节点,分别是节点 1 和节点 0 。它与节点 1 互质(gcd(3,2) == 1),所以节点 1 是离它最近的符合要求的祖先节点。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/02/20/untitled-diagram1.png" style="width: 441px; height: 291px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [5,6,10,2,3,6,15], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]
|
||||
<b>输出:</b>[-1,0,-1,0,0,0,-1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums.length == n</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[j].length == 2</code></li>
|
||||
<li><code>0 <= u<sub>j</sub>, v<sub>j</sub> < n</code></li>
|
||||
<li><code>u<sub>j</sub> != v<sub>j</sub></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>给你两个字符串 <code>word1</code> 和 <code>word2</code> 。请你从 <code>word1</code> 开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到合并后字符串的末尾。</p>
|
||||
|
||||
<p>返回 <strong>合并后的字符串</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word1 = "abc", word2 = "pqr"
|
||||
<strong>输出:</strong>"apbqcr"
|
||||
<strong>解释:</strong>字符串合并情况如下所示:
|
||||
word1: a b c
|
||||
word2: p q r
|
||||
合并后: a p b q c r
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word1 = "ab", word2 = "pqrs"
|
||||
<strong>输出:</strong>"apbqrs"
|
||||
<strong>解释:</strong>注意,word2 比 word1 长,"rs" 需要追加到合并后字符串的末尾。
|
||||
word1: a b
|
||||
word2: p q r s
|
||||
合并后: a p b q r s
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word1 = "abcd", word2 = "pq"
|
||||
<strong>输出:</strong>"apbqcd"
|
||||
<strong>解释:</strong>注意,word1 比 word2 长,"cd" 需要追加到合并后字符串的末尾。
|
||||
word1: a b c d
|
||||
word2: p q
|
||||
合并后: a p b q c d
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word1.length, word2.length <= 100</code></li>
|
||||
<li><code>word1</code> 和 <code>word2</code> 由小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>给你一个二维整数数组 <code>logs</code> ,其中每个 <code>logs[i] = [birth<sub>i</sub>, death<sub>i</sub>]</code> 表示第 <code>i</code> 个人的出生和死亡年份。</p>
|
||||
|
||||
<p>年份 <code>x</code> 的 <strong>人口</strong> 定义为这一年期间活着的人的数目。第 <code>i</code> 个人被计入年份 <code>x</code> 的人口需要满足:<code>x</code> 在闭区间 <code>[birth<sub>i</sub>, death<sub>i</sub> - 1]</code> 内。注意,人不应当计入他们死亡当年的人口中。</p>
|
||||
|
||||
<p>返回 <strong>人口最多</strong> 且 <strong>最早</strong> 的年份。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>logs = [[1993,1999],[2000,2010]]
|
||||
<strong>输出:</strong>1993
|
||||
<strong>解释:</strong>人口最多为 1 ,而 1993 是人口为 1 的最早年份。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>logs = [[1950,1961],[1960,1971],[1970,1981]]
|
||||
<strong>输出:</strong>1960
|
||||
<strong>解释:</strong>
|
||||
人口最多为 2 ,分别出现在 1960 和 1970 。
|
||||
其中最早年份是 1960 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= logs.length <= 100</code></li>
|
||||
<li><code>1950 <= birth<sub>i</sub> < death<sub>i</sub> <= 2050</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你长度相等的两个字符串 <code>s1</code> 和 <code>s2</code> 。一次<strong> 字符串交换 </strong>操作的步骤如下:选出某个字符串中的两个下标(不必不同),并交换这两个下标所对应的字符。</p>
|
||||
|
||||
<p>如果对 <strong>其中一个字符串</strong> 执行 <strong>最多一次字符串交换</strong> 就可以使两个字符串相等,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s1 = "bank", s2 = "kanb"
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>例如,交换 s2 中的第一个和最后一个字符可以得到 "bank"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s1 = "attack", s2 = "defend"
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>一次字符串交换无法使两个字符串相等
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s1 = "kelb", s2 = "kelb"
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>两个字符串已经相等,所以不需要进行字符串交换
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s1 = "abcd", s2 = "dcba"
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s1.length, s2.length <= 100</code></li>
|
||||
<li><code>s1.length == s2.length</code></li>
|
||||
<li><code>s1</code> 和 <code>s2</code> 仅由小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,80 @@
|
||||
<p>你有一辆货运卡车,你需要用这一辆车把一些箱子从仓库运送到码头。这辆卡车每次运输有 <strong>箱子数目的限制</strong> 和 <strong>总重量的限制</strong> 。</p>
|
||||
|
||||
<p>给你一个箱子数组 <code>boxes</code> 和三个整数 <code>portsCount</code>, <code>maxBoxes</code> 和 <code>maxWeight</code> ,其中 <code>boxes[i] = [ports<sub>i</sub>, weight<sub>i</sub>]</code> 。</p>
|
||||
|
||||
<ul>
|
||||
<li><code>ports<sub>i</sub></code> 表示第 <code>i</code> 个箱子需要送达的码头, <code>weights<sub>i</sub></code> 是第 <code>i</code> 个箱子的重量。</li>
|
||||
<li><code>portsCount</code> 是码头的数目。</li>
|
||||
<li><code>maxBoxes</code> 和 <code>maxWeight</code> 分别是卡车每趟运输箱子数目和重量的限制。</li>
|
||||
</ul>
|
||||
|
||||
<p>箱子需要按照 <strong>数组顺序</strong> 运输,同时每次运输需要遵循以下步骤:</p>
|
||||
|
||||
<ul>
|
||||
<li>卡车从 <code>boxes</code> 队列中按顺序取出若干个箱子,但不能违反 <code>maxBoxes</code> 和 <code>maxWeight</code> 限制。</li>
|
||||
<li>对于在卡车上的箱子,我们需要 <strong>按顺序</strong> 处理它们,卡车会通过 <strong>一趟行程</strong> 将最前面的箱子送到目的地码头并卸货。如果卡车已经在对应的码头,那么不需要 <strong>额外行程</strong> ,箱子也会立马被卸货。</li>
|
||||
<li>卡车上所有箱子都被卸货后,卡车需要 <strong>一趟行程</strong> 回到仓库,从箱子队列里再取出一些箱子。</li>
|
||||
</ul>
|
||||
|
||||
<p>卡车在将所有箱子运输并卸货后,最后必须回到仓库。</p>
|
||||
|
||||
<p>请你返回将所有箱子送到相应码头的 <b>最少行程</b> 次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>boxes = [[1,1],[2,1],[1,1]], portsCount = 2, maxBoxes = 3, maxWeight = 3
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>最优策略如下:
|
||||
- 卡车将所有箱子装上车,到达码头 1 ,然后去码头 2 ,然后再回到码头 1 ,最后回到仓库,总共需要 4 趟行程。
|
||||
所以总行程数为 4 。
|
||||
注意到第一个和第三个箱子不能同时被卸货,因为箱子需要按顺序处理(也就是第二个箱子需要先被送到码头 2 ,然后才能处理第三个箱子)。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>boxes = [[1,2],[3,3],[3,1],[3,1],[2,4]], portsCount = 3, maxBoxes = 3, maxWeight = 6
|
||||
<b>输出:</b>6
|
||||
<b>解释:</b>最优策略如下:
|
||||
- 卡车首先运输第一个箱子,到达码头 1 ,然后回到仓库,总共 2 趟行程。
|
||||
- 卡车运输第二、第三、第四个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。
|
||||
- 卡车运输第五个箱子,到达码头 3 ,回到仓库,总共 2 趟行程。
|
||||
总行程数为 2 + 2 + 2 = 6 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>boxes = [[1,4],[1,2],[2,1],[2,1],[3,2],[3,4]], portsCount = 3, maxBoxes = 6, maxWeight = 7
|
||||
<b>输出:</b>6
|
||||
<b>解释:</b>最优策略如下:
|
||||
- 卡车运输第一和第二个箱子,到达码头 1 ,然后回到仓库,总共 2 趟行程。
|
||||
- 卡车运输第三和第四个箱子,到达码头 2 ,然后回到仓库,总共 2 趟行程。
|
||||
- 卡车运输第五和第六个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。
|
||||
总行程数为 2 + 2 + 2 = 6 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>boxes = [[2,4],[2,5],[3,1],[3,2],[3,7],[3,1],[4,4],[1,3],[5,2]], portsCount = 5, maxBoxes = 5, maxWeight = 7
|
||||
<b>输出:</b>14
|
||||
<b>解释:</b>最优策略如下:
|
||||
- 卡车运输第一个箱子,到达码头 2 ,然后回到仓库,总共 2 趟行程。
|
||||
- 卡车运输第二个箱子,到达码头 2 ,然后回到仓库,总共 2 趟行程。
|
||||
- 卡车运输第三和第四个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。
|
||||
- 卡车运输第五个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。
|
||||
- 卡车运输第六和第七个箱子,到达码头 3 ,然后去码头 4 ,然后回到仓库,总共 3 趟行程。
|
||||
- 卡车运输第八和第九个箱子,到达码头 1 ,然后去码头 5 ,然后回到仓库,总共 3 趟行程。
|
||||
总行程数为 2 + 2 + 2 + 2 + 3 + 3 = 14 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= boxes.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= portsCount, maxBoxes, maxWeight <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= ports<sub>i</sub> <= portsCount</code></li>
|
||||
<li><code>1 <= weights<sub>i</sub> <= maxWeight</code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>存在一个由 <code>n</code> 个不同元素组成的整数数组 <code>nums</code> ,但你已经记不清具体内容。好在你还记得 <code>nums</code> 中的每一对相邻元素。</p>
|
||||
|
||||
<p>给你一个二维整数数组 <code>adjacentPairs</code> ,大小为 <code>n - 1</code> ,其中每个 <code>adjacentPairs[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> 表示元素 <code>u<sub>i</sub></code> 和 <code>v<sub>i</sub></code> 在 <code>nums</code> 中相邻。</p>
|
||||
|
||||
<p>题目数据保证所有由元素 <code>nums[i]</code> 和 <code>nums[i+1]</code> 组成的相邻元素对都存在于 <code>adjacentPairs</code> 中,存在形式可能是 <code>[nums[i], nums[i+1]]</code> ,也可能是 <code>[nums[i+1], nums[i]]</code> 。这些相邻元素对可以 <strong>按任意顺序</strong> 出现。</p>
|
||||
|
||||
<p>返回 <strong>原始数组</strong><em> </em><code>nums</code><em> </em>。如果存在多种解答,返回 <strong>其中任意一个</strong> 即可。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>adjacentPairs = [[2,1],[3,4],[3,2]]
|
||||
<strong>输出:</strong>[1,2,3,4]
|
||||
<strong>解释:</strong>数组的所有相邻元素对都在 adjacentPairs 中。
|
||||
特别要注意的是,adjacentPairs[i] 只表示两个元素相邻,并不保证其 左-右 顺序。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>adjacentPairs = [[4,-2],[1,4],[-3,1]]
|
||||
<strong>输出:</strong>[-2,4,1,-3]
|
||||
<strong>解释:</strong>数组中可能存在负数。
|
||||
另一种解答是 [-3,1,4,-2] ,也会被视作正确答案。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>adjacentPairs = [[100000,-100000]]
|
||||
<strong>输出:</strong>[100000,-100000]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums.length == n</code></li>
|
||||
<li><code>adjacentPairs.length == n - 1</code></li>
|
||||
<li><code>adjacentPairs[i].length == 2</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= nums[i], u<sub>i</sub>, v<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
<li>题目数据保证存在一些以 <code>adjacentPairs</code> 作为元素对的数组 <code>nums</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>现有一个加权无向连通图。给你一个正整数 <code>n</code> ,表示图中有 <code>n</code> 个节点,并按从 <code>1</code> 到 <code>n</code> 给节点编号;另给你一个数组 <code>edges</code> ,其中每个 <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, weight<sub>i</sub>]</code> 表示存在一条位于节点 <code>u<sub>i</sub></code> 和 <code>v<sub>i</sub></code> 之间的边,这条边的权重为 <code>weight<sub>i</sub></code> 。</p>
|
||||
|
||||
<p>从节点 <code>start</code> 出发到节点 <code>end</code> 的路径是一个形如 <code>[z<sub>0</sub>, z<sub>1</sub>,<sub> </sub>z<sub>2</sub>, ..., z<sub>k</sub>]</code> 的节点序列,满足 <code>z<sub>0 </sub>= start</code> 、<code>z<sub>k</sub> = end</code> 且在所有符合 <code>0 <= i <= k-1</code> 的节点 <code>z<sub>i</sub></code> 和 <code>z<sub>i+1</sub></code> 之间存在一条边。</p>
|
||||
|
||||
<p>路径的距离定义为这条路径上所有边的权重总和。用 <code>distanceToLastNode(x)</code> 表示节点 <code>n</code> 和 <code>x</code> 之间路径的最短距离。<strong>受限路径</strong> 为满足 <code>distanceToLastNode(z<sub>i</sub>) > distanceToLastNode(z<sub>i+1</sub>)</code> 的一条路径,其中 <code>0 <= i <= k-1</code> 。</p>
|
||||
|
||||
<p>返回从节点 <code>1</code> 出发到节点 <code>n</code> 的 <strong>受限路径数</strong> 。由于数字可能很大,请返回对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 的结果。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/03/07/restricted_paths_ex1.png" style="width: 351px; height: 341px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 5, edges = [[1,2,3],[1,3,3],[2,3,1],[1,4,2],[5,2,2],[3,5,1],[5,4,10]]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>每个圆包含黑色的节点编号和蓝色的 distanceToLastNode 值。三条受限路径分别是:
|
||||
1) 1 --> 2 --> 5
|
||||
2) 1 --> 2 --> 3 --> 5
|
||||
3) 1 --> 3 --> 5
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/03/07/restricted_paths_ex22.png" style="width: 356px; height: 401px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 7, edges = [[1,3,1],[4,1,2],[7,3,4],[2,5,3],[5,6,1],[6,7,2],[7,5,3],[2,6,4]]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>每个圆包含黑色的节点编号和蓝色的 distanceToLastNode 值。唯一一条受限路径是:1 --> 3 --> 7 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>n - 1 <= edges.length <= 4 * 10<sup>4</sup></code></li>
|
||||
<li><code>edges[i].length == 3</code></li>
|
||||
<li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li>
|
||||
<li><code>u<sub>i </sub>!= v<sub>i</sub></code></li>
|
||||
<li><code>1 <= weight<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
<li>任意两个节点之间至多存在一条边</li>
|
||||
<li>任意两个节点之间至少存在一条路径</li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 。一个子数组 <code>[nums<sub>l</sub>, nums<sub>l+1</sub>, ..., nums<sub>r-1</sub>, nums<sub>r</sub>]</code> 的 <strong>和的绝对值</strong> 为 <code>abs(nums<sub>l</sub> + nums<sub>l+1</sub> + ... + nums<sub>r-1</sub> + nums<sub>r</sub>)</code> 。</p>
|
||||
|
||||
<p>请你找出 <code>nums</code> 中 <strong>和的绝对值</strong> 最大的任意子数组(<b>可能为空</b>),并返回该 <strong>最大值</strong> 。</p>
|
||||
|
||||
<p><code>abs(x)</code> 定义如下:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果 <code>x</code> 是负整数,那么 <code>abs(x) = -x</code> 。</li>
|
||||
<li>如果 <code>x</code> 是非负整数,那么 <code>abs(x) = x</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,-3,2,3,-4]
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>子数组 [2,3] 和的绝对值最大,为 abs(2+3) = abs(5) = 5 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,-5,1,-4,3,-2]
|
||||
<b>输出:</b>8
|
||||
<b>解释:</b>子数组 [-5,1,-4] 和的绝对值最大,为 abs(-5+1-4) = abs(-8) = 8 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>有 3n 堆数目不一的硬币,你和你的朋友们打算按以下方式分硬币:</p>
|
||||
|
||||
<ul>
|
||||
<li>每一轮中,你将会选出 <strong>任意</strong> 3 堆硬币(不一定连续)。</li>
|
||||
<li>Alice 将会取走硬币数量最多的那一堆。</li>
|
||||
<li>你将会取走硬币数量第二多的那一堆。</li>
|
||||
<li>Bob 将会取走最后一堆。</li>
|
||||
<li>重复这个过程,直到没有更多硬币。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个整数数组 <code>piles</code> ,其中 <code>piles[i]</code> 是第 <code>i</code> 堆中硬币的数目。</p>
|
||||
|
||||
<p>返回你可以获得的最大硬币数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>piles = [2,4,1,2,7,8]
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>选出 (2, 7, 8) ,Alice 取走 8 枚硬币的那堆,你取走 <strong>7</strong> 枚硬币的那堆,Bob 取走最后一堆。
|
||||
选出 (1, 2, 4) , Alice 取走 4 枚硬币的那堆,你取走 <strong>2</strong> 枚硬币的那堆,Bob 取走最后一堆。
|
||||
你可以获得的最大硬币数目:7 + 2 = 9.
|
||||
考虑另外一种情况,如果选出的是 (1, <strong>2</strong>, 8) 和 (2, <strong>4</strong>, 7) ,你就只能得到 2 + 4 = 6 枚硬币,这不是最优解。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>piles = [2,4,5]
|
||||
<strong>输出:</strong>4
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>piles = [9,8,7,6,5,1,2,3,4]
|
||||
<strong>输出:</strong>18
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= piles.length <= 10^5</code></li>
|
||||
<li><code>piles.length % 3 == 0</code></li>
|
||||
<li><code>1 <= piles[i] <= 10^4</code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>一款新的在线电子游戏在近期发布,在该电子游戏中,以 <strong>刻钟</strong> 为周期规划若干时长为 <strong>15 分钟</strong> 的游戏对局。这意味着,在 <code>HH:00</code>、<code>HH:15</code>、<code>HH:30</code> 和 <code>HH:45</code> ,将会开始一个新的对局,其中 <code>HH</code> 用一个从 <code>00</code> 到 <code>23</code> 的整数表示。游戏中使用 <strong>24 小时制的时钟</strong> ,所以一天中最早的时间是 <code>00:00</code> ,最晚的时间是 <code>23:59</code> 。</p>
|
||||
|
||||
<p>给你两个字符串 <code>startTime</code> 和 <code>finishTime</code> ,均符合 <code>"HH:MM"</code> 格式,分别表示你 <strong>进入</strong> 和 <strong>退出</strong> 游戏的确切时间,请计算在整个游戏会话期间,你完成的 <strong>完整对局的对局数</strong> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,如果 <code>startTime = "05:20"</code> 且 <code>finishTime = "05:59"</code> ,这意味着你仅仅完成从 <code>05:30</code> 到 <code>05:45</code> 这一个完整对局。而你没有完成从 <code>05:15</code> 到 <code>05:30</code> 的完整对局,因为你是在对局开始后进入的游戏;同时,你也没有完成从 <code>05:45</code> 到 <code>06:00</code> 的完整对局,因为你是在对局结束前退出的游戏。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果 <code>finishTime</code> <strong>早于</strong> <code>startTime</code> ,这表示你玩了个通宵(也就是从 <code>startTime</code> 到午夜,再从午夜到 <code>finishTime</code>)。</p>
|
||||
|
||||
<p>假设你是从 <code>startTime</code> 进入游戏,并在 <code>finishTime</code> 退出游戏,请计算并返回你完成的 <strong>完整对局的对局数</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>startTime = "12:01", finishTime = "12:44"
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>你完成了从 12:15 到 12:30 的一个完整对局。
|
||||
你没有完成从 12:00 到 12:15 的完整对局,因为你是在对局开始后的 12:01 进入的游戏。
|
||||
你没有完成从 12:30 到 12:45 的完整对局,因为你是在对局结束前的 12:44 退出的游戏。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>startTime = "20:00", finishTime = "06:00"
|
||||
<strong>输出:</strong>40
|
||||
<strong>解释:</strong>你完成了从 20:00 到 00:00 的 16 个完整的对局,以及从 00:00 到 06:00 的 24 个完整的对局。
|
||||
16 + 24 = 40
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>startTime = "00:00", finishTime = "23:59"
|
||||
<strong>输出:</strong>95
|
||||
<strong>解释:</strong>除最后一个小时你只完成了 3 个完整对局外,其余每个小时均完成了 4 场完整对局。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>startTime</code> 和 <code>finishTime</code> 的格式为 <code>HH:MM</code></li>
|
||||
<li><code>00 <= HH <= 23</code></li>
|
||||
<li><code>00 <= MM <= 59</code></li>
|
||||
<li><code>startTime</code> 和 <code>finishTime</code> 不相等</li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的正整数数组 <code>candiesCount</code> ,其中 <code>candiesCount[i]</code> 表示你拥有的第 <code>i</code> 类糖果的数目。同时给你一个二维数组 <code>queries</code> ,其中 <code>queries[i] = [favoriteType<sub>i</sub>, favoriteDay<sub>i</sub>, dailyCap<sub>i</sub>]</code> 。</p>
|
||||
|
||||
<p>你按照如下规则进行一场游戏:</p>
|
||||
|
||||
<ul>
|
||||
<li>你从第 <code><strong>0</strong></code><strong> </strong>天开始吃糖果。</li>
|
||||
<li>你在吃完 <strong>所有</strong> 第 <code>i - 1</code> 类糖果之前,<strong>不能</strong> 吃任何一颗第 <code>i</code> 类糖果。</li>
|
||||
<li>在吃完所有糖果之前,你必须每天 <strong>至少</strong> 吃 <strong>一颗</strong> 糖果。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你构建一个布尔型数组 <code>answer</code> ,用以给出 <code>queries</code> 中每一项的对应答案。此数组满足:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>answer.length == queries.length</code> 。<code>answer[i]</code> 是 <code>queries[i]</code> 的答案。</li>
|
||||
<li><code>answer[i]</code> 为 <code>true</code> 的条件是:在每天吃 <strong>不超过</strong> <code>dailyCap<sub>i</sub></code><sub> </sub>颗糖果的前提下,你可以在第 <code>favoriteDay<sub>i</sub></code> 天吃到第 <code>favoriteType<sub>i</sub></code> 类糖果;否则 <code>answer[i]</code> 为 <code>false</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>注意,只要满足上面 3 条规则中的第二条规则,你就可以在同一天吃不同类型的糖果。</p>
|
||||
|
||||
<p>请你返回得到的数组<em> </em><code>answer</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>candiesCount = [7,4,5,3,8], queries = [[0,2,2],[4,2,4],[2,13,1000000000]]
|
||||
<b>输出:</b>[true,false,true]
|
||||
<strong>提示:</strong>
|
||||
1- 在第 0 天吃 2 颗糖果(类型 0),第 1 天吃 2 颗糖果(类型 0),第 2 天你可以吃到类型 0 的糖果。
|
||||
2- 每天你最多吃 4 颗糖果。即使第 0 天吃 4 颗糖果(类型 0),第 1 天吃 4 颗糖果(类型 0 和类型 1),你也没办法在第 2 天吃到类型 4 的糖果。换言之,你没法在每天吃 4 颗糖果的限制下在第 2 天吃到第 4 类糖果。
|
||||
3- 如果你每天吃 1 颗糖果,你可以在第 13 天吃到类型 2 的糖果。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>candiesCount = [5,2,6,4,1], queries = [[3,1,2],[4,10,3],[3,10,100],[4,100,30],[1,3,1]]
|
||||
<b>输出:</b>[false,true,true,false,false]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= candiesCount.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= candiesCount[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[i].length == 3</code></li>
|
||||
<li><code>0 <= favoriteType<sub>i</sub> < candiesCount.length</code></li>
|
||||
<li><code>0 <= favoriteDay<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= dailyCap<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>给你一个长度为 <code>n</code> 的整数数组 <code>coins</code> ,它代表你拥有的 <code>n</code> 个硬币。第 <code>i</code> 个硬币的值为 <code>coins[i]</code> 。如果你从这些硬币中选出一部分硬币,它们的和为 <code>x</code> ,那么称,你可以 <strong>构造</strong> 出 <code>x</code> 。</p>
|
||||
|
||||
<p>请返回从 <code>0</code> 开始(<strong>包括</strong> <code>0</code> ),你最多能 <strong>构造</strong> 出多少个连续整数。</p>
|
||||
|
||||
<p>你可能有多个相同值的硬币。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>coins = [1,3]
|
||||
<b>输出:</b>2
|
||||
<strong>解释:</strong>你可以得到以下这些值:
|
||||
- 0:什么都不取 []
|
||||
- 1:取 [1]
|
||||
从 0 开始,你可以构造出 2 个连续整数。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>coins = [1,1,1,4]
|
||||
<b>输出:</b>8
|
||||
<strong>解释:</strong>你可以得到以下这些值:
|
||||
- 0:什么都不取 []
|
||||
- 1:取 [1]
|
||||
- 2:取 [1,1]
|
||||
- 3:取 [1,1,1]
|
||||
- 4:取 [4]
|
||||
- 5:取 [4,1]
|
||||
- 6:取 [4,1,1]
|
||||
- 7:取 [4,1,1,1]
|
||||
从 0 开始,你可以构造出 8 个连续整数。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,4,10,3,1]
|
||||
<b>输出:</b>20</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>coins.length == n</code></li>
|
||||
<li><code>1 <= n <= 4 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= coins[i] <= 4 * 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>给你一个二进制字符串 <code>s</code> 。你可以按任意顺序执行以下两种操作任意次:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>类型 1 :删除</strong> 字符串 <code>s</code> 的第一个字符并将它 <strong>添加</strong> 到字符串结尾。</li>
|
||||
<li><strong>类型 2 :选择 </strong>字符串 <code>s</code> 中任意一个字符并将该字符 <strong>反转 </strong>,也就是如果值为 <code>'0'</code> ,则反转得到 <code>'1'</code> ,反之亦然。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回使 <code>s</code> 变成 <strong>交替</strong> 字符串的前提下, <strong>类型 2 </strong>的 <strong>最少</strong> 操作次数 。</p>
|
||||
|
||||
<p>我们称一个字符串是 <strong>交替</strong> 的,需要满足任意相邻字符都不同。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,字符串 <code>"010"</code> 和 <code>"1010"</code> 都是交替的,但是字符串 <code>"0100"</code> 不是。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "111000"
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>执行第一种操作两次,得到 s = "100011" 。
|
||||
然后对第三个和第六个字符执行第二种操作,得到 s = "10<strong>1</strong>01<strong>0</strong>" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "010"
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>字符串已经是交替的。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "1110"
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>对第二个字符执行第二种操作,得到 s = "1<strong>0</strong>10" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> 要么是 <code>'0'</code> ,要么是 <code>'1'</code> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给你一个字符串 <code>s</code> ,它仅包含字符 <code>'a'</code> 和 <code>'b'</code> 。</p>
|
||||
|
||||
<p>你可以删除 <code>s</code> 中任意数目的字符,使得 <code>s</code> <strong>平衡</strong> 。我们称 <code>s</code> <strong>平衡的</strong> 当不存在下标对 <code>(i,j)</code> 满足 <code>i < j</code> 且 <code>s[i] = 'b'</code> 同时 <code>s[j]= 'a'</code> 。</p>
|
||||
|
||||
<p>请你返回使 <code>s</code> <strong>平衡</strong> 的 <strong>最少</strong> 删除次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "aababbab"
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>你可以选择以下任意一种方案:
|
||||
下标从 0 开始,删除第 2 和第 6 个字符("aa<strong>b</strong>abb<strong>a</strong>b" -> "aaabbb"),
|
||||
下标从 0 开始,删除第 3 和第 6 个字符("aab<strong>a</strong>bb<strong>a</strong>b" -> "aabbbb")。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "bbaaaaabb"
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>唯一的最优解是删除最前面两个字符。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> 要么是 <code>'a'</code> 要么是 <code>'b'</code><strong> </strong>。</li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>给你一个字符串 <code>s</code> (<strong>下标从 0 开始</strong>)。你需要对 <code>s</code> 执行以下操作直到它变为一个有序字符串:</p>
|
||||
|
||||
<ol>
|
||||
<li>找到 <strong>最大下标</strong> <code>i</code> ,使得 <code>1 <= i < s.length</code> 且 <code>s[i] < s[i - 1]</code> 。</li>
|
||||
<li>找到 <strong>最大下标</strong> <code>j</code> ,使得 <code>i <= j < s.length</code> 且对于所有在闭区间 <code>[i, j]</code> 之间的 <code>k</code> 都有 <code>s[k] < s[i - 1]</code> 。</li>
|
||||
<li>交换下标为 <code>i - 1</code> 和 <code>j</code> 处的两个字符。</li>
|
||||
<li>将下标 <code>i</code> 开始的字符串后缀反转。</li>
|
||||
</ol>
|
||||
|
||||
<p>请你返回将字符串变成有序的最少操作次数。由于答案可能会很大,请返回它对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 的结果。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "cba"
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>模拟过程如下所示:
|
||||
操作 1:i=2,j=2。交换 s[1] 和 s[2] 得到 s="cab" ,然后反转下标从 2 开始的后缀字符串,得到 s="cab" 。
|
||||
操作 2:i=1,j=2。交换 s[0] 和 s[2] 得到 s="bac" ,然后反转下标从 1 开始的后缀字符串,得到 s="bca" 。
|
||||
操作 3:i=2,j=2。交换 s[1] 和 s[2] 得到 s="bac" ,然后反转下标从 2 开始的后缀字符串,得到 s="bac" 。
|
||||
操作 4:i=1,j=1。交换 s[0] 和 s[1] 得到 s="abc" ,然后反转下标从 1 开始的后缀字符串,得到 s="acb" 。
|
||||
操作 5:i=2,j=2。交换 s[1] 和 s[2] 得到 s="abc" ,然后反转下标从 2 开始的后缀字符串,得到 s="abc" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "aabaa"
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>模拟过程如下所示:
|
||||
操作 1:i=3,j=4。交换 s[2] 和 s[4] 得到 s="aaaab" ,然后反转下标从 3 开始的后缀字符串,得到 s="aaaba" 。
|
||||
操作 2:i=4,j=4。交换 s[3] 和 s[4] 得到 s="aaaab" ,然后反转下标从 4 开始的后缀字符串,得到 s="aaaab" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "cdbea"
|
||||
<b>输出:</b>63</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "leetcodeleetcodeleetcode"
|
||||
<b>输出:</b>982157772
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 3000</code></li>
|
||||
<li><code>s</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> 。区间 <code>[left, right]</code>(<code>left <= right</code>)的 <strong>异或结果</strong> 是对下标位于 <code>left</code> 和 <code>right</code>(包括 <code>left</code> 和 <code>right</code> )之间所有元素进行 <code>XOR</code> 运算的结果:<code>nums[left] XOR nums[left+1] XOR ... XOR nums[right]</code> 。</p>
|
||||
|
||||
<p>返回数组中 <strong>要更改的最小元素数</strong> ,以使所有长度为 <code>k</code> 的区间异或结果等于零。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,0,3,0], k = 1
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>将数组 [<strong>1</strong>,<strong>2</strong>,0,<strong>3</strong>,0] 修改为 [<strong>0</strong>,<strong>0</strong>,0,<strong>0</strong>,0]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,4,5,2,1,7,3,4,7], k = 3
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>将数组 [3,4,<strong>5</strong>,<strong>2</strong>,<strong>1</strong>,7,3,4,7] 修改为 [3,4,<strong>7</strong>,<strong>3</strong>,<strong>4</strong>,7,3,4,7]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,4,1,2,5,1,2,6], k = 3
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>将数组[1,2,<strong>4,</strong>1,2,<strong>5</strong>,1,2,<strong>6</strong>] 修改为 [1,2,<strong>3</strong>,1,2,<strong>3</strong>,1,2,<strong>3</strong>]</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= nums.length <= 2000</code></li>
|
||||
<li><code>0 <= nums[i] < 2<sup>10</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>存在一个长度为 <code>n</code> 的数组 <code>arr</code> ,其中 <code>arr[i] = (2 * i) + 1</code> ( <code>0 <= i < n</code> )。</p>
|
||||
|
||||
<p>一次操作中,你可以选出两个下标,记作 <code>x</code> 和 <code>y</code> ( <code>0 <= x, y < n</code> )并使 <code>arr[x]</code> 减去 <code>1</code> 、<code>arr[y]</code> 加上 <code>1</code> (即 <code>arr[x] -=1 </code>且 <code>arr[y] += 1</code> )。最终的目标是使数组中的所有元素都 <strong>相等</strong> 。题目测试用例将会 <strong>保证</strong> :在执行若干步操作后,数组中的所有元素最终可以全部相等。</p>
|
||||
|
||||
<p>给你一个整数 <code>n</code>,即数组的长度。请你返回使数组 <code>arr</code> 中所有元素相等所需的 <strong>最小操作数</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 3
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>arr = [1, 3, 5]
|
||||
第一次操作选出 x = 2 和 y = 0,使数组变为 [2, 3, 4]
|
||||
第二次操作继续选出 x = 2 和 y = 0,数组将会变成 [3, 3, 3]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 6
|
||||
<strong>输出:</strong>9
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10^4</code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>给你一个长度为<strong> 偶数</strong> <code>n</code> 的整数数组 <code>nums</code> 和一个整数 <code>limit</code> 。每一次操作,你可以将 <code>nums</code> 中的任何整数替换为 <code>1</code> 到 <code>limit</code> 之间的另一个整数。</p>
|
||||
|
||||
<p>如果对于所有下标 <code>i</code>(<strong>下标从 </strong><code>0</code><strong> 开始</strong>),<code>nums[i] + nums[n - 1 - i]</code> 都等于同一个数,则数组 <code>nums</code> 是 <strong>互补的</strong> 。例如,数组 <code>[1,2,3,4]</code> 是互补的,因为对于所有下标 <code>i</code> ,<code>nums[i] + nums[n - 1 - i] = 5</code> 。</p>
|
||||
|
||||
<p>返回使数组 <strong>互补</strong> 的 <strong>最少</strong> 操作次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,4,3], limit = 4
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>经过 1 次操作,你可以将数组 nums 变成 [1,2,<strong>2</strong>,3](加粗元素是变更的数字):
|
||||
nums[0] + nums[3] = 1 + 3 = 4.
|
||||
nums[1] + nums[2] = 2 + 2 = 4.
|
||||
nums[2] + nums[1] = 2 + 2 = 4.
|
||||
nums[3] + nums[0] = 3 + 1 = 4.
|
||||
对于每个 i ,nums[i] + nums[n-1-i] = 4 ,所以 nums 是互补的。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,2,1], limit = 2
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>经过 2 次操作,你可以将数组 nums 变成 [<strong>2</strong>,2,2,<strong>2</strong>] 。你不能将任何数字变更为 3 ,因为 3 > limit 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,1,2], limit = 2
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>nums 已经是互补的。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= limit <= 10<sup>5</sup></code></li>
|
||||
<li><code>n</code> 是偶数。</li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>给你一个整数数组 <code>nums</code> ,你的目标是令 <code>nums</code> 中的所有元素相等。完成一次减少操作需要遵照下面的几个步骤:</p>
|
||||
|
||||
<ol>
|
||||
<li>找出 <code>nums</code> 中的 <strong>最大</strong> 值。记这个值为 <code>largest</code> 并取其下标 <code>i</code> (<strong>下标从 0 开始计数</strong>)。如果有多个元素都是最大值,则取最小的 <code>i</code> 。</li>
|
||||
<li>找出 <code>nums</code> 中的 <strong>下一个最大</strong> 值,这个值 <strong>严格小于</strong> <code>largest</code> ,记为 <code>nextLargest</code> 。</li>
|
||||
<li>将 <code>nums[i]</code> 减少到 <code>nextLargest</code> 。</li>
|
||||
</ol>
|
||||
|
||||
<p>返回使<em> </em><code>nums</code><em> </em>中的所有元素相等的操作次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [5,1,3]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>需要 3 次操作使 nums 中的所有元素相等:
|
||||
1. largest = 5 下标为 0 。nextLargest = 3 。将 nums[0] 减少到 3 。nums = [<strong>3</strong>,1,3] 。
|
||||
2. largest = 3 下标为 0 。nextLargest = 1 。将 nums[0] 减少到 1 。nums = [<strong>1</strong>,1,3] 。
|
||||
3. largest = 3 下标为 2 。nextLargest = 1 。将 nums[2] 减少到 1 。nums = [<strong>1</strong>,1,<strong>1</strong>] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,1]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>nums 中的所有元素已经是相等的。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,2,2,3]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>需要 4 次操作使 nums 中的所有元素相等:
|
||||
1. largest = 3 下标为 4 。nextLargest = 2 。将 nums[4] 减少到 2 。nums = [1,1,2,2,<strong>2</strong>] 。
|
||||
2. largest = 2 下标为 2 。nextLargest = 1 。将 nums[2] 减少到 1 。nums = [1,1,<strong>1</strong>,2,2] 。
|
||||
3. largest = 2 下标为 3 。nextLargest = 1 。将 nums[3] 减少到 1 。nums = [1,1,1,<strong>1</strong>,2] 。
|
||||
4. largest = 2 下标为 4 。nextLargest = 1 。将 nums[4] 减少到 1 。nums = [1,1,1,1,<strong>1</strong>] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 5 * 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>给你一个正整数数组 <code>nums</code>,请你移除 <strong>最短</strong> 子数组(可以为 <strong>空</strong>),使得剩余元素的 <strong>和</strong> 能被 <code>p</code> 整除。 <strong>不允许</strong> 将整个数组都移除。</p>
|
||||
|
||||
<p>请你返回你需要移除的最短子数组的长度,如果无法满足题目要求,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p><strong>子数组</strong> 定义为原数组中连续的一组元素。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [3,1,4,2], p = 6
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>nums 中元素和为 10,不能被 p 整除。我们可以移除子数组 [4] ,剩余元素的和为 6 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [6,3,5,2], p = 9
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>我们无法移除任何一个元素使得和被 9 整除,最优方案是移除子数组 [5,2] ,剩余元素为 [6,3],和为 9 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [1,2,3], p = 3
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>和恰好为 6 ,已经能被 3 整除了。所以我们不需要移除任何元素。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [1,2,3], p = 7
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>没有任何方案使得移除子数组后剩余元素的和被 7 整除。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [1000000000,1000000000,1000000000], p = 3
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= p <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>给你一个整数 <code>n</code>,你需要重复执行多次下述操作将其转换为 <code>0</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li>翻转 <code>n</code> 的二进制表示中最右侧位(第 <code>0</code> 位)。</li>
|
||||
<li>如果第 <code>(i-1)</code> 位为 <code>1</code> 且从第 <code>(i-2)</code> 位到第 <code>0</code> 位都为 <code>0</code>,则翻转 <code>n</code> 的二进制表示中的第 <code>i</code> 位。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回将 <code>n</code> 转换为 <code>0</code> 的最小操作次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>3 的二进制表示为 "11"
|
||||
"<strong>1</strong>1" -> "<strong>0</strong>1" ,执行的是第 2 种操作,因为第 0 位为 1 。
|
||||
"0<strong>1</strong>" -> "0<strong>0</strong>" ,执行的是第 1 种操作。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 6
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>6 的二进制表示为 "110".
|
||||
"<strong>1</strong>10" -> "<strong>0</strong>10" ,执行的是第 2 种操作,因为第 1 位为 1 ,第 0 到 0 位为 0 。
|
||||
"01<strong>0</strong>" -> "01<strong>1</strong>" ,执行的是第 1 种操作。
|
||||
"0<strong>1</strong>1" -> "0<strong>0</strong>1" ,执行的是第 2 种操作,因为第 0 位为 1 。
|
||||
"00<strong>1</strong>" -> "00<strong>0</strong>" ,执行的是第 1 种操作。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>给你两个 <strong>下标从 0 开始</strong> 的整数数组 <code>servers</code> 和 <code>tasks</code> ,长度分别为 <code>n</code> 和 <code>m</code> 。<code>servers[i]</code> 是第 <code>i<sup></sup></code> 台服务器的 <strong>权重</strong> ,而 <code>tasks[j]</code> 是处理第 <code>j<sup></sup></code> 项任务 <strong>所需要的时间</strong>(单位:秒)。</p>
|
||||
|
||||
<p>你正在运行一个仿真系统,在处理完所有任务后,该系统将会关闭。每台服务器只能同时处理一项任务。第 <code>0</code> 项任务在第 <code>0</code> 秒可以开始处理,相应地,第 <code>j</code> 项任务在第 <code>j</code> 秒可以开始处理。处理第 <code>j</code> 项任务时,你需要为它分配一台 <strong>权重最小</strong> 的空闲服务器。如果存在多台相同权重的空闲服务器,请选择 <strong>下标最小</strong> 的服务器。如果一台空闲服务器在第 <code>t</code> 秒分配到第 <code>j</code> 项任务,那么在 <code>t + tasks[j]</code> 时它将恢复空闲状态。</p>
|
||||
|
||||
<p>如果没有空闲服务器,则必须等待,直到出现一台空闲服务器,并 <strong>尽可能早</strong> 地处理剩余任务。 如果有多项任务等待分配,则按照 <strong>下标递增</strong> 的顺序完成分配。</p>
|
||||
|
||||
<p>如果同一时刻存在多台空闲服务器,可以同时将多项任务分别分配给它们。</p>
|
||||
|
||||
<p>构建长度为 <code>m</code> 的答案数组 <code>ans</code> ,其中 <code>ans[j]</code> 是第 <code>j</code> 项任务分配的服务器的下标。</p>
|
||||
|
||||
<p>返回答案数组<em> </em><code>ans</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>servers = [3,3,2], tasks = [1,2,3,2,1,2]
|
||||
<strong>输出:</strong>[2,2,0,2,1,2]
|
||||
<strong>解释:</strong>事件按时间顺序如下:
|
||||
- 0 秒时,第 0 项任务加入到任务队列,使用第 2 台服务器处理到 1 秒。
|
||||
- 1 秒时,第 2 台服务器空闲,第 1 项任务加入到任务队列,使用第 2 台服务器处理到 3 秒。
|
||||
- 2 秒时,第 2 项任务加入到任务队列,使用第 0 台服务器处理到 5 秒。
|
||||
- 3 秒时,第 2 台服务器空闲,第 3 项任务加入到任务队列,使用第 2 台服务器处理到 5 秒。
|
||||
- 4 秒时,第 4 项任务加入到任务队列,使用第 1 台服务器处理到 5 秒。
|
||||
- 5 秒时,所有服务器都空闲,第 5 项任务加入到任务队列,使用第 2 台服务器处理到 7 秒。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>servers = [5,1,4,3,2], tasks = [2,1,2,4,5,2,1]
|
||||
<strong>输出:</strong>[1,4,1,4,1,3,2]
|
||||
<strong>解释:</strong>事件按时间顺序如下:
|
||||
- 0 秒时,第 0 项任务加入到任务队列,使用第 1 台服务器处理到 2 秒。
|
||||
- 1 秒时,第 1 项任务加入到任务队列,使用第 4 台服务器处理到 2 秒。
|
||||
- 2 秒时,第 1 台和第 4 台服务器空闲,第 2 项任务加入到任务队列,使用第 1 台服务器处理到 4 秒。
|
||||
- 3 秒时,第 3 项任务加入到任务队列,使用第 4 台服务器处理到 7 秒。
|
||||
- 4 秒时,第 1 台服务器空闲,第 4 项任务加入到任务队列,使用第 1 台服务器处理到 9 秒。
|
||||
- 5 秒时,第 5 项任务加入到任务队列,使用第 3 台服务器处理到 7 秒。
|
||||
- 6 秒时,第 6 项任务加入到任务队列,使用第 2 台服务器处理到 7 秒。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>servers.length == n</code></li>
|
||||
<li><code>tasks.length == m</code></li>
|
||||
<li><code>1 <= n, m <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= servers[i], tasks[j] <= 2 * 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>Alice 把 <code>n</code> 个气球排列在一根绳子上。给你一个下标从 <strong>0</strong> 开始的字符串 <code>colors</code> ,其中 <code>colors[i]</code> 是第 <code>i</code> 个气球的颜色。</p>
|
||||
|
||||
<p>Alice 想要把绳子装扮成 <strong>彩色</strong> ,且她不希望两个连续的气球涂着相同的颜色,所以她喊来 Bob 帮忙。Bob 可以从绳子上移除一些气球使绳子变成 <strong>彩色</strong> 。给你一个下标从 <strong>0</strong> 开始的整数数组 <code>neededTime</code> ,其中 <code>neededTime[i]</code> 是 Bob 从绳子上移除第 <code>i</code> 个气球需要的时间(以秒为单位)。</p>
|
||||
|
||||
<p>返回 Bob 使绳子变成 <strong>彩色</strong> 需要的 <strong>最少时间</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/ballon1.jpg" style="width: 404px; height: 243px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>colors = "abaac", neededTime = [1,2,3,4,5]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>在上图中,'a' 是蓝色,'b' 是红色且 'c' 是绿色。
|
||||
Bob 可以移除下标 2 的蓝色气球。这将花费 3 秒。
|
||||
移除后,不存在两个连续的气球涂着相同的颜色。总时间 = 3 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/balloon2.jpg" style="width: 244px; height: 243px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>colors = "abc", neededTime = [1,2,3]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>绳子已经是彩色的,Bob 不需要从绳子上移除任何气球。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/balloon3.jpg" style="width: 404px; height: 243px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>colors = "aabaa", neededTime = [1,2,3,4,1]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>Bob 会移除下标 0 和下标 4 处的气球。这两个气球各需要 1 秒来移除。
|
||||
移除后,不存在两个连续的气球涂着相同的颜色。总时间 = 1 + 1 = 2 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == colors.length == neededTime.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= neededTime[i] <= 10<sup>4</sup></code></li>
|
||||
<li><code>colors</code> 仅由小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,59 @@
|
||||
<p>给你一个由若干 <code>0</code> 和 <code>1</code> 组成的二维网格 <code>grid</code> ,其中 <code>0</code> 表示水,而 <code>1</code> 表示陆地。岛屿由水平方向或竖直方向上相邻的 <code>1</code> (陆地)连接形成。</p>
|
||||
|
||||
<p>如果 <strong>恰好只有一座岛屿 </strong>,则认为陆地是 <strong>连通的</strong> ;否则,陆地就是 <strong>分离的</strong> 。</p>
|
||||
|
||||
<p>一天内,可以将任何单个陆地单元(<code>1</code>)更改为水单元(<code>0</code>)。</p>
|
||||
|
||||
<p>返回使陆地分离的最少天数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/30/1926_island.png" style="height: 139px; width: 498px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>至少需要 2 天才能得到分离的陆地。
|
||||
将陆地 grid[1][1] 和 grid[0][2] 更改为水,得到两个分离的岛屿。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>grid = [[1,1]]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>如果网格中都是水,也认为是分离的 ([[1,1]] -> [[0,0]]),0 岛屿。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>grid = [[1,0,1,0]]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>grid = [[1,1,0,1,1],
|
||||
[1,1,1,1,1],
|
||||
[1,1,0,1,1],
|
||||
[1,1,0,1,1]]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>grid = [[1,1,0,1,1],
|
||||
[1,1,1,1,1],
|
||||
[1,1,0,1,1],
|
||||
[1,1,1,1,1]]
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= grid.length, grid[i].length <= 30</code></li>
|
||||
<li><code>grid[i][j]</code> 为 <code>0</code> 或 <code>1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>Alice 和 Bob 共有一个无向图,其中包含 n 个节点和 3 种类型的边:</p>
|
||||
|
||||
<ul>
|
||||
<li>类型 1:只能由 Alice 遍历。</li>
|
||||
<li>类型 2:只能由 Bob 遍历。</li>
|
||||
<li>类型 3:Alice 和 Bob 都可以遍历。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个数组 <code>edges</code> ,其中 <code>edges[i] = [type<sub>i</sub>, u<sub>i</sub>, v<sub>i</sub>]</code> 表示节点 <code>u<sub>i</sub></code> 和 <code>v<sub>i</sub></code> 之间存在类型为 <code>type<sub>i</sub></code> 的双向边。请你在保证图仍能够被 Alice和 Bob 完全遍历的前提下,找出可以删除的最大边数。如果从任何节点开始,Alice 和 Bob 都可以到达所有其他节点,则认为图是可以完全遍历的。</p>
|
||||
|
||||
<p>返回可以删除的最大边数,如果 Alice 和 Bob 无法完全遍历图,则返回 -1 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/09/06/5510ex1.png" style="height: 191px; width: 179px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>如果删除<strong> </strong>[1,1,2] 和 [1,1,3] 这两条边,Alice 和 Bob 仍然可以完全遍历这个图。再删除任何其他的边都无法保证图可以完全遍历。所以可以删除的最大边数是 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/09/06/5510ex2.png" style="height: 190px; width: 178px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>注意,删除任何一条边都会使 Alice 和 Bob 无法完全遍历这个图。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/09/06/5510ex3.png" style="height: 190px; width: 178px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>在当前图中,Alice 无法从其他节点到达节点 4 。类似地,Bob 也不能达到节点 1 。因此,图无法完全遍历。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10^5</code></li>
|
||||
<li><code>1 <= edges.length <= min(10^5, 3 * n * (n-1) / 2)</code></li>
|
||||
<li><code>edges[i].length == 3</code></li>
|
||||
<li><code>1 <= edges[i][0] <= 3</code></li>
|
||||
<li><code>1 <= edges[i][1] < edges[i][2] <= n</code></li>
|
||||
<li>所有元组 <code>(type<sub>i</sub>, u<sub>i</sub>, v<sub>i</sub>)</code> 互不相同</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>表: <code>Users</code></p>
|
||||
|
||||
<pre>
|
||||
+----------------+---------+
|
||||
| Column Name | Type |
|
||||
+----------------+---------+
|
||||
| user_id | int |
|
||||
| name | varchar |
|
||||
+----------------+---------+
|
||||
user_id 是该表的主键。
|
||||
该表包含用户的 ID 和名字。名字仅由小写和大写字符组成。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写一个 SQL 查询来修复名字,使得只有第一个字符是大写的,其余都是小写的。</p>
|
||||
|
||||
<p>返回按 <code>user_id</code> 排序的结果表。</p>
|
||||
|
||||
<p>查询结果格式示例如下。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Users table:
|
||||
+---------+-------+
|
||||
| user_id | name |
|
||||
+---------+-------+
|
||||
| 1 | aLice |
|
||||
| 2 | bOB |
|
||||
+---------+-------+
|
||||
<strong>输出:</strong>
|
||||
+---------+-------+
|
||||
| user_id | name |
|
||||
+---------+-------+
|
||||
| 1 | Alice |
|
||||
| 2 | Bob |
|
||||
+---------+-------+</pre>
|
@@ -0,0 +1,49 @@
|
||||
<p>给你一个二进制字符串 <code>binary</code> ,它仅有 <code>0</code> 或者 <code>1</code> 组成。你可以使用下面的操作任意次对它进行修改:</p>
|
||||
|
||||
<ul>
|
||||
<li>操作 1 :如果二进制串包含子字符串 <code>"00"</code> ,你可以用 <code>"10"</code> 将其替换。
|
||||
|
||||
<ul>
|
||||
<li>比方说, <code>"<strong>00</strong>010" -> "<strong>10</strong>010"</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>操作 2 :如果二进制串包含子字符串 <code>"10"</code> ,你可以用 <code>"01"</code> 将其替换。
|
||||
<ul>
|
||||
<li>比方说, <code>"000<strong>10</strong>" -> "000<strong>01</strong>"</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回执行上述操作任意次以后能得到的 <strong>最大二进制字符串</strong> 。如果二进制字符串 <code>x</code> 对应的十进制数字大于二进制字符串 <code>y</code> 对应的十进制数字,那么我们称二进制字符串<em> </em><code>x</code><em> </em>大于二进制字符串<em> </em><code>y</code><em> </em>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>binary = "000110"
|
||||
<b>输出:</b>"111011"
|
||||
<b>解释:</b>一个可行的转换为:
|
||||
"0001<strong>10</strong>" -> "0001<strong>01</strong>"
|
||||
"<strong>00</strong>0101" -> "<strong>10</strong>0101"
|
||||
"1<strong>00</strong>101" -> "1<strong>10</strong>101"
|
||||
"110<strong>10</strong>1" -> "110<strong>01</strong>1"
|
||||
"11<strong>00</strong>11" -> "11<strong>10</strong>11"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>binary = "01"
|
||||
<b>输出:</b>"01"
|
||||
<b>解释:</b>"01" 没办法进行任何转换。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= binary.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>binary</code> 仅包含 <code>'0'</code> 和 <code>'1'</code> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p><strong>小写字符 </strong>的 <strong>数值</strong> 是它在字母表中的位置(从 <code>1</code> 开始),因此 <code>a</code> 的数值为 <code>1</code> ,<code>b</code> 的数值为 <code>2</code> ,<code>c</code> 的数值为 <code>3</code> ,以此类推。</p>
|
||||
|
||||
<p>字符串由若干小写字符组成,<strong>字符串的数值</strong> 为各字符的数值之和。例如,字符串 <code>"abe"</code> 的数值等于 <code>1 + 2 + 5 = 8</code> 。</p>
|
||||
|
||||
<p>给你两个整数 <code>n</code> 和 <code>k</code> 。返回 <strong>长度</strong> 等于 <code>n</code> 且 <strong>数值</strong> 等于 <code>k</code> 的 <strong>字典序最小</strong> 的字符串。</p>
|
||||
|
||||
<p>注意,如果字符串 <code>x</code> 在字典排序中位于 <code>y</code> 之前,就认为 <code>x</code> 字典序比 <code>y</code> 小,有以下两种情况:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>x</code> 是 <code>y</code> 的一个前缀;</li>
|
||||
<li>如果 <code>i</code> 是 <code>x[i] != y[i]</code> 的第一个位置,且 <code>x[i]</code> 在字母表中的位置比 <code>y[i]</code> 靠前。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3, k = 27
|
||||
<strong>输出:</strong>"aay"
|
||||
<strong>解释:</strong>字符串的数值为 1 + 1 + 25 = 27,它是数值满足要求且长度等于 3 字典序最小的字符串。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 5, k = 73
|
||||
<strong>输出:</strong>"aaszz"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>n <= k <= 26 * n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>给你一个浮点数 <code>hour</code> ,表示你到达办公室可用的总通勤时间。要到达办公室,你必须按给定次序乘坐 <code>n</code> 趟列车。另给你一个长度为 <code>n</code> 的整数数组 <code>dist</code> ,其中 <code>dist[i]</code> 表示第 <code>i</code> 趟列车的行驶距离(单位是千米)。</p>
|
||||
|
||||
<p>每趟列车均只能在整点发车,所以你可能需要在两趟列车之间等待一段时间。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,第 <code>1</code> 趟列车需要 <code>1.5</code> 小时,那你必须再等待 <code>0.5</code> 小时,搭乘在第 2 小时发车的第 <code>2</code> 趟列车。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回能满足你准时到达办公室所要求全部列车的<strong> 最小正整数 </strong>时速(单位:千米每小时),如果无法准时到达,则返回 <code>-1</code> 。</p>
|
||||
|
||||
<p>生成的测试用例保证答案不超过 <code>10<sup>7</sup></code> ,且 <code>hour</code> 的 <strong>小数点后最多存在两位数字</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>dist = [1,3,2], hour = 6
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>速度为 1 时:
|
||||
- 第 1 趟列车运行需要 1/1 = 1 小时。
|
||||
- 由于是在整数时间到达,可以立即换乘在第 1 小时发车的列车。第 2 趟列车运行需要 3/1 = 3 小时。
|
||||
- 由于是在整数时间到达,可以立即换乘在第 4 小时发车的列车。第 3 趟列车运行需要 2/1 = 2 小时。
|
||||
- 你将会恰好在第 6 小时到达。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>dist = [1,3,2], hour = 2.7
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>速度为 3 时:
|
||||
- 第 1 趟列车运行需要 1/3 = 0.33333 小时。
|
||||
- 由于不是在整数时间到达,故需要等待至第 1 小时才能搭乘列车。第 2 趟列车运行需要 3/3 = 1 小时。
|
||||
- 由于是在整数时间到达,可以立即换乘在第 2 小时发车的列车。第 3 趟列车运行需要 2/3 = 0.66667 小时。
|
||||
- 你将会在第 2.66667 小时到达。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>dist = [1,3,2], hour = 1.9
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>不可能准时到达,因为第 3 趟列车最早是在第 2 小时发车。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == dist.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= dist[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= hour <= 10<sup>9</sup></code></li>
|
||||
<li><code>hours</code> 中,小数点后最多存在两位数字</li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>给你一个整数 <code>hoursBefore</code> ,表示你要前往会议所剩下的可用小时数。要想成功抵达会议现场,你必须途经 <code>n</code> 条道路。道路的长度用一个长度为 <code>n</code> 的整数数组 <code>dist</code> 表示,其中 <code>dist[i]</code> 表示第 <code>i</code> 条道路的长度(单位:<strong>千米</strong>)。另给你一个整数 <code>speed</code> ,表示你在道路上前进的速度(单位:<strong>千米每小时</strong>)。</p>
|
||||
|
||||
<p>当你通过第 <code>i</code> 条路之后,就必须休息并等待,直到 <strong>下一个整数小时</strong> 才能开始继续通过下一条道路。注意:你不需要在通过最后一条道路后休息,因为那时你已经抵达会议现场。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,如果你通过一条道路用去 <code>1.4</code> 小时,那你必须停下来等待,到 <code>2</code> 小时才可以继续通过下一条道路。如果通过一条道路恰好用去 <code>2</code> 小时,就无需等待,可以直接继续。</li>
|
||||
</ul>
|
||||
|
||||
<p>然而,为了能准时到达,你可以选择 <strong>跳过</strong> 一些路的休息时间,这意味着你不必等待下一个整数小时。注意,这意味着与不跳过任何休息时间相比,你可能在不同时刻到达接下来的道路。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,假设通过第 <code>1</code> 条道路用去 <code>1.4</code> 小时,且通过第 <code>2</code> 条道路用去 <code>0.6</code> 小时。跳过第 <code>1</code> 条道路的休息时间意味着你将会在恰好 <code>2</code> 小时完成通过第 <code>2</code> 条道路,且你能够立即开始通过第 <code>3</code> 条道路。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回准时抵达会议现场所需要的 <strong>最小跳过次数</strong> ,如果 <strong>无法准时参会</strong> ,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>dist = [1,3,2], speed = 4, hoursBefore = 2
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>
|
||||
不跳过任何休息时间,你将用 (1/4 + 3/4) + (3/4 + 1/4) + (2/4) = 2.5 小时才能抵达会议现场。
|
||||
可以跳过第 1 次休息时间,共用 ((1/4 + <strong>0</strong>) + (3/4 + 0)) + (2/4) = 1.5 小时抵达会议现场。
|
||||
注意,第 2 次休息时间缩短为 0 ,由于跳过第 1 次休息时间,你是在整数小时处完成通过第 2 条道路。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>dist = [7,3,5,5], speed = 2, hoursBefore = 10
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
不跳过任何休息时间,你将用 (7/2 + 1/2) + (3/2 + 1/2) + (5/2 + 1/2) + (5/2) = 11.5 小时才能抵达会议现场。
|
||||
可以跳过第 1 次和第 3 次休息时间,共用 ((7/2 + <strong>0</strong>) + (3/2 + 0)) + ((5/2 + <strong>0</strong>) + (5/2)) = 10 小时抵达会议现场。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>dist = [7,3,5,5], speed = 1, hoursBefore = 10
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>即使跳过所有的休息时间,也无法准时参加会议。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == dist.length</code></li>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
<li><code>1 <= dist[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= speed <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= hoursBefore <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>给你一个正整数数组 <code>arr</code> 。请你对 <code>arr</code> 执行一些操作(也可以不进行任何操作),使得数组满足以下条件:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>arr</code> 中 <strong>第一个</strong> 元素必须为 <code>1</code> 。</li>
|
||||
<li>任意相邻两个元素的差的绝对值 <strong>小于等于</strong> <code>1</code> ,也就是说,对于任意的 <code>1 <= i < arr.length</code> (<strong>数组下标从 0 开始</strong>),都满足 <code>abs(arr[i] - arr[i - 1]) <= 1</code> 。<code>abs(x)</code> 为 <code>x</code> 的绝对值。</li>
|
||||
</ul>
|
||||
|
||||
<p>你可以执行以下 2 种操作任意次:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>减小</strong> <code>arr</code> 中任意元素的值,使其变为一个 <strong>更小的正整数</strong> 。</li>
|
||||
<li><strong>重新排列</strong> <code>arr</code> 中的元素,你可以以任意顺序重新排列。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回执行以上操作后,在满足前文所述的条件下,<code>arr</code> 中可能的 <strong>最大值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>arr = [2,2,1,2,1]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>
|
||||
我们可以重新排列 arr 得到 <code>[1,2,2,2,1] ,该数组满足所有条件。</code>
|
||||
arr 中最大元素为 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>arr = [100,1,1000]
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>
|
||||
一个可行的方案如下:
|
||||
1. 重新排列 <code>arr</code> 得到 <code>[1,100,1000] 。</code>
|
||||
2. 将第二个元素减小为 2 。
|
||||
3. 将第三个元素减小为 3 。
|
||||
现在 <code>arr = [1,2,3] ,满足所有条件。</code>
|
||||
arr 中最大元素为 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>arr = [1,2,3,4,5]
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>数组已经满足所有条件,最大元素为 5 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>给你两个字符串 <code>a</code> 和 <code>b</code> ,它们长度相同。请你选择一个下标,将两个字符串都在 <strong>相同的下标 </strong>分割开。由 <code>a</code> 可以得到两个字符串: <code>a<sub>prefix</sub></code> 和 <code>a<sub>suffix</sub></code> ,满足 <code>a = a<sub>prefix</sub> + a<sub>suffix</sub></code><sub> </sub>,同理,由 <code>b</code> 可以得到两个字符串 <code>b<sub>prefix</sub></code> 和 <code>b<sub>suffix</sub></code> ,满足 <code>b = b<sub>prefix</sub> + b<sub>suffix</sub></code> 。请你判断 <code>a<sub>prefix</sub> + b<sub>suffix</sub></code> 或者 <code>b<sub>prefix</sub> + a<sub>suffix</sub></code> 能否构成回文串。</p>
|
||||
|
||||
<p>当你将一个字符串 <code>s</code> 分割成 <code>s<sub>prefix</sub></code> 和 <code>s<sub>suffix</sub></code> 时, <code>s<sub>suffix</sub></code> 或者 <code>s<sub>prefix</sub></code> 可以为空。比方说, <code>s = "abc"</code> 那么 <code>"" + "abc"</code> , <code>"a" + "bc" </code>, <code>"ab" + "c"</code> 和 <code>"abc" + ""</code> 都是合法分割。</p>
|
||||
|
||||
<p>如果 <strong>能构成回文字符串</strong> ,那么请返回 <code>true</code>,否则返回<em> </em><code>false</code> 。</p>
|
||||
|
||||
<p><strong>注意</strong>, <code>x + y</code> 表示连接字符串 <code>x</code> 和 <code>y</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>a = "x", b = "y"
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>如果 a 或者 b 是回文串,那么答案一定为 true ,因为你可以如下分割:
|
||||
a<sub>prefix</sub> = "", a<sub>suffix</sub> = "x"
|
||||
b<sub>prefix</sub> = "", b<sub>suffix</sub> = "y"
|
||||
那么 a<sub>prefix</sub> + b<sub>suffix</sub> = "" + "y" = "y" 是回文串。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>a = "abdef", b = "fecab"
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>a = "ulacfd", b = "jizalu"
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>在下标为 3 处分割:
|
||||
a<sub>prefix</sub> = "ula", a<sub>suffix</sub> = "cfd"
|
||||
b<sub>prefix</sub> = "jiz", b<sub>suffix</sub> = "alu"
|
||||
那么 a<sub>prefix</sub> + b<sub>suffix</sub> = "ula" + "alu" = "ulaalu" 是回文串。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= a.length, b.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>a.length == b.length</code></li>
|
||||
<li><code>a</code> 和 <code>b</code> 都只包含小写英文字母</li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>给你一个二进制串 <code>s</code> (一个只包含 0 和 1 的字符串),我们可以将 <code>s</code> 分割成 3 个 <strong>非空</strong> 字符串 s1, s2, s3 (s1 + s2 + s3 = s)。</p>
|
||||
|
||||
<p>请你返回分割 <code>s</code> 的方案数,满足 s1,s2 和 s3 中字符 '1' 的数目相同。</p>
|
||||
|
||||
<p>由于答案可能很大,请将它对 10^9 + 7 取余后返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "10101"
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>总共有 4 种方法将 s 分割成含有 '1' 数目相同的三个子字符串。
|
||||
"1|010|1"
|
||||
"1|01|01"
|
||||
"10|10|1"
|
||||
"10|1|01"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "1001"
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "0000"
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>总共有 3 种分割 s 的方法。
|
||||
"0|0|00"
|
||||
"0|00|0"
|
||||
"00|0|0"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "100100010100110"
|
||||
<strong>输出:</strong>12
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>s[i] == '0'</code> 或者 <code>s[i] == '1'</code></li>
|
||||
<li><code>3 <= s.length <= 10^5</code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code> ,这个数组中至多有 <code>50</code> 个不同的值。同时你有 <code>m</code> 个顾客的订单 <code>quantity</code> ,其中,整数 <code>quantity[i]</code> 是第 <code>i</code> 位顾客订单的数目。请你判断是否能将 <code>nums</code> 中的整数分配给这些顾客,且满足:</p>
|
||||
|
||||
<ul>
|
||||
<li>第 <code>i</code> 位顾客 <strong>恰好 </strong>有 <code>quantity[i]</code> 个整数。</li>
|
||||
<li>第 <code>i</code> 位顾客拿到的整数都是 <strong>相同的</strong> 。</li>
|
||||
<li>每位顾客都满足上述两个要求。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果你可以分配 <code>nums</code> 中的整数满足上面的要求,那么请返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,3,4], quantity = [2]
|
||||
<b>输出:</b>false
|
||||
<strong>解释:</strong>第 0 位顾客没办法得到两个相同的整数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,3,3], quantity = [2]
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>第 0 位顾客得到 [3,3] 。整数 [1,2] 都没有被使用。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,1,2,2], quantity = [2,2]
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>第 0 位顾客得到 [1,1] ,第 1 位顾客得到 [2,2] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
<li><code>m == quantity.length</code></li>
|
||||
<li><code>1 <= m <= 10</code></li>
|
||||
<li><code>1 <= quantity[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums</code> 中至多有 <code>50</code> 个不同的数字。</li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>有一根长度为 <code>n</code> 个单位的木棍,棍上从 <code>0</code> 到 <code>n</code> 标记了若干位置。例如,长度为 <strong>6</strong> 的棍子可以标记如下:</p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/09/statement.jpg" style="height: 111px; width: 521px;" /></p>
|
||||
|
||||
<p>给你一个整数数组 <code>cuts</code> ,其中 <code>cuts[i]</code> 表示你需要将棍子切开的位置。</p>
|
||||
|
||||
<p>你可以按顺序完成切割,也可以根据需要更改切割的顺序。</p>
|
||||
|
||||
<p>每次切割的成本都是当前要切割的棍子的长度,切棍子的总成本是历次切割成本的总和。对棍子进行切割将会把一根木棍分成两根较小的木棍(这两根木棍的长度和就是切割前木棍的长度)。请参阅第一个示例以获得更直观的解释。</p>
|
||||
|
||||
<p>返回切棍子的 <strong>最小总成本</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/09/e1.jpg" style="height: 284px; width: 350px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 7, cuts = [1,3,4,5]
|
||||
<strong>输出:</strong>16
|
||||
<strong>解释:</strong>按 [1, 3, 4, 5] 的顺序切割的情况如下所示:
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/09/e11.jpg" style="height: 284px; width: 350px;" />
|
||||
第一次切割长度为 7 的棍子,成本为 7 。第二次切割长度为 6 的棍子(即第一次切割得到的第二根棍子),第三次切割为长度 4 的棍子,最后切割长度为 3 的棍子。总成本为 7 + 6 + 4 + 3 = 20 。
|
||||
而将切割顺序重新排列为 [3, 5, 1, 4] 后,总成本 = 16(如示例图中 7 + 4 + 3 + 2 = 16)。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 9, cuts = [5,6,1,4,2]
|
||||
<strong>输出:</strong>22
|
||||
<strong>解释:</strong>如果按给定的顺序切割,则总成本为 25 。总成本 <= 25 的切割顺序很多,例如,[4, 6, 5, 2, 1] 的总成本 = 22,是所有可能方案中成本最小的。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10^6</code></li>
|
||||
<li><code>1 <= cuts.length <= min(n - 1, 100)</code></li>
|
||||
<li><code>1 <= cuts[i] <= n - 1</code></li>
|
||||
<li><code>cuts</code> 数组中的所有整数都 <strong>互不相同</strong></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,如果 <strong>恰好</strong> 删除 <strong>一个</strong> 元素后,数组 <strong>严格递增</strong> ,那么请你返回 <code>true</code> ,否则返回 <code>false</code> 。如果数组本身已经是严格递增的,请你也返回 <code>true</code> 。</p>
|
||||
|
||||
<p>数组 <code>nums</code> 是 <strong>严格递增</strong> 的定义为:对于任意下标的 <code>1 <= i < nums.length</code> 都满足 <code>nums[i - 1] < nums[i]</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,2,<strong>10</strong>,5,7]
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>从 nums 中删除下标 2 处的 10 ,得到 [1,2,5,7] 。
|
||||
[1,2,5,7] 是严格递增的,所以返回 true 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [2,3,1,2]
|
||||
<b>输出:</b>false
|
||||
<b>解释:</b>
|
||||
[3,1,2] 是删除下标 0 处元素后得到的结果。
|
||||
[2,1,2] 是删除下标 1 处元素后得到的结果。
|
||||
[2,3,2] 是删除下标 2 处元素后得到的结果。
|
||||
[2,3,1] 是删除下标 3 处元素后得到的结果。
|
||||
没有任何结果数组是严格递增的,所以返回 false 。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,1,1]
|
||||
<b>输出:</b>false
|
||||
<b>解释:</b>删除任意元素后的结果都是 [1,1] 。
|
||||
[1,1] 不是严格递增的,所以返回 false 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,2,3]
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>[1,2,3] 已经是严格递增的,所以返回 true 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>给你两个字符串 <code>s</code> 和 <code>part</code> ,请你对 <code>s</code> 反复执行以下操作直到 <b>所有</b> 子字符串 <code>part</code> 都被删除:</p>
|
||||
|
||||
<ul>
|
||||
<li>找到 <code>s</code> 中 <strong>最左边</strong> 的子字符串 <code>part</code> ,并将它从 <code>s</code> 中删除。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回从 <code>s</code> 中删除所有 <code>part</code> 子字符串以后得到的剩余字符串。</p>
|
||||
|
||||
<p>一个 <strong>子字符串</strong> 是一个字符串中连续的字符序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "daabcbaabcbc", part = "abc"
|
||||
<b>输出:</b>"dab"
|
||||
<b>解释:</b>以下操作按顺序执行:
|
||||
- s = "da<strong>abc</strong>baabcbc" ,删除下标从 2 开始的 "abc" ,得到 s = "dabaabcbc" 。
|
||||
- s = "daba<strong>abc</strong>bc" ,删除下标从 4 开始的 "abc" ,得到 s = "dababc" 。
|
||||
- s = "dab<strong>abc</strong>" ,删除下标从 3 开始的 "abc" ,得到 s = "dab" 。
|
||||
此时 s 中不再含有子字符串 "abc" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "axxxxyyyyb", part = "xy"
|
||||
<b>输出:</b>"ab"
|
||||
<b>解释:</b>以下操作按顺序执行:
|
||||
- s = "axxx<strong>xy</strong>yyyb" ,删除下标从 4 开始的 "xy" ,得到 s = "axxxyyyb" 。
|
||||
- s = "axx<strong>xy</strong>yyb" ,删除下标从 3 开始的 "xy" ,得到 s = "axxyyb" 。
|
||||
- s = "ax<strong>xy</strong>yb" ,删除下标从 2 开始的 "xy" ,得到 s = "axyb" 。
|
||||
- s = "a<strong>xy</strong>b" ,删除下标从 1 开始的 "xy" ,得到 s = "ab" 。
|
||||
此时 s 中不再含有子字符串 "xy" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>1 <= part.length <= 1000</code></li>
|
||||
<li><code>s</code> 和 <code>part</code> 只包小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>给你一个字符串 <code>s</code> 和两个整数 <code>x</code> 和 <code>y</code> 。你可以执行下面两种操作任意次。</p>
|
||||
|
||||
<ul>
|
||||
<li>删除子字符串 <code>"ab"</code> 并得到 <code>x</code> 分。
|
||||
|
||||
<ul>
|
||||
<li>比方说,从 <code>"c<strong>ab</strong>xbae"</code> 删除 <code>ab</code> ,得到 <code>"cxbae"</code> 。</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>删除子字符串<code>"ba"</code> 并得到 <code>y</code> 分。
|
||||
<ul>
|
||||
<li>比方说,从 <code>"cabx<strong>ba</strong>e"</code> 删除 <code>ba</code> ,得到 <code>"cabxe"</code> 。</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>请返回对 <code>s</code> 字符串执行上面操作若干次能得到的最大得分。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "cdbcbbaaabab", x = 4, y = 5
|
||||
<b>输出:</b>19
|
||||
<strong>解释:</strong>
|
||||
- 删除 "cdbcbbaaa<strong>ba</strong>b" 中加粗的 "ba" ,得到 s = "cdbcbbaaab" ,加 5 分。
|
||||
- 删除 "cdbcbbaa<strong>ab</strong>" 中加粗的 "ab" ,得到 s = "cdbcbbaa" ,加 4 分。
|
||||
- 删除 "cdbcb<strong>ba</strong>a" 中加粗的 "ba" ,得到 s = "cdbcba" ,加 5 分。
|
||||
- 删除 "cdbc<strong>ba</strong>" 中加粗的 "ba" ,得到 s = "cdbc" ,加 5 分。
|
||||
总得分为 5 + 4 + 5 + 5 = 19 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "aabbaaxybbaabb", x = 5, y = 4
|
||||
<b>输出:</b>20
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>给你一个正整数数组 <code>nums</code> ,请你从中删除一个含有 <strong>若干不同元素</strong> 的子数组<strong>。</strong>删除子数组的 <strong>得分</strong> 就是子数组各元素之 <strong>和</strong> 。</p>
|
||||
|
||||
<p>返回 <strong>只删除一个</strong> 子数组可获得的 <strong>最大得分</strong><em> 。</em></p>
|
||||
|
||||
<p>如果数组 <code>b</code> 是数组 <code>a</code> 的一个连续子序列,即如果它等于 <code>a[l],a[l+1],...,a[r]</code> ,那么它就是 <code>a</code> 的一个子数组。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,2,4,5,6]
|
||||
<strong>输出:</strong>17
|
||||
<strong>解释:</strong>最优子数组是 [2,4,5,6]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [5,2,1,2,5,2,1,2,5]
|
||||
<strong>输出:</strong>8
|
||||
<strong>解释:</strong>最优子数组是 [5,2,1] 或 [1,2,5]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>给你一个只包含字符 <code>'a'</code>,<code>'b'</code> 和 <code>'c'</code> 的字符串 <code>s</code> ,你可以执行下面这个操作(5 个步骤)任意次:</p>
|
||||
|
||||
<ol>
|
||||
<li>选择字符串 <code>s</code> 一个 <strong>非空</strong> 的前缀,这个前缀的所有字符都相同。</li>
|
||||
<li>选择字符串 <code>s</code> 一个 <strong>非空</strong> 的后缀,这个后缀的所有字符都相同。</li>
|
||||
<li>前缀和后缀在字符串中任意位置都不能有交集。</li>
|
||||
<li>前缀和后缀包含的所有字符都要相同。</li>
|
||||
<li>同时删除前缀和后缀。</li>
|
||||
</ol>
|
||||
|
||||
<p>请你返回对字符串 <code>s</code> 执行上面操作任意次以后(可能 0 次),能得到的 <strong>最短长度</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "ca"
|
||||
<b>输出:</b>2
|
||||
<strong>解释:</strong>你没法删除任何一个字符,所以字符串长度仍然保持不变。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "cabaabac"
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>最优操作序列为:
|
||||
- 选择前缀 "c" 和后缀 "c" 并删除它们,得到 s = "abaaba" 。
|
||||
- 选择前缀 "a" 和后缀 "a" 并删除它们,得到 s = "baab" 。
|
||||
- 选择前缀 "b" 和后缀 "b" 并删除它们,得到 s = "aa" 。
|
||||
- 选择前缀 "a" 和后缀 "a" 并删除它们,得到 s = "" 。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "aabccabba"
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>最优操作序列为:
|
||||
- 选择前缀 "aa" 和后缀 "a" 并删除它们,得到 s = "bccabb" 。
|
||||
- 选择前缀 "b" 和后缀 "bb" 并删除它们,得到 s = "cca" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> 只包含字符 <code>'a'</code>,<code>'b'</code> 和 <code>'c'</code> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>给你一个整数数组 <code>arr</code> ,请你删除一个子数组(可以为空),使得 <code>arr</code> 中剩下的元素是 <strong>非递减</strong> 的。</p>
|
||||
|
||||
<p>一个子数组指的是原数组中连续的一个子序列。</p>
|
||||
|
||||
<p>请你返回满足题目要求的最短子数组的长度。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,2,3,10,4,2,3,5]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>我们需要删除的最短子数组是 [10,4,2] ,长度为 3 。剩余元素形成非递减数组 [1,2,3,3,5] 。
|
||||
另一个正确的解为删除子数组 [3,10,4] 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [5,4,3,2,1]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>由于数组是严格递减的,我们只能保留一个元素。所以我们需要删除长度为 4 的子数组,要么删除 [5,4,3,2],要么删除 [4,3,2,1]。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,2,3]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>数组已经是非递减的了,我们不需要删除任何元素。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 10^5</code></li>
|
||||
<li><code>0 <= arr[i] <= 10^9</code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>给你一个整数 <code>n</code> ,如果你可以将 <code>n</code> 表示成若干个不同的三的幂之和,请你返回 <code>true</code> ,否则请返回 <code>false</code> 。</p>
|
||||
|
||||
<p>对于一个整数 <code>y</code> ,如果存在整数 <code>x</code> 满足 <code>y == 3<sup>x</sup></code> ,我们称这个整数 <code>y</code> 是三的幂。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>n = 12
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>12 = 3<sup>1</sup> + 3<sup>2</sup>
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>n = 91
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>91 = 3<sup>0</sup> + 3<sup>2</sup> + 3<sup>4</sup>
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>n = 21
|
||||
<b>输出:</b>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p><strong>全字母句</strong> 指包含英语字母表中每个字母至少一次的句子。</p>
|
||||
|
||||
<p>给你一个仅由小写英文字母组成的字符串 <code>sentence</code> ,请你判断 <code>sentence</code> 是否为 <strong>全字母句</strong> 。</p>
|
||||
|
||||
<p>如果是,返回<em> </em><code>true</code> ;否则,返回<em> </em><code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>sentence = "thequickbrownfoxjumpsoverthelazydog"
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong><code>sentence</code> 包含英语字母表中每个字母至少一次。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>sentence = "leetcode"
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= sentence.length <= 1000</code></li>
|
||||
<li><code>sentence</code> 由小写英语字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一个坐标 <code>coordinates</code> ,它是一个字符串,表示国际象棋棋盘中一个格子的坐标。下图是国际象棋棋盘示意图。</p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/04/03/chessboard.png" style="width: 400px; height: 396px;" /></p>
|
||||
|
||||
<p>如果所给格子的颜色是白色,请你返回 <code>true</code>,如果是黑色,请返回 <code>false</code> 。</p>
|
||||
|
||||
<p>给定坐标一定代表国际象棋棋盘上一个存在的格子。坐标第一个字符是字母,第二个字符是数字。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>coordinates = "a1"
|
||||
<b>输出:</b>false
|
||||
<b>解释:</b>如上图棋盘所示,"a1" 坐标的格子是黑色的,所以返回 false 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>coordinates = "h3"
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>如上图棋盘所示,"h3" 坐标的格子是白色的,所以返回 true 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>coordinates = "c7"
|
||||
<b>输出:</b>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>coordinates.length == 2</code></li>
|
||||
<li><code>'a' <= coordinates[0] <= 'h'</code></li>
|
||||
<li><code>'1' <= coordinates[1] <= '8'</code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给你一个偶数长度的字符串 <code>s</code> 。将其拆分成长度相同的两半,前一半为 <code>a</code> ,后一半为 <code>b</code> 。</p>
|
||||
|
||||
<p>两个字符串 <strong>相似</strong> 的前提是它们都含有相同数目的元音(<code>'a'</code>,<code>'e'</code>,<code>'i'</code>,<code>'o'</code>,<code>'u'</code>,<code>'A'</code>,<code>'E'</code>,<code>'I'</code>,<code>'O'</code>,<code>'U'</code>)。注意,<code>s</code> 可能同时含有大写和小写字母。</p>
|
||||
|
||||
<p>如果<em> </em><code>a</code><em> </em>和<em> </em><code>b</code> 相似,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "book"
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>a = "b<strong>o</strong>" 且 b = "<strong>o</strong>k" 。a 中有 1 个元音,b 也有 1 个元音。所以,a 和 b 相似。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "textbook"
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>a = "t<strong>e</strong>xt" 且 b = "b<strong>oo</strong>k" 。a 中有 1 个元音,b 中有 2 个元音。因此,a 和 b 不相似。
|
||||
注意,元音 o 在 b 中出现两次,记为 2 个。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 1000</code></li>
|
||||
<li><code>s.length</code> 是偶数</li>
|
||||
<li><code>s</code> 由 <strong>大写和小写</strong> 字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>给你两个大小为 <code>n x n</code> 的二进制矩阵 <code>mat</code> 和 <code>target</code> 。现<strong> 以 90 度顺时针轮转 </strong>矩阵 <code>mat</code> 中的元素 <strong>若干次</strong> ,如果能够使 <code>mat</code> 与 <code>target</code> 一致,返回 <code>true</code> ;否则,返回<em> </em><code>false</code><em> 。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/20/grid3.png" style="width: 301px; height: 121px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>mat = [[0,1],[1,0]], target = [[1,0],[0,1]]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>顺时针轮转 90 度一次可以使 mat 和 target 一致。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/20/grid4.png" style="width: 301px; height: 121px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>mat = [[0,1],[1,1]], target = [[1,0],[0,1]]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>无法通过轮转矩阵中的元素使 equal 与 target 一致。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/26/grid4.png" style="width: 661px; height: 184px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>顺时针轮转 90 度两次可以使 mat 和 target 一致。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == mat.length == target.length</code></li>
|
||||
<li><code>n == mat[i].length == target[i].length</code></li>
|
||||
<li><code>1 <= n <= 10</code></li>
|
||||
<li><code>mat[i][j]</code> 和 <code>target[i][j]</code> 不是 <code>0</code> 就是 <code>1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>有一只跳蚤的家在数轴上的位置 <code>x</code> 处。请你帮助它从位置 <code>0</code> 出发,到达它的家。</p>
|
||||
|
||||
<p>跳蚤跳跃的规则如下:</p>
|
||||
|
||||
<ul>
|
||||
<li>它可以 <strong>往前</strong> 跳恰好 <code>a</code> 个位置(即往右跳)。</li>
|
||||
<li>它可以 <strong>往后</strong> 跳恰好 <code>b</code> 个位置(即往左跳)。</li>
|
||||
<li>它不能 <strong>连续</strong> 往后跳 <code>2</code> 次。</li>
|
||||
<li>它不能跳到任何 <code>forbidden</code> 数组中的位置。</li>
|
||||
</ul>
|
||||
|
||||
<p>跳蚤可以往前跳 <strong>超过</strong> 它的家的位置,但是它 <strong>不能跳到负整数</strong> 的位置。</p>
|
||||
|
||||
<p>给你一个整数数组 <code>forbidden</code> ,其中 <code>forbidden[i]</code> 是跳蚤不能跳到的位置,同时给你整数 <code>a</code>, <code>b</code> 和 <code>x</code> ,请你返回跳蚤到家的最少跳跃次数。如果没有恰好到达 <code>x</code> 的可行方案,请你返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>forbidden = [14,4,18,1,15], a = 3, b = 15, x = 9
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>往前跳 3 次(0 -> 3 -> 6 -> 9),跳蚤就到家了。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>forbidden = [8,3,16,6,12,20], a = 15, b = 13, x = 11
|
||||
<b>输出:</b>-1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>forbidden = [1,6,2,14,5,17,4], a = 16, b = 9, x = 7
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>往前跳一次(0 -> 16),然后往回跳一次(16 -> 7),跳蚤就到家了。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= forbidden.length <= 1000</code></li>
|
||||
<li><code>1 <= a, b, forbidden[i] <= 2000</code></li>
|
||||
<li><code>0 <= x <= 2000</code></li>
|
||||
<li><code>forbidden</code> 中所有位置互不相同。</li>
|
||||
<li>位置 <code>x</code> 不在 <code>forbidden</code> 中。</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一个整数数组 <code>nums</code> (下标 <strong>从 0 开始</strong> 计数)以及两个整数 <code>target</code> 和 <code>start</code> ,请你找出一个下标 <code>i</code> ,满足 <code>nums[i] == target</code> 且 <code>abs(i - start)</code> <strong>最小化</strong> 。注意:<code>abs(x)</code> 表示 <code>x</code> 的绝对值。</p>
|
||||
|
||||
<p>返回 <code>abs(i - start)</code> 。</p>
|
||||
|
||||
<p>题目数据保证 <code>target</code> 存在于 <code>nums</code> 中。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,4,5], target = 5, start = 3
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>nums[4] = 5 是唯一一个等于 target 的值,所以答案是 abs(4 - 3) = 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1], target = 1, start = 0
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>nums[0] = 1 是唯一一个等于 target 的值,所以答案是 abs(0 - 0) = 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>nums 中的每个值都是 1 ,但 nums[0] 使 abs(i - start) 的结果得以最小化,所以答案是 abs(0 - 0) = 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= start < nums.length</code></li>
|
||||
<li><code>target</code> 存在于 <code>nums</code> 中</li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>给你一个二维整数数组 <code>intervals</code> ,其中 <code>intervals[i] = [left<sub>i</sub>, right<sub>i</sub>]</code> 表示第 <code>i</code> 个区间开始于 <code>left<sub>i</sub></code> 、结束于 <code>right<sub>i</sub></code>(包含两侧取值,<strong>闭区间</strong>)。区间的 <strong>长度</strong> 定义为区间中包含的整数数目,更正式地表达是 <code>right<sub>i</sub> - left<sub>i</sub> + 1</code> 。</p>
|
||||
|
||||
<p>再给你一个整数数组 <code>queries</code> 。第 <code>j</code> 个查询的答案是满足 <code>left<sub>i</sub> <= queries[j] <= right<sub>i</sub></code> 的 <strong>长度最小区间 <code>i</code> 的长度</strong> 。如果不存在这样的区间,那么答案是 <code>-1</code> 。</p>
|
||||
|
||||
<p>以数组形式返回对应查询的所有答案。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]
|
||||
<strong>输出:</strong>[3,3,1,4]
|
||||
<strong>解释:</strong>查询处理如下:
|
||||
- Query = 2 :区间 [2,4] 是包含 2 的最小区间,答案为 4 - 2 + 1 = 3 。
|
||||
- Query = 3 :区间 [2,4] 是包含 3 的最小区间,答案为 4 - 2 + 1 = 3 。
|
||||
- Query = 4 :区间 [4,4] 是包含 4 的最小区间,答案为 4 - 4 + 1 = 1 。
|
||||
- Query = 5 :区间 [3,6] 是包含 5 的最小区间,答案为 6 - 3 + 1 = 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]
|
||||
<strong>输出:</strong>[2,-1,4,6]
|
||||
<strong>解释:</strong>查询处理如下:
|
||||
- Query = 2 :区间 [2,3] 是包含 2 的最小区间,答案为 3 - 2 + 1 = 2 。
|
||||
- Query = 19:不存在包含 19 的区间,答案为 -1 。
|
||||
- Query = 5 :区间 [2,5] 是包含 5 的最小区间,答案为 5 - 2 + 1 = 4 。
|
||||
- Query = 22:区间 [20,25] 是包含 22 的最小区间,答案为 25 - 20 + 1 = 6 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= intervals.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[i].length == 2</code></li>
|
||||
<li><code>1 <= left<sub>i</sub> <= right<sub>i</sub> <= 10<sup>7</sup></code></li>
|
||||
<li><code>1 <= queries[j] <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>如果一个十进制数字不含任何前导零,且每一位上的数字不是 <code>0</code> 就是 <code>1</code> ,那么该数字就是一个 <strong>十-二进制数</strong> 。例如,<code>101</code> 和 <code>1100</code> 都是 <strong>十-二进制数</strong>,而 <code>112</code> 和 <code>3001</code> 不是。</p>
|
||||
|
||||
<p>给你一个表示十进制整数的字符串 <code>n</code> ,返回和为 <code>n</code> 的 <strong>十-二进制数 </strong>的最少数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = "32"
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>10 + 11 + 11 = 32
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = "82734"
|
||||
<strong>输出:</strong>8
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = "27346209830709182346"
|
||||
<strong>输出:</strong>9
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>n</code> 仅由数字组成</li>
|
||||
<li><code>n</code> 不含任何前导零并总是表示正整数</li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>给你一个二维数组 <code>tasks</code> ,用于表示 <code>n</code> 项从 <code>0</code> 到 <code>n - 1</code> 编号的任务。其中 <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> 意味着第 <code>i<sup></sup></code> 项任务将会于 <code>enqueueTime<sub>i</sub></code> 时进入任务队列,需要 <code>processingTime<sub>i</sub></code><sub> </sub>的时长完成执行。</p>
|
||||
|
||||
<p>现有一个单线程 CPU ,同一时间只能执行 <strong>最多一项</strong> 任务,该 CPU 将会按照下述方式运行:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果 CPU 空闲,且任务队列中没有需要执行的任务,则 CPU 保持空闲状态。</li>
|
||||
<li>如果 CPU 空闲,但任务队列中有需要执行的任务,则 CPU 将会选择 <strong>执行时间最短</strong> 的任务开始执行。如果多个任务具有同样的最短执行时间,则选择下标最小的任务开始执行。</li>
|
||||
<li>一旦某项任务开始执行,CPU 在 <strong>执行完整个任务</strong> 前都不会停止。</li>
|
||||
<li>CPU 可以在完成一项任务后,立即开始执行一项新任务。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回<em> </em>CPU<em> </em>处理任务的顺序。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>tasks = [[1,2],[2,4],[3,2],[4,1]]
|
||||
<strong>输出:</strong>[0,2,3,1]
|
||||
<strong>解释:</strong>事件按下述流程运行:
|
||||
- time = 1 ,任务 0 进入任务队列,可执行任务项 = {0}
|
||||
- 同样在 time = 1 ,空闲状态的 CPU 开始执行任务 0 ,可执行任务项 = {}
|
||||
- time = 2 ,任务 1 进入任务队列,可执行任务项 = {1}
|
||||
- time = 3 ,任务 2 进入任务队列,可执行任务项 = {1, 2}
|
||||
- 同样在 time = 3 ,CPU 完成任务 0 并开始执行队列中用时最短的任务 2 ,可执行任务项 = {1}
|
||||
- time = 4 ,任务 3 进入任务队列,可执行任务项 = {1, 3}
|
||||
- time = 5 ,CPU 完成任务 2 并开始执行队列中用时最短的任务 3 ,可执行任务项 = {1}
|
||||
- time = 6 ,CPU 完成任务 3 并开始执行任务 1 ,可执行任务项 = {}
|
||||
- time = 10 ,CPU 完成任务 1 并进入空闲状态
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]
|
||||
<strong>输出:</strong>[4,3,2,0,1]
|
||||
<strong>解释:</strong>事件按下述流程运行:
|
||||
- time = 7 ,所有任务同时进入任务队列,可执行任务项 = {0,1,2,3,4}
|
||||
- 同样在 time = 7 ,空闲状态的 CPU 开始执行任务 4 ,可执行任务项 = {0,1,2,3}
|
||||
- time = 9 ,CPU 完成任务 4 并开始执行任务 3 ,可执行任务项 = {0,1,2}
|
||||
- time = 13 ,CPU 完成任务 3 并开始执行任务 2 ,可执行任务项 = {0,1}
|
||||
- time = 18 ,CPU 完成任务 2 并开始执行任务 0 ,可执行任务项 = {1}
|
||||
- time = 28 ,CPU 完成任务 0 并开始执行任务 1 ,可执行任务项 = {}
|
||||
- time = 40 ,CPU 完成任务 1 并进入空闲状态</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>tasks.length == n</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>请你将一些箱子装在 <strong>一辆卡车</strong> 上。给你一个二维数组 <code>boxTypes</code> ,其中 <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>numberOfBoxes<sub>i</sub></code> 是类型 <code>i</code> 的箱子的数量。</li>
|
||||
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>是类型 <code>i</code> 每个箱子可以装载的单元数量。</li>
|
||||
</ul>
|
||||
|
||||
<p>整数 <code>truckSize</code> 表示卡车上可以装载 <strong>箱子</strong> 的 <strong>最大数量</strong> 。只要箱子数量不超过 <code>truckSize</code> ,你就可以选择任意箱子装到卡车上。</p>
|
||||
|
||||
<p>返回卡车可以装载 <strong>单元</strong> 的 <strong>最大</strong> 总数<em>。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
|
||||
<strong>输出:</strong>8
|
||||
<strong>解释:</strong>箱子的情况如下:
|
||||
- 1 个第一类的箱子,里面含 3 个单元。
|
||||
- 2 个第二类的箱子,每个里面含 2 个单元。
|
||||
- 3 个第三类的箱子,每个里面含 1 个单元。
|
||||
可以选择第一类和第二类的所有箱子,以及第三类的一个箱子。
|
||||
单元总数 = (1 * 3) + (2 * 2) + (1 * 1) = 8</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
|
||||
<strong>输出:</strong>91
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= boxTypes.length <= 1000</code></li>
|
||||
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
|
||||
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>给你一个 <strong>有效的</strong> 布尔表达式,用字符串 <code>expression</code> 表示。这个字符串包含字符 <code>'1'</code>,<code>'0'</code>,<code>'&'</code>(按位 <strong>与</strong> 运算),<code>'|'</code>(按位 <strong>或</strong> 运算),<code>'('</code> 和 <code>')'</code> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,<code>"()1|1"</code> 和 <code>"(1)&()"</code> <strong>不是有效</strong> 布尔表达式。而 <code>"1"</code>, <code>"(((1))|(0))"</code> 和 <code>"1|(0&(1))"</code> 是 <strong>有效</strong> 布尔表达式。</li>
|
||||
</ul>
|
||||
|
||||
<p>你的目标是将布尔表达式的 <strong>值</strong> <strong>反转 </strong>(也就是将 <code>0</code> 变为 <code>1</code> ,或者将 <code>1</code> 变为 <code>0</code>),请你返回达成目标需要的 <strong>最少操作</strong> 次数。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,如果表达式 <code>expression = "1|1|(0&0)&1"</code> ,它的 <strong>值</strong> 为 <code>1|1|(0&0)&1 = 1|1|0&1 = 1|0&1 = 1&1 = 1</code> 。我们想要执行操作将 <strong>新的</strong> 表达式的值变成 <code>0</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>可执行的 <strong>操作</strong> 如下:</p>
|
||||
|
||||
<ul>
|
||||
<li>将一个 <code>'1'</code> 变成一个 <code>'0'</code> 。</li>
|
||||
<li>将一个 <code>'0'</code> 变成一个 <code>'1'</code> 。</li>
|
||||
<li>将一个 <code>'&'</code> 变成一个 <code>'|'</code> 。</li>
|
||||
<li>将一个 <code>'|'</code> 变成一个 <code>'&'</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>注意:</strong><code>'&'</code> 的 <strong>运算优先级</strong> 与 <code>'|'</code> <strong>相同</strong> 。计算表达式时,括号优先级 <strong>最高</strong> ,然后按照 <strong>从左到右</strong> 的顺序运算。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>expression = "1&(0|1)"
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>我们可以将 "1&(0<strong>|</strong>1)" 变成 "1&(0<strong>&</strong>1)" ,执行的操作为将一个 '|' 变成一个 '&' ,执行了 1 次操作。
|
||||
新表达式的值为 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>expression = "(0&0)&(0&0&0)"
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>我们可以将 "(0<strong>&0</strong>)<strong>&</strong>(0&0&0)" 变成 "(0<strong>|1</strong>)<strong>|</strong>(0&0&0)" ,执行了 3 次操作。
|
||||
新表达式的值为 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>expression = "(0|(1|0&1))"
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>我们可以将 "(0|(<strong>1</strong>|0&1))" 变成 "(0|(<strong>0</strong>|0&1))" ,执行了 1 次操作。
|
||||
新表达式的值为 0 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= expression.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>expression</code> 只包含 <code>'1'</code>,<code>'0'</code>,<code>'&'</code>,<code>'|'</code>,<code>'('</code> 和 <code>')'</code></li>
|
||||
<li>所有括号都有与之匹配的对应括号。</li>
|
||||
<li>不会有空的括号(也就是说 <code>"()"</code> 不是 <code>expression</code> 的子字符串)。</li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>一个句子是由一些单词与它们之间的单个空格组成,且句子的开头和结尾没有多余空格。比方说,<code>"Hello World"</code> ,<code>"HELLO"</code> ,<code>"hello world hello world"</code> 都是句子。每个单词都 <strong>只</strong> 包含大写和小写英文字母。</p>
|
||||
|
||||
<p>如果两个句子 <code>sentence1</code> 和 <code>sentence2</code> ,可以通过往其中一个句子插入一个任意的句子(<strong>可以是空句子</strong>)而得到另一个句子,那么我们称这两个句子是 <strong>相似的</strong> 。比方说,<code>sentence1 = "Hello my name is Jane"</code> 且 <code>sentence2 = "Hello Jane"</code> ,我们可以往 <code>sentence2</code> 中 <code>"Hello"</code> 和 <code>"Jane"</code> 之间插入 <code>"my name is"</code> 得到 <code>sentence1</code> 。</p>
|
||||
|
||||
<p>给你两个句子 <code>sentence1</code> 和 <code>sentence2</code> ,如果<em> </em><code>sentence1</code> 和<em> </em><code>sentence2</code> 是相似的,请你返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>sentence1 = "My name is Haley", sentence2 = "My Haley"
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>可以往 sentence2 中 "My" 和 "Haley" 之间插入 "name is" ,得到 sentence1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>sentence1 = "of", sentence2 = "A lot of words"
|
||||
<b>输出:</b>false
|
||||
<strong>解释:</strong>没法往这两个句子中的一个句子只插入一个句子就得到另一个句子。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>sentence1 = "Eating right now", sentence2 = "Eating"
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>可以往 sentence2 的结尾插入 "right now" 得到 sentence1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>sentence1 = "Luky", sentence2 = "Lucccky"
|
||||
<b>输出:</b>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= sentence1.length, sentence2.length <= 100</code></li>
|
||||
<li><code>sentence1</code> 和 <code>sentence2</code> 都只包含大小写英文字母和空格。</li>
|
||||
<li><code>sentence1</code> 和 <code>sentence2</code> 中的单词都只由单个空格隔开。</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>给你一个 <strong>有向无环图</strong> , <code>n</code> 个节点编号为 <code>0</code> 到 <code>n-1</code> ,以及一个边数组 <code>edges</code> ,其中 <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> 表示一条从点 <code>from<sub>i</sub></code> 到点 <code>to<sub>i</sub></code> 的有向边。</p>
|
||||
|
||||
<p>找到最小的点集使得从这些点出发能到达图中所有点。题目保证解存在且唯一。</p>
|
||||
|
||||
<p>你可以以任意顺序返回这些节点编号。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/22/5480e1.png" style="height: 181px; width: 231px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]
|
||||
<strong>输出:</strong>[0,3]
|
||||
<strong>解释:</strong>从单个节点出发无法到达所有节点。从 0 出发我们可以到达 [0,1,2,5] 。从 3 出发我们可以到达 [3,4,2,5] 。所以我们输出 [0,3] 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/22/5480e2.png" style="height: 201px; width: 201px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]]
|
||||
<strong>输出:</strong>[0,2,3]
|
||||
<strong>解释:</strong>注意到节点 0,3 和 2 无法从其他节点到达,所以我们必须将它们包含在结果点集中,这些点都能到达节点 1 和 4 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10^5</code></li>
|
||||
<li><code>1 <= edges.length <= min(10^5, n * (n - 1) / 2)</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= from<sub>i,</sub> to<sub>i</sub> < n</code></li>
|
||||
<li>所有点对 <code>(from<sub>i</sub>, to<sub>i</sub>)</code> 互不相同。</li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>给你一个整数数组 <code>heights</code> ,表示建筑物的高度。另有一些砖块 <code>bricks</code> 和梯子 <code>ladders</code> 。</p>
|
||||
|
||||
<p>你从建筑物 <code>0</code> 开始旅程,不断向后面的建筑物移动,期间可能会用到砖块或梯子。</p>
|
||||
|
||||
<p>当从建筑物 <code>i</code> 移动到建筑物 <code>i+1</code>(下标<strong> 从 0 开始 </strong>)时:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果当前建筑物的高度 <strong>大于或等于</strong> 下一建筑物的高度,则不需要梯子或砖块</li>
|
||||
<li>如果当前建筑的高度 <strong>小于</strong> 下一个建筑的高度,您可以使用 <strong>一架梯子</strong> 或 <strong><code>(h[i+1] - h[i])</code> 个砖块</strong></li>
|
||||
</ul>
|
||||
如果以最佳方式使用给定的梯子和砖块,返回你可以到达的最远建筑物的下标(下标<strong> 从 0 开始 </strong>)。
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/10/31/q4.gif" style="width: 562px; height: 561px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>从建筑物 0 出发,你可以按此方案完成旅程:
|
||||
- 不使用砖块或梯子到达建筑物 1 ,因为 4 >= 2
|
||||
- 使用 5 个砖块到达建筑物 2 。你必须使用砖块或梯子,因为 2 < 7
|
||||
- 不使用砖块或梯子到达建筑物 3 ,因为 7 >= 6
|
||||
- 使用唯一的梯子到达建筑物 4 。你必须使用砖块或梯子,因为 6 < 9
|
||||
无法越过建筑物 4 ,因为没有更多砖块或梯子。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
|
||||
<strong>输出:</strong>7
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>heights = [14,3,19,3], bricks = 17, ladders = 0
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= heights.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= heights[i] <= 10<sup>6</sup></code></li>
|
||||
<li><code>0 <= bricks <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= ladders <= heights.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>给你一个数组 <code>rectangles</code> ,其中 <code>rectangles[i] = [l<sub>i</sub>, w<sub>i</sub>]</code> 表示第 <code>i</code> 个矩形的长度为 <code>l<sub>i</sub></code> 、宽度为 <code>w<sub>i</sub></code> 。</p>
|
||||
|
||||
<p>如果存在 <code>k</code> 同时满足 <code>k <= l<sub>i</sub></code> 和 <code>k <= w<sub>i</sub></code> ,就可以将第 <code>i</code> 个矩形切成边长为 <code>k</code> 的正方形。例如,矩形 <code>[4,6]</code> 可以切成边长最大为 <code>4</code> 的正方形。</p>
|
||||
|
||||
<p>设 <code>maxLen</code> 为可以从矩形数组 <code>rectangles</code> 切分得到的 <strong>最大正方形</strong> 的边长。</p>
|
||||
|
||||
<p>请你统计有多少个矩形能够切出边长为<em> </em><code>maxLen</code> 的正方形,并返回矩形 <strong>数目</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>rectangles = [[5,8],[3,9],[5,12],[16,5]]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>能从每个矩形中切出的最大正方形边长分别是 [5,3,5,5] 。
|
||||
最大正方形的边长为 5 ,可以由 3 个矩形切分得到。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>rectangles = [[2,3],[3,7],[4,3],[3,7]]
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= rectangles.length <= 1000</code></li>
|
||||
<li><code>rectangles[i].length == 2</code></li>
|
||||
<li><code>1 <= l<sub>i</sub>, w<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>l<sub>i</sub> != w<sub>i</sub></code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>表:<code>Products</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| product_id | int |
|
||||
| low_fats | enum |
|
||||
| recyclable | enum |
|
||||
+-------------+---------+
|
||||
product_id 是这个表的主键。
|
||||
low_fats 是枚举类型,取值为以下两种 ('Y', 'N'),其中 'Y' 表示该产品是低脂产品,'N' 表示不是低脂产品。
|
||||
recyclable 是枚举类型,取值为以下两种 ('Y', 'N'),其中 'Y' 表示该产品可回收,而 'N' 表示不可回收。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>写出 SQL 语句,查找既是低脂又是可回收的产品编号。</p>
|
||||
|
||||
<p>返回结果 <strong>无顺序要求</strong> 。</p>
|
||||
|
||||
<p>查询结果格式如下例所示:</p>
|
||||
|
||||
<pre>
|
||||
Products 表:
|
||||
+-------------+----------+------------+
|
||||
| product_id | low_fats | recyclable |
|
||||
+-------------+----------+------------+
|
||||
| 0 | Y | N |
|
||||
| 1 | Y | Y |
|
||||
| 2 | N | Y |
|
||||
| 3 | Y | Y |
|
||||
| 4 | N | N |
|
||||
+-------------+----------+------------+
|
||||
Result 表:
|
||||
+-------------+
|
||||
| product_id |
|
||||
+-------------+
|
||||
| 1 |
|
||||
| 3 |
|
||||
+-------------+
|
||||
只有产品 id 为 1 和 3 的产品,既是低脂又是可回收的产品。
|
||||
</pre>
|
@@ -0,0 +1,50 @@
|
||||
<p>给你两个字符串 <code>s</code> 和 <code>p</code> ,其中 <code>p</code> 是 <code>s</code> 的一个 <strong>子序列</strong> 。同时,给你一个元素 <strong>互不相同</strong> 且下标 <strong>从 0 开始</strong> 计数的整数数组 <code>removable</code> ,该数组是 <code>s</code> 中下标的一个子集(<code>s</code> 的下标也 <strong>从 0 开始</strong> 计数)。</p>
|
||||
|
||||
<p>请你找出一个整数 <code>k</code>(<code>0 <= k <= removable.length</code>),选出 <code>removable</code> 中的 <strong>前</strong> <code>k</code> 个下标,然后从 <code>s</code> 中移除这些下标对应的 <code>k</code> 个字符。整数 <code>k</code> 需满足:在执行完上述步骤后, <code>p</code> 仍然是 <code>s</code> 的一个 <strong>子序列</strong> 。更正式的解释是,对于每个 <code>0 <= i < k</code> ,先标记出位于 <code>s[removable[i]]</code> 的字符,接着移除所有标记过的字符,然后检查 <code>p</code> 是否仍然是 <code>s</code> 的一个子序列。</p>
|
||||
|
||||
<p>返回你可以找出的 <strong>最大</strong><em> </em><code>k</code><em> </em>,满足在移除字符后<em> </em><code>p</code><em> </em>仍然是 <code>s</code> 的一个子序列。</p>
|
||||
|
||||
<p>字符串的一个 <strong>子序列</strong> 是一个由原字符串生成的新字符串,生成过程中可能会移除原字符串中的一些字符(也可能不移除)但不改变剩余字符之间的相对顺序。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abcacb", p = "ab", removable = [3,1,0]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>在移除下标 3 和 1 对应的字符后,"a<strong>b</strong>c<strong>a</strong>cb" 变成 "accb" 。
|
||||
"ab" 是 "<strong>a</strong>cc<strong>b</strong>" 的一个子序列。
|
||||
如果移除下标 3、1 和 0 对应的字符后,"<strong>ab</strong>c<strong>a</strong>cb" 变成 "ccb" ,那么 "ab" 就不再是 s 的一个子序列。
|
||||
因此,最大的 k 是 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abcbddddd", p = "abcd", removable = [3,2,1,4,5,6]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>在移除下标 3 对应的字符后,"abc<strong>b</strong>ddddd" 变成 "abcddddd" 。
|
||||
"abcd" 是 "<strong>abcd</strong>dddd" 的一个子序列。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abcab", p = "abc", removable = [0,1,2,3,4]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>如果移除数组 removable 的第一个下标,"abc" 就不再是 s 的一个子序列。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= p.length <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= removable.length < s.length</code></li>
|
||||
<li><code>0 <= removable[i] < s.length</code></li>
|
||||
<li><code>p</code> 是 <code>s</code> 的一个 <strong>子字符串</strong></li>
|
||||
<li><code>s</code> 和 <code>p</code> 都由小写英文字母组成</li>
|
||||
<li><code>removable</code> 中的元素 <strong>互不相同</strong></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>给你一个点数组 <code>points</code> 和一个表示角度的整数 <code>angle</code> ,你的位置是 <code>location</code> ,其中 <code>location = [pos<sub>x</sub>, pos<sub>y</sub>]</code> 且 <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> 都表示 X-Y 平面上的整数坐标。</p>
|
||||
|
||||
<p>最开始,你面向东方进行观测。你 <strong>不能</strong> 进行移动改变位置,但可以通过 <strong>自转</strong> 调整观测角度。换句话说,<code>pos<sub>x</sub></code> 和 <code>pos<sub>y</sub></code> 不能改变。你的视野范围的角度用 <code>angle</code> 表示, 这决定了你观测任意方向时可以多宽。设 <code>d</code> 为你逆时针自转旋转的度数,那么你的视野就是角度范围 <code>[d - angle/2, d + angle/2]</code> 所指示的那片区域。</p>
|
||||
|
||||
<video autoplay="" controls="" height="360" muted="" style="max-width:100%;height:auto;" width="750"><source src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/10/04/angle.mp4" type="video/mp4" />Your browser does not support the video tag or this video format.</video>
|
||||
|
||||
<p>对于每个点,如果由该点、你的位置以及从你的位置直接向东的方向形成的角度 <strong>位于你的视野中</strong> ,那么你就可以看到它。</p>
|
||||
|
||||
<p>同一个坐标上可以有多个点。你所在的位置也可能存在一些点,但不管你的怎么旋转,总是可以看到这些点。同时,点不会阻碍你看到其他点。</p>
|
||||
|
||||
<p>返回你能看到的点的最大数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/10/04/89a07e9b-00ab-4967-976a-c723b2aa8656.png" style="height: 300px; width: 400px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>阴影区域代表你的视野。在你的视野中,所有的点都清晰可见,尽管 [2,2] 和 [3,3]在同一条直线上,你仍然可以看到 [3,3] 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>points = [[2,1],[2,2],[3,4],[1,1]], angle = 90, location = [1,1]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>在你的视野中,所有的点都清晰可见,包括你所在位置的那个点。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/10/04/5010bfd3-86e6-465f-ac64-e9df941d2e49.png" style="height: 348px; width: 690px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>points = [[1,0],[2,1]], angle = 13, location = [1,1]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>如图所示,你只能看到两点之一。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>location.length == 2</code></li>
|
||||
<li><code>0 <= angle < 360</code></li>
|
||||
<li><code>0 <= pos<sub>x</sub>, pos<sub>y</sub>, x<sub>i</sub>, y<sub>i</sub> <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>厨房里总共有 <code>n</code> 个橘子,你决定每一天选择如下方式之一吃这些橘子:</p>
|
||||
|
||||
<ul>
|
||||
<li>吃掉一个橘子。</li>
|
||||
<li>如果剩余橘子数 <code>n</code> 能被 2 整除,那么你可以吃掉 <code>n/2</code> 个橘子。</li>
|
||||
<li>如果剩余橘子数 <code>n</code> 能被 3 整除,那么你可以吃掉 <code>2*(n/3)</code> 个橘子。</li>
|
||||
</ul>
|
||||
|
||||
<p>每天你只能从以上 3 种方案中选择一种方案。</p>
|
||||
|
||||
<p>请你返回吃掉所有 <code>n</code> 个橘子的最少天数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 10
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>你总共有 10 个橘子。
|
||||
第 1 天:吃 1 个橘子,剩余橘子数 10 - 1 = 9。
|
||||
第 2 天:吃 6 个橘子,剩余橘子数 9 - 2*(9/3) = 9 - 6 = 3。(9 可以被 3 整除)
|
||||
第 3 天:吃 2 个橘子,剩余橘子数 3 - 2*(3/3) = 3 - 2 = 1。
|
||||
第 4 天:吃掉最后 1 个橘子,剩余橘子数 1 - 1 = 0。
|
||||
你需要至少 4 天吃掉 10 个橘子。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 6
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>你总共有 6 个橘子。
|
||||
第 1 天:吃 3 个橘子,剩余橘子数 6 - 6/2 = 6 - 3 = 3。(6 可以被 2 整除)
|
||||
第 2 天:吃 2 个橘子,剩余橘子数 3 - 2*(3/3) = 3 - 2 = 1。(3 可以被 3 整除)
|
||||
第 3 天:吃掉剩余 1 个橘子,剩余橘子数 1 - 1 = 0。
|
||||
你至少需要 3 天吃掉 6 个橘子。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 1
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 56
|
||||
<strong>输出:</strong>6
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2*10^9</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>有一棵特殊的苹果树,一连 <code>n</code> 天,每天都可以长出若干个苹果。在第 <code>i</code> 天,树上会长出 <code>apples[i]</code> 个苹果,这些苹果将会在 <code>days[i]</code> 天后(也就是说,第 <code>i + days[i]</code> 天时)腐烂,变得无法食用。也可能有那么几天,树上不会长出新的苹果,此时用 <code>apples[i] == 0</code> 且 <code>days[i] == 0</code> 表示。</p>
|
||||
|
||||
<p>你打算每天 <strong>最多</strong> 吃一个苹果来保证营养均衡。注意,你可以在这 <code>n</code> 天之后继续吃苹果。</p>
|
||||
|
||||
<p>给你两个长度为 <code>n</code> 的整数数组 <code>days</code> 和 <code>apples</code> ,返回你可以吃掉的苹果的最大数目<em>。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>apples = [1,2,3,5,2], days = [3,2,1,4,2]
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>你可以吃掉 7 个苹果:
|
||||
- 第一天,你吃掉第一天长出来的苹果。
|
||||
- 第二天,你吃掉一个第二天长出来的苹果。
|
||||
- 第三天,你吃掉一个第二天长出来的苹果。过了这一天,第三天长出来的苹果就已经腐烂了。
|
||||
- 第四天到第七天,你吃的都是第四天长出来的苹果。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>你可以吃掉 5 个苹果:
|
||||
- 第一天到第三天,你吃的都是第一天长出来的苹果。
|
||||
- 第四天和第五天不吃苹果。
|
||||
- 第六天和第七天,你吃的都是第六天长出来的苹果。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>apples.length == n</code></li>
|
||||
<li><code>days.length == n</code></li>
|
||||
<li><code>1 <= n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= apples[i], days[i] <= 2 * 10<sup>4</sup></code></li>
|
||||
<li>只有在 <code>apples[i] = 0</code> 时,<code>days[i] = 0</code> 才成立</li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>给你两个链表 <code>list1</code> 和 <code>list2</code> ,它们包含的元素分别为 <code>n</code> 个和 <code>m</code> 个。</p>
|
||||
|
||||
<p>请你将 <code>list1</code> 中下标从 <code>a</code> 到 <code>b</code> 的全部节点都删除,并将<code>list2</code> 接在被删除节点的位置。</p>
|
||||
|
||||
<p>下图中蓝色边和节点展示了操作后的结果:</p>
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/11/28/fig1.png" style="height: 130px; width: 504px;" />
|
||||
<p>请你返回结果链表的头指针。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/11/28/merge_linked_list_ex1.png" style="width: 406px; height: 140px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>list1 = [0,1,2,3,4,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]
|
||||
<b>输出:</b>[0,1,2,1000000,1000001,1000002,5]
|
||||
<b>解释:</b>我们删除 list1 中下标为 3 和 4 的两个节点,并将 list2 接在该位置。上图中蓝色的边和节点为答案链表。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/11/28/merge_linked_list_ex2.png" style="width: 463px; height: 140px;" />
|
||||
<pre>
|
||||
<b>输入:</b>list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [1000000,1000001,1000002,1000003,1000004]
|
||||
<b>输出:</b>[0,1,1000000,1000001,1000002,1000003,1000004,6]
|
||||
<b>解释:</b>上图中蓝色的边和节点为答案链表。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= list1.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= a <= b < list1.length - 1</code></li>
|
||||
<li><code>1 <= list2.length <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,68 @@
|
||||
<p>给你 <code>n</code> 个 <strong>二叉搜索树的根节点</strong> ,存储在数组 <code>trees</code> 中(<strong>下标从 0 开始</strong>),对应 <code>n</code> 棵不同的二叉搜索树。<code>trees</code> 中的每棵二叉搜索树 <strong>最多有 3 个节点</strong> ,且不存在值相同的两个根节点。在一步操作中,将会完成下述步骤:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择两个 <strong>不同的</strong> 下标 <code>i</code> 和 <code>j</code> ,要求满足在 <code>trees[i]</code> 中的某个 <strong>叶节点</strong> 的值等于 <code>trees[j]</code> 的 <strong>根节点的值</strong> 。</li>
|
||||
<li>用 <code>trees[j]</code> 替换 <code>trees[i]</code> 中的那个叶节点。</li>
|
||||
<li>从 <code>trees</code> 中移除 <code>trees[j]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果在执行 <code>n - 1</code> 次操作后,能形成一棵有效的二叉搜索树,则返回结果二叉树的 <strong>根节点</strong> ;如果无法构造一棵有效的二叉搜索树<em>,</em>返回<em> </em><code>null</code> 。</p>
|
||||
|
||||
<p>二叉搜索树是一种二叉树,且树中每个节点均满足下述属性:</p>
|
||||
|
||||
<ul>
|
||||
<li>任意节点的左子树中的值都 <strong>严格小于</strong> 此节点的值。</li>
|
||||
<li>任意节点的右子树中的值都 <strong>严格大于</strong> 此节点的值。</li>
|
||||
</ul>
|
||||
|
||||
<p>叶节点是不含子节点的节点。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/08/d1.png" />
|
||||
<pre>
|
||||
<strong>输入:</strong>trees = [[2,1],[3,2,5],[5,4]]
|
||||
<strong>输出:</strong>[3,2,5,1,null,4]
|
||||
<strong>解释:</strong>
|
||||
第一步操作中,选出 i=1 和 j=0 ,并将 trees[0] 合并到 trees[1] 中。
|
||||
删除 trees[0] ,trees = [[3,2,5,1],[5,4]] 。
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/24/diagram.png" />
|
||||
在第二步操作中,选出 i=0 和 j=1 ,将 trees[1] 合并到 trees[0] 中。
|
||||
删除 trees[1] ,trees = [[3,2,5,1,null,4]] 。
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/24/diagram-2.png" />
|
||||
结果树如上图所示,为一棵有效的二叉搜索树,所以返回该树的根节点。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/08/d2.png" />
|
||||
<pre>
|
||||
<strong>输入:</strong>trees = [[5,3,8],[3,2,6]]
|
||||
<strong>输出:</strong>[]
|
||||
<strong>解释:</strong>
|
||||
选出 i=0 和 j=1 ,然后将 trees[1] 合并到 trees[0] 中。
|
||||
删除 trees[1] ,trees = [[5,3,8,2,6]] 。
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/24/diagram-3.png" />
|
||||
结果树如上图所示。仅能执行一次有效的操作,但结果树不是一棵有效的二叉搜索树,所以返回 null 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/08/d3.png" />
|
||||
<pre>
|
||||
<strong>输入:</strong>trees = [[5,4],[3]]
|
||||
<strong>输出:</strong>[]
|
||||
<strong>解释:</strong>无法执行任何操作。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == trees.length</code></li>
|
||||
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
|
||||
<li>每棵树中节点数目在范围 <code>[1, 3]</code> 内。</li>
|
||||
<li>输入数据的每个节点可能有子节点但不存在子节点的子节点</li>
|
||||
<li><code>trees</code> 中不存在两棵树根节点值相同的情况。</li>
|
||||
<li>输入中的所有树都是 <strong>有效的二叉树搜索树</strong> 。</li>
|
||||
<li><code>1 <= TreeNode.val <= 5 * 10<sup>4</sup></code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,63 @@
|
||||
<p><strong>三元组</strong> 是一个由三个整数组成的数组。给你一个二维整数数组 <code>triplets</code> ,其中 <code>triplets[i] = [a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>]</code> 表示第 <code>i</code> 个 <strong>三元组</strong> 。同时,给你一个整数数组 <code>target = [x, y, z]</code> ,表示你想要得到的 <strong>三元组</strong> 。</p>
|
||||
|
||||
<p>为了得到 <code>target</code> ,你需要对 <code>triplets</code> 执行下面的操作 <strong>任意次</strong>(可能 <strong>零</strong> 次):</p>
|
||||
|
||||
<ul>
|
||||
<li>选出两个下标(下标 <strong>从 0 开始</strong> 计数)<code>i</code> 和 <code>j</code>(<code>i != j</code>),并 <strong>更新</strong> <code>triplets[j]</code> 为 <code>[max(a<sub>i</sub>, a<sub>j</sub>), max(b<sub>i</sub>, b<sub>j</sub>), max(c<sub>i</sub>, c<sub>j</sub>)]</code> 。
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>triplets[i] = [2, 5, 3]</code> 且 <code>triplets[j] = [1, 7, 5]</code>,<code>triplets[j]</code> 将会更新为 <code>[max(2, 1), max(5, 7), max(3, 5)] = [2, 7, 5]</code> 。</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>如果通过以上操作我们可以使得目标 <strong>三元组</strong> <code>target</code> 成为 <code>triplets</code> 的一个 <strong>元素</strong> ,则返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>执行下述操作:
|
||||
- 选择第一个和最后一个三元组 [<strong>[2,5,3]</strong>,[1,8,4],<strong>[1,7,5]</strong>] 。更新最后一个三元组为 [max(2,1), max(5,7), max(3,5)] = [2,7,5] 。triplets = [[2,5,3],[1,8,4],<strong>[2,7,5]</strong>]
|
||||
目标三元组 [2,7,5] 现在是 triplets 的一个元素。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>triplets = [[1,3,4],[2,5,8]], target = [2,5,8]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>目标三元组 [2,5,8] 已经是 triplets 的一个元素。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>triplets = [[2,5,3],[2,3,4],[1,2,5],[5,2,3]], target = [5,5,5]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>执行下述操作:
|
||||
- 选择第一个和第三个三元组 [<strong>[2,5,3]</strong>,[2,3,4],<strong>[1,2,5]</strong>,[5,2,3]] 。更新第三个三元组为 [max(2,1), max(5,2), max(3,5)] = [2,5,5] 。triplets = [[2,5,3],[2,3,4],<strong>[2,5,5]</strong>,[5,2,3]] 。
|
||||
- 选择第三个和第四个三元组 [[2,5,3],[2,3,4],<strong>[2,5,5]</strong>,<strong>[5,2,3]</strong>] 。更新第四个三元组为 [max(2,5), max(5,2), max(5,3)] = [5,5,5] 。triplets = [[2,5,3],[2,3,4],[2,5,5],<strong>[5,5,5]</strong>] 。
|
||||
目标三元组 [5,5,5] 现在是 triplets 的一个元素。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>triplets = [[3,4,5],[4,5,6]], target = [3,2,5]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>无法得到 [3,2,5] ,因为 triplets 不含 2 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= triplets.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>triplets[i].length == target.length == 3</code></li>
|
||||
<li><code>1 <= a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>, x, y, z <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>给你一个二进制字符串 <code>s</code> 。如果字符串中由 <code>1</code> 组成的 <strong>最长</strong> 连续子字符串 <strong>严格长于</strong> 由 <code>0</code> 组成的 <strong>最长</strong> 连续子字符串,返回 <code>true</code> ;否则,返回 <code>false</code><em> </em>。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>s = "<strong>11</strong>01<strong>000</strong>10"</code> 中,由 <code>1</code> 组成的最长连续子字符串的长度是 <code>2</code> ,由 <code>0</code> 组成的最长连续子字符串的长度是 <code>3</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>注意,如果字符串中不存在 <code>0</code> ,此时认为由 <code>0</code> 组成的最长连续子字符串的长度是 <code>0</code> 。字符串中不存在 <code>1</code> 的情况也适用此规则。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "1101"
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>
|
||||
由 <code>1</code> 组成的最长连续子字符串的长度是 2:"<strong>11</strong>01"
|
||||
由 <code>0</code> 组成的最长连续子字符串的长度是 1:"11<strong>0</strong>1"
|
||||
由 1 组成的子字符串更长,故返回 true 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "111000"
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>
|
||||
由 <code>1</code> 组成的最长连续子字符串的长度是 3:"<strong>111</strong>000"
|
||||
由<code> 0</code> 组成的最长连续子字符串的长度是 3:"111<strong>000</strong>"
|
||||
由 1 组成的子字符串不比由 0 组成的子字符串长,故返回 false 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "110100010"
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>
|
||||
由 <code>1</code> 组成的最长连续子字符串的长度是 2:"<strong>11</strong>0100010"
|
||||
由 <code>0</code> 组成的最长连续子字符串的长度是 3:"1101<strong>000</strong>10"
|
||||
由 1 组成的子字符串不比由 0 组成的子字符串长,故返回 false 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s[i]</code> 不是 <code>'0'</code> 就是 <code>'1'</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 。数组中唯一元素是那些只出现 <strong>恰好一次</strong> 的元素。</p>
|
||||
|
||||
<p>请你返回 <code>nums</code> 中唯一元素的 <strong>和</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,2,3,2]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>唯一元素为 [1,3] ,和为 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,1,1,1,1]
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>没有唯一元素,和为 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3 :</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,2,3,4,5]
|
||||
<b>输出:</b>15
|
||||
<b>解释:</b>唯一元素为 [1,2,3,4,5] ,和为 15 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>给你一个字符串 <code>s</code> ,如果可以将它分割成三个 <strong>非空</strong> 回文子字符串,那么返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p>当一个字符串正着读和反着读是一模一样的,就称其为 <strong>回文字符串</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "abcbdd"
|
||||
<b>输出:</b>true
|
||||
<strong>解释:</strong>"abcbdd" = "a" + "bcb" + "dd",三个子字符串都是回文的。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "bcbddxy"
|
||||
<b>输出:</b>false
|
||||
<strong>解释:</strong>s 没办法被分割成 3 个回文子字符串。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= s.length <= 2000</code></li>
|
||||
<li><code>s</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>给你一个整数 <code>n</code> 和一个整数数组 <code>rounds</code> 。有一条圆形赛道由 <code>n</code> 个扇区组成,扇区编号从 <code>1</code> 到 <code>n</code> 。现将在这条赛道上举办一场马拉松比赛,该马拉松全程由 <code>m</code> 个阶段组成。其中,第 <code>i</code> 个阶段将会从扇区 <code>rounds[i - 1]</code> 开始,到扇区 <code>rounds[i]</code> 结束。举例来说,第 <code>1</code> 阶段从 <code>rounds[0]</code> 开始,到 <code>rounds[1]</code> 结束。</p>
|
||||
|
||||
<p>请你以数组形式返回经过次数最多的那几个扇区,按扇区编号 <strong>升序</strong> 排列。</p>
|
||||
|
||||
<p>注意,赛道按扇区编号升序逆时针形成一个圆(请参见第一个示例)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/22/3rd45e.jpg" style="height: 341px; width: 433px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 4, rounds = [1,3,1,2]
|
||||
<strong>输出:</strong>[1,2]
|
||||
<strong>解释:</strong>本场马拉松比赛从扇区 1 开始。经过各个扇区的次序如下所示:
|
||||
1 --> 2 --> 3(阶段 1 结束)--> 4 --> 1(阶段 2 结束)--> 2(阶段 3 结束,即本场马拉松结束)
|
||||
其中,扇区 1 和 2 都经过了两次,它们是经过次数最多的两个扇区。扇区 3 和 4 都只经过了一次。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 2, rounds = [2,1,2,1,2,1,2,1,2]
|
||||
<strong>输出:</strong>[2]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 7, rounds = [1,3,5,7]
|
||||
<strong>输出:</strong>[1,2,3,4,5,6,7]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 100</code></li>
|
||||
<li><code>1 <= m <= 100</code></li>
|
||||
<li><code>rounds.length == m + 1</code></li>
|
||||
<li><code>1 <= rounds[i] <= n</code></li>
|
||||
<li><code>rounds[i] != rounds[i + 1]</code> ,其中 <code>0 <= i < m</code></li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>给你一个大小为 <code>m x n</code> 的整数矩阵 <code>isWater</code> ,它代表了一个由 <strong>陆地</strong> 和 <strong>水域</strong> 单元格组成的地图。</p>
|
||||
|
||||
<ul>
|
||||
<li>如果 <code>isWater[i][j] == 0</code> ,格子 <code>(i, j)</code> 是一个 <strong>陆地</strong> 格子。</li>
|
||||
<li>如果 <code>isWater[i][j] == 1</code> ,格子 <code>(i, j)</code> 是一个 <strong>水域</strong> 格子。</li>
|
||||
</ul>
|
||||
|
||||
<p>你需要按照如下规则给每个单元格安排高度:</p>
|
||||
|
||||
<ul>
|
||||
<li>每个格子的高度都必须是非负的。</li>
|
||||
<li>如果一个格子是 <strong>水域</strong> ,那么它的高度必须为 <code>0</code> 。</li>
|
||||
<li>任意相邻的格子高度差 <strong>至多</strong> 为 <code>1</code> 。当两个格子在正东、南、西、北方向上相互紧挨着,就称它们为相邻的格子。(也就是说它们有一条公共边)</li>
|
||||
</ul>
|
||||
|
||||
<p>找到一种安排高度的方案,使得矩阵中的最高高度值 <strong>最大</strong> 。</p>
|
||||
|
||||
<p>请你返回一个大小为 <code>m x n</code> 的整数矩阵 <code>height</code> ,其中 <code>height[i][j]</code> 是格子 <code>(i, j)</code> 的高度。如果有多种解法,请返回 <strong>任意一个</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-82045-am.png" style="width: 220px; height: 219px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>isWater = [[0,1],[0,0]]
|
||||
<b>输出:</b>[[1,0],[2,1]]
|
||||
<b>解释:</b>上图展示了给各个格子安排的高度。
|
||||
蓝色格子是水域格,绿色格子是陆地格。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-82050-am.png" style="width: 300px; height: 296px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>isWater = [[0,0,1],[1,0,0],[0,0,0]]
|
||||
<b>输出:</b>[[1,1,0],[0,1,1],[1,2,2]]
|
||||
<b>解释:</b>所有安排方案中,最高可行高度为 2 。
|
||||
任意安排方案中,只要最高高度为 2 且符合上述规则的,都为可行方案。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == isWater.length</code></li>
|
||||
<li><code>n == isWater[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 1000</code></li>
|
||||
<li><code>isWater[i][j]</code> 要么是 <code>0</code> ,要么是 <code>1</code> 。</li>
|
||||
<li>至少有 <strong>1</strong> 个水域格子。</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>给你一个 <strong>从 0 开始的排列</strong> <code>nums</code>(<strong>下标也从 0 开始</strong>)。请你构建一个 <strong>同样长度</strong> 的数组 <code>ans</code> ,其中,对于每个 <code>i</code>(<code>0 <= i < nums.length</code>),都满足 <code>ans[i] = nums[nums[i]]</code> 。返回构建好的数组 <code>ans</code> 。</p>
|
||||
|
||||
<p><strong>从 0 开始的排列</strong> <code>nums</code> 是一个由 <code>0</code> 到 <code>nums.length - 1</code>(<code>0</code> 和 <code>nums.length - 1</code> 也包含在内)的不同整数组成的数组。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [0,2,1,5,3,4]
|
||||
<strong>输出:</strong>[0,1,2,4,5,3]<strong>
|
||||
解释:</strong>数组 ans 构建如下:
|
||||
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
|
||||
= [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
|
||||
= [0,1,2,4,5,3]</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [5,0,1,2,3,4]
|
||||
<strong>输出:</strong>[4,5,0,1,2,3]
|
||||
<strong>解释:</strong>数组 ans 构建如下:
|
||||
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
|
||||
= [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]
|
||||
= [4,5,0,1,2,3]</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>nums</code> 中的元素 <strong>互不相同</strong></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>给你两个整数 <code>memory1</code> 和 <code>memory2</code> 分别表示两个内存条剩余可用内存的位数。现在有一个程序每秒递增的速度消耗着内存。</p>
|
||||
|
||||
<p>在第 <code>i</code> 秒(秒数从 1 开始),有 <code>i</code> 位内存被分配到 <strong>剩余内存较多</strong> 的内存条(如果两者一样多,则分配到第一个内存条)。如果两者剩余内存都不足 <code>i</code> 位,那么程序将 <b>意外退出</b> 。</p>
|
||||
|
||||
<p>请你返回一个数组,包含<em> </em><code>[crashTime, memory1<sub>crash</sub>, memory2<sub>crash</sub>]</code> ,其中 <code>crashTime</code>是程序意外退出的时间(单位为秒),<em> </em><code>memory1<sub>crash</sub></code><em> </em>和<em> </em><code>memory2<sub>crash</sub></code><em> </em>分别是两个内存条最后剩余内存的位数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>memory1 = 2, memory2 = 2
|
||||
<b>输出:</b>[3,1,0]
|
||||
<b>解释:</b>内存分配如下:
|
||||
- 第 1 秒,内存条 1 被占用 1 位内存。内存条 1 现在有 1 位剩余可用内存。
|
||||
- 第 2 秒,内存条 2 被占用 2 位内存。内存条 2 现在有 0 位剩余可用内存。
|
||||
- 第 3 秒,程序意外退出,两个内存条分别有 1 位和 0 位剩余可用内存。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>memory1 = 8, memory2 = 11
|
||||
<b>输出:</b>[6,0,4]
|
||||
<b>解释:</b>内存分配如下:
|
||||
- 第 1 秒,内存条 2 被占用 1 位内存,内存条 2 现在有 10 位剩余可用内存。
|
||||
- 第 2 秒,内存条 2 被占用 2 位内存,内存条 2 现在有 8 位剩余可用内存。
|
||||
- 第 3 秒,内存条 1 被占用 3 位内存,内存条 1 现在有 5 位剩余可用内存。
|
||||
- 第 4 秒,内存条 2 被占用 4 位内存,内存条 2 现在有 4 位剩余可用内存。
|
||||
- 第 5 秒,内存条 1 被占用 5 位内存,内存条 1 现在有 0 位剩余可用内存。
|
||||
- 第 6 秒,程序意外退出,两个内存条分别有 0 位和 4 位剩余可用内存。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= memory1, memory2 <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>给你一维空间的 <code>n</code> 个点,其中第 <code>i</code> 个点(编号从 <code>0</code> 到 <code>n-1</code>)位于 <code>x = i</code> 处,请你找到 <strong>恰好</strong> <code>k</code> <strong>个不重叠</strong> 线段且每个线段至少覆盖两个点的方案数。线段的两个端点必须都是 <strong>整数坐标</strong> 。这 <code>k</code> 个线段不需要全部覆盖全部 <code>n</code> 个点,且它们的端点 <strong>可以 </strong>重合。</p>
|
||||
|
||||
<p>请你返回 <code>k</code> 个不重叠线段的方案数。由于答案可能很大,请将结果对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/10/17/ex1.png" style="width: 179px; height: 222px;" />
|
||||
<pre>
|
||||
<b>输入:</b>n = 4, k = 2
|
||||
<b>输出:</b>5
|
||||
<strong>解释:
|
||||
</strong>如图所示,两个线段分别用红色和蓝色标出。
|
||||
上图展示了 5 种不同的方案 {(0,2),(2,3)},{(0,1),(1,3)},{(0,1),(2,3)},{(1,2),(2,3)},{(0,1),(1,2)} 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 3, k = 1
|
||||
<b>输出:</b>3
|
||||
<strong>解释:</strong>总共有 3 种不同的方案 {(0,1)}, {(0,2)}, {(1,2)} 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 30, k = 7
|
||||
<b>输出:</b>796297179
|
||||
<strong>解释:</strong>画 7 条线段的总方案数为 3796297200 种。将这个数对 10<sup>9</sup> + 7 取余得到 796297179 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 5, k = 3
|
||||
<b>输出:</b>7
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 3, k = 2
|
||||
<b>输出:</b>1</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 1000</code></li>
|
||||
<li><code>1 <= k <= n-1</code></li>
|
||||
</ul>
|
34
算法题(国内版)/problem (Chinese)/大餐计数 [count-good-meals].html
Normal file
34
算法题(国内版)/problem (Chinese)/大餐计数 [count-good-meals].html
Normal file
@@ -0,0 +1,34 @@
|
||||
<p><strong>大餐</strong> 是指 <strong>恰好包含两道不同餐品</strong> 的一餐,其美味程度之和等于 2 的幂。</p>
|
||||
|
||||
<p>你可以搭配 <strong>任意</strong> 两道餐品做一顿大餐。</p>
|
||||
|
||||
<p>给你一个整数数组 <code>deliciousness</code> ,其中 <code>deliciousness[i]</code> 是第 <code>i<sup></sup></code> 道餐品的美味程度,返回你可以用数组中的餐品做出的不同 <strong>大餐</strong> 的数量。结果需要对 <code>10<sup>9</sup> + 7</code> 取余。</p>
|
||||
|
||||
<p>注意,只要餐品下标不同,就可以认为是不同的餐品,即便它们的美味程度相同。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>deliciousness = [1,3,5,7,9]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>大餐的美味程度组合为 (1,3) 、(1,7) 、(3,5) 和 (7,9) 。
|
||||
它们各自的美味程度之和分别为 4 、8 、8 和 16 ,都是 2 的幂。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>deliciousness = [1,1,1,3,3,3,7]
|
||||
<strong>输出:</strong>15
|
||||
<strong>解释:</strong>大餐的美味程度组合为 3 种 (1,1) ,9 种 (1,3) ,和 3 种 (1,7) 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= deliciousness.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= deliciousness[i] <= 2<sup>20</sup></code></li>
|
||||
</ul>
|
73
算法题(国内版)/problem (Chinese)/奇偶树 [even-odd-tree].html
Normal file
73
算法题(国内版)/problem (Chinese)/奇偶树 [even-odd-tree].html
Normal file
@@ -0,0 +1,73 @@
|
||||
<p>如果一棵二叉树满足下述几个条件,则可以称为 <strong>奇偶树</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li>二叉树根节点所在层下标为 <code>0</code> ,根的子节点所在层下标为 <code>1</code> ,根的孙节点所在层下标为 <code>2</code> ,依此类推。</li>
|
||||
<li><strong>偶数下标</strong> 层上的所有节点的值都是 <strong>奇</strong> 整数,从左到右按顺序 <strong>严格递增</strong></li>
|
||||
<li><strong>奇数下标</strong> 层上的所有节点的值都是 <strong>偶</strong> 整数,从左到右按顺序 <strong>严格递减</strong></li>
|
||||
</ul>
|
||||
|
||||
<p>给你二叉树的根节点,如果二叉树为 <strong>奇偶树 </strong>,则返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/10/04/sample_1_1966.png" style="height: 229px; width: 362px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,10,4,3,null,7,9,12,8,6,null,null,2]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>每一层的节点值分别是:
|
||||
0 层:[1]
|
||||
1 层:[10,4]
|
||||
2 层:[3,7,9]
|
||||
3 层:[12,8,6,2]
|
||||
由于 0 层和 2 层上的节点值都是奇数且严格递增,而 1 层和 3 层上的节点值都是偶数且严格递减,因此这是一棵奇偶树。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/10/04/sample_2_1966.png" style="height: 167px; width: 363px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [5,4,2,3,3,7]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>每一层的节点值分别是:
|
||||
0 层:[5]
|
||||
1 层:[4,2]
|
||||
2 层:[3,3,7]
|
||||
2 层上的节点值不满足严格递增的条件,所以这不是一棵奇偶树。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/10/04/sample_1_333_1966.png" style="height: 167px; width: 363px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [5,9,1,3,5,7]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>1 层上的节点值应为偶数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1]
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [11,8,6,1,3,9,11,30,20,18,16,12,10,4,2,17]
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点数在范围 <code>[1, 10<sup>5</sup>]</code> 内</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
47
算法题(国内版)/problem (Chinese)/奇妙序列 [fancy-sequence].html
Normal file
47
算法题(国内版)/problem (Chinese)/奇妙序列 [fancy-sequence].html
Normal file
@@ -0,0 +1,47 @@
|
||||
<p>请你实现三个 API <code>append</code>,<code>addAll</code> 和 <code>multAll</code> 来实现奇妙序列。</p>
|
||||
|
||||
<p>请实现 <code>Fancy</code> 类 :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>Fancy()</code> 初始化一个空序列对象。</li>
|
||||
<li><code>void append(val)</code> 将整数 <code>val</code> 添加在序列末尾。</li>
|
||||
<li><code>void addAll(inc)</code> 将所有序列中的现有数值都增加 <code>inc</code> 。</li>
|
||||
<li><code>void multAll(m)</code> 将序列中的所有现有数值都乘以整数 <code>m</code> 。</li>
|
||||
<li><code>int getIndex(idx)</code> 得到下标为 <code>idx</code> 处的数值(下标从 0 开始),并将结果对 <code>10<sup>9</sup> + 7</code> 取余。如果下标大于等于序列的长度,请返回 <code>-1</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
["Fancy", "append", "addAll", "append", "multAll", "getIndex", "addAll", "append", "multAll", "getIndex", "getIndex", "getIndex"]
|
||||
[[], [2], [3], [7], [2], [0], [3], [10], [2], [0], [1], [2]]
|
||||
<strong>输出:</strong>
|
||||
[null, null, null, null, null, 10, null, null, null, 26, 34, 20]
|
||||
|
||||
<strong>解释:</strong>
|
||||
Fancy fancy = new Fancy();
|
||||
fancy.append(2); // 奇妙序列:[2]
|
||||
fancy.addAll(3); // 奇妙序列:[2+3] -> [5]
|
||||
fancy.append(7); // 奇妙序列:[5, 7]
|
||||
fancy.multAll(2); // 奇妙序列:[5*2, 7*2] -> [10, 14]
|
||||
fancy.getIndex(0); // 返回 10
|
||||
fancy.addAll(3); // 奇妙序列:[10+3, 14+3] -> [13, 17]
|
||||
fancy.append(10); // 奇妙序列:[13, 17, 10]
|
||||
fancy.multAll(2); // 奇妙序列:[13*2, 17*2, 10*2] -> [26, 34, 20]
|
||||
fancy.getIndex(0); // 返回 26
|
||||
fancy.getIndex(1); // 返回 34
|
||||
fancy.getIndex(2); // 返回 20
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= val, inc, m <= 100</code></li>
|
||||
<li><code>0 <= idx <= 10<sup>5</sup></code></li>
|
||||
<li>总共最多会有 <code>10<sup>5</sup></code> 次对 <code>append</code>,<code>addAll</code>,<code>multAll</code> 和 <code>getIndex</code> 的调用。</li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>给你一个奇怪的打印机,它有如下两个特殊的打印规则:</p>
|
||||
|
||||
<ul>
|
||||
<li>每一次操作时,打印机会用同一种颜色打印一个矩形的形状,每次打印会覆盖矩形对应格子里原本的颜色。</li>
|
||||
<li>一旦矩形根据上面的规则使用了一种颜色,那么 <strong>相同的颜色不能再被使用 </strong>。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个初始没有颜色的 <code>m x n</code> 的矩形 <code>targetGrid</code> ,其中 <code>targetGrid[row][col]</code> 是位置 <code>(row, col)</code> 的颜色。</p>
|
||||
|
||||
<p>如果你能按照上述规则打印出矩形<em> </em><code>targetGrid</code> ,请你返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/09/19/sample_1_1929.png" style="height: 138px; width: 483px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>targetGrid = [[1,1,1,1],[1,2,2,1],[1,2,2,1],[1,1,1,1]]
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/09/19/sample_2_1929.png" style="height: 290px; width: 483px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>targetGrid = [[1,1,1,1],[1,1,3,3],[1,1,3,4],[5,5,1,4]]
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>targetGrid = [[1,2,1],[2,1,2],[1,2,1]]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>没有办法得到 targetGrid ,因为每一轮操作使用的颜色互不相同。</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>targetGrid = [[1,1,1],[3,1,3]]
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == targetGrid.length</code></li>
|
||||
<li><code>n == targetGrid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 60</code></li>
|
||||
<li><code>1 <= targetGrid[row][col] <= 60</code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>给你一个正整数 <code>primeFactors</code> 。你需要构造一个正整数 <code>n</code> ,它满足以下条件:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>n</code> 质因数(质因数需要考虑重复的情况)的数目 <strong>不超过 </strong><code>primeFactors</code> 个。</li>
|
||||
<li><code>n</code> 好因子的数目最大化。如果 <code>n</code> 的一个因子可以被 <code>n</code> 的每一个质因数整除,我们称这个因子是 <strong>好因子</strong> 。比方说,如果 <code>n = 12</code> ,那么它的质因数为 <code>[2,2,3]</code> ,那么 <code>6</code> 和 <code>12</code> 是好因子,但 <code>3</code> 和 <code>4</code> 不是。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回 <code>n</code> 的好因子的数目。由于答案可能会很大,请返回答案对 <code>10<sup>9</sup> + 7</code> <b>取余</b> 的结果。</p>
|
||||
|
||||
<p>请注意,一个质数的定义是大于 <code>1</code> ,且不能被分解为两个小于该数的自然数相乘。一个数 <code>n</code> 的质因子是将 <code>n</code> 分解为若干个质因子,且它们的乘积为 <code>n</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>primeFactors = 5
|
||||
<strong>输出:</strong>6
|
||||
<b>解释:</b>200 是一个可行的 n 。
|
||||
它有 5 个质因子:[2,2,2,5,5] ,且有 6 个好因子:[10,20,40,50,100,200] 。
|
||||
不存在别的 n 有至多 5 个质因子,且同时有更多的好因子。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>primeFactors = 8
|
||||
<b>输出:</b>18
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= primeFactors <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>给你一个整数数组 <code>nums</code> <strong>(下标从 0 开始)</strong>和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>一个子数组 <code>(i, j)</code> 的 <strong>分数</strong> 定义为 <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code> 。一个 <strong>好</strong> 子数组的两个端点下标需要满足 <code>i <= k <= j</code> 。</p>
|
||||
|
||||
<p>请你返回 <strong>好</strong> 子数组的最大可能 <strong>分数</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,4,3,7,4,5], k = 3
|
||||
<b>输出:</b>15
|
||||
<b>解释:</b>最优子数组的左右端点下标是 (1, 5) ,分数为 min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [5,5,4,5,4,1,1,1], k = 0
|
||||
<b>输出:</b>20
|
||||
<b>解释:</b>最优子数组的左右端点下标是 (0, 4) ,分数为 min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= k < nums.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>一个数组的 <strong>最小乘积</strong> 定义为这个数组中 <strong>最小值</strong> <strong>乘以 </strong>数组的 <strong>和</strong> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,数组 <code>[3,2,5]</code> (最小值是 <code>2</code>)的最小乘积为 <code>2 * (3+2+5) = 2 * 10 = 20</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个正整数数组 <code>nums</code> ,请你返回 <code>nums</code> 任意 <strong>非空子数组</strong> 的<strong>最小乘积</strong> 的 <strong>最大值</strong> 。由于答案可能很大,请你返回答案对 <code>10<sup>9</sup> + 7</code> <strong>取余 </strong>的结果。</p>
|
||||
|
||||
<p>请注意,最小乘积的最大值考虑的是取余操作 <strong>之前</strong> 的结果。题目保证最小乘积的最大值在 <strong>不取余</strong> 的情况下可以用 <strong>64 位有符号整数</strong> 保存。</p>
|
||||
|
||||
<p><strong>子数组</strong> 定义为一个数组的 <strong>连续</strong> 部分。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,<strong>2,3,2</strong>]
|
||||
<b>输出:</b>14
|
||||
<b>解释:</b>最小乘积的最大值由子数组 [2,3,2] (最小值是 2)得到。
|
||||
2 * (2+3+2) = 2 * 7 = 14 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,<strong>3,3</strong>,1,2]
|
||||
<b>输出:</b>18
|
||||
<b>解释:</b>最小乘积的最大值由子数组 [3,3] (最小值是 3)得到。
|
||||
3 * (3+3) = 3 * 6 = 18 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,1,<strong>5,6,4</strong>,2]
|
||||
<b>输出:</b>60
|
||||
<b>解释:</b>最小乘积的最大值由子数组 [5,6,4] (最小值是 4)得到。
|
||||
4 * (5+6+4) = 4 * 15 = 60 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>给你一个字符串 <code>word</code> ,该字符串由数字和小写英文字母组成。</p>
|
||||
|
||||
<p>请你用空格替换每个不是数字的字符。例如,<code>"a123bc34d8ef34"</code> 将会变成 <code>" 123 34 8 34"</code> 。注意,剩下的这些整数为(相邻彼此至少有一个空格隔开):<code>"123"</code>、<code>"34"</code>、<code>"8"</code> 和 <code>"34"</code> 。</p>
|
||||
|
||||
<p>返回对 <code>word</code> 完成替换后形成的 <strong>不同</strong> 整数的数目。</p>
|
||||
|
||||
<p>只有当两个整数的 <strong>不含前导零</strong> 的十进制表示不同, 才认为这两个整数也不同。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "a<strong>123</strong>bc<strong>34</strong>d<strong>8</strong>ef<strong>34</strong>"
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>不同的整数有 "123"、"34" 和 "8" 。注意,"34" 只计数一次。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "leet<strong>1234</strong>code<strong>234</strong>"
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "a<strong>1</strong>b<strong>01</strong>c<strong>001</strong>"
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>"1"、"01" 和 "001" 视为同一个整数的十进制表示,因为在比较十进制值时会忽略前导零的存在。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 1000</code></li>
|
||||
<li><code>word</code> 由数字和小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>给你一个字符串 <code>num</code> ,表示一个大整数。请你在字符串 <code>num</code> 的所有 <strong>非空子字符串</strong> 中找出 <strong>值最大的奇数</strong> ,并以字符串形式返回。如果不存在奇数,则返回一个空字符串<em> </em><code>""</code><em> </em>。</p>
|
||||
|
||||
<p><strong>子字符串</strong> 是字符串中的一个连续的字符序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = "52"
|
||||
<strong>输出:</strong>"5"
|
||||
<strong>解释:</strong>非空子字符串仅有 "5"、"2" 和 "52" 。"5" 是其中唯一的奇数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = "4206"
|
||||
<strong>输出:</strong>""
|
||||
<strong>解释:</strong>在 "4206" 中不存在奇数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = "35427"
|
||||
<strong>输出:</strong>"35427"
|
||||
<strong>解释:</strong>"35427" 本身就是一个奇数。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>num</code> 仅由数字组成且不含前导零</li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>给你一个混合字符串 <code>s</code> ,请你返回 <code>s</code> 中 <strong>第二大 </strong>的数字,如果不存在第二大的数字,请你返回 <code>-1</code> 。</p>
|
||||
|
||||
<p><strong>混合字符串 </strong>由小写英文字母和数字组成。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "dfa12321afd"
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>出现在 s 中的数字包括 [1, 2, 3] 。第二大的数字是 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "abc1111"
|
||||
<b>输出:</b>-1
|
||||
<b>解释:</b>出现在 s 中的数字只包含 [1] 。没有第二大的数字。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 500</code></li>
|
||||
<li><code>s</code> 只包含小写英文字母和(或)数字。</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>如果字符串 <code>s</code> 中 <strong>不存在</strong> 两个不同字符 <strong>频次</strong> 相同的情况,就称 <code>s</code> 是 <strong>优质字符串</strong> 。</p>
|
||||
|
||||
<p>给你一个字符串 <code>s</code>,返回使 <code>s</code> 成为 <strong>优质字符串</strong> 需要删除的 <strong>最小</strong> 字符数。</p>
|
||||
|
||||
<p>字符串中字符的 <strong>频次</strong> 是该字符在字符串中的出现次数。例如,在字符串 <code>"aab"</code> 中,<code>'a'</code> 的频次是 <code>2</code>,而 <code>'b'</code> 的频次是 <code>1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aab"
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong><code>s</code> 已经是优质字符串。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aaabbbcc"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>可以删除两个 'b' , 得到优质字符串 "aaabcc" 。
|
||||
另一种方式是删除一个 'b' 和一个 'c' ,得到优质字符串 "aaabbc" 。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "ceabaacb"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>可以删除两个 'c' 得到优质字符串 "eabaab" 。
|
||||
注意,只需要关注结果字符串中仍然存在的字符。(即,频次为 0 的字符会忽略不计。)
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> 仅含小写英文字母</li>
|
||||
</ul>
|
@@ -0,0 +1,60 @@
|
||||
<p>给你一个任务数组 <code>tasks</code> ,其中 <code>tasks[i] = [actual<sub>i</sub>, minimum<sub>i</sub>]</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>actual<sub>i</sub></code> 是完成第 <code>i</code> 个任务 <strong>需要耗费</strong> 的实际能量。</li>
|
||||
<li><code>minimum<sub>i</sub></code> 是开始第 <code>i</code> 个任务前需要达到的最低能量。</li>
|
||||
</ul>
|
||||
|
||||
<p>比方说,如果任务为 <code>[10, 12]</code> 且你当前的能量为 <code>11</code> ,那么你不能开始这个任务。如果你当前的能量为 <code>13</code> ,你可以完成这个任务,且完成它后剩余能量为 <code>3</code> 。</p>
|
||||
|
||||
<p>你可以按照 <strong>任意顺序</strong> 完成任务。</p>
|
||||
|
||||
<p>请你返回完成所有任务的 <strong>最少</strong> 初始能量。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>tasks = [[1,2],[2,4],[4,8]]
|
||||
<b>输出:</b>8
|
||||
<strong>解释:</strong>
|
||||
一开始有 8 能量,我们按照如下顺序完成任务:
|
||||
- 完成第 3 个任务,剩余能量为 8 - 4 = 4 。
|
||||
- 完成第 2 个任务,剩余能量为 4 - 2 = 2 。
|
||||
- 完成第 1 个任务,剩余能量为 2 - 1 = 1 。
|
||||
注意到尽管我们有能量剩余,但是如果一开始只有 7 能量是不能完成所有任务的,因为我们无法开始第 3 个任务。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>tasks = [[1,3],[2,4],[10,11],[10,12],[8,9]]
|
||||
<b>输出:</b>32
|
||||
<strong>解释:</strong>
|
||||
一开始有 32 能量,我们按照如下顺序完成任务:
|
||||
- 完成第 1 个任务,剩余能量为 32 - 1 = 31 。
|
||||
- 完成第 2 个任务,剩余能量为 31 - 2 = 29 。
|
||||
- 完成第 3 个任务,剩余能量为 29 - 10 = 19 。
|
||||
- 完成第 4 个任务,剩余能量为 19 - 10 = 9 。
|
||||
- 完成第 5 个任务,剩余能量为 9 - 8 = 1 。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>tasks = [[1,7],[2,8],[3,9],[4,10],[5,11],[6,12]]
|
||||
<b>输出:</b>27
|
||||
<strong>解释:</strong>
|
||||
一开始有 27 能量,我们按照如下顺序完成任务:
|
||||
- 完成第 5 个任务,剩余能量为 27 - 5 = 22 。
|
||||
- 完成第 2 个任务,剩余能量为 22 - 2 = 20 。
|
||||
- 完成第 3 个任务,剩余能量为 20 - 3 = 17 。
|
||||
- 完成第 1 个任务,剩余能量为 17 - 1 = 16 。
|
||||
- 完成第 4 个任务,剩余能量为 16 - 4 = 12 。
|
||||
- 完成第 6 个任务,剩余能量为 12 - 6 = 6 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tasks.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= actual<sub>i</sub> <= minimum<sub>i</sub> <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user