1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-05 15:31:43 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
zhangbk1
2023-12-20 15:51:53 +08:00
parent 3770b44d1e
commit 359df08458
65 changed files with 20572 additions and 14095 deletions

View File

@@ -0,0 +1,53 @@
<p>给你一个长度为 <code>n</code>&nbsp;下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;</p>
<p>你可以对 <code>nums</code>&nbsp;执行特殊操作 <strong>任意次</strong>&nbsp;(也可以 <strong>0</strong>&nbsp;次)。每一次特殊操作中,你需要 <strong>按顺序</strong>&nbsp;执行以下步骤:</p>
<ul>
<li>从范围&nbsp;<code>[0, n - 1]</code>&nbsp;里选择一个下标 <code>i</code>&nbsp;和一个 <strong></strong>&nbsp;整数&nbsp;<code>x</code>&nbsp;</li>
<li>&nbsp;<code>|nums[i] - x|</code>&nbsp;添加到总代价里。</li>
<li><code>nums[i]</code>&nbsp;变为&nbsp;<code>x</code>&nbsp;</li>
</ul>
<p>如果一个正整数正着读和反着读都相同,那么我们称这个数是<strong>&nbsp;回文数</strong>&nbsp;。比方说,<code>121</code>&nbsp;<code>2552</code>&nbsp;<code>65756</code>&nbsp;都是回文数,但是&nbsp;<code>24</code>&nbsp;<code>46</code>&nbsp;<code>235</code>&nbsp;都不是回文数。</p>
<p>如果一个数组中的所有元素都等于一个整数&nbsp;<code>y</code>&nbsp;,且&nbsp;<code>y</code>&nbsp;是一个小于&nbsp;<code>10<sup>9</sup></code>&nbsp;&nbsp;<strong>回文数</strong>&nbsp;,那么我们称这个数组是一个 <strong>等数数组&nbsp;</strong></p>
<p>请你返回一个整数,表示执行任意次特殊操作后使 <code>nums</code>&nbsp;成为 <strong>等数数组</strong>&nbsp;<strong>最小</strong>&nbsp;总代价。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>nums = [1,2,3,4,5]
<b>输出:</b>6
<b>解释:</b>我们可以将数组中所有元素变为回文数 3 得到等数数组,数组变成 [3,3,3,3,3] 需要执行 4 次特殊操作,代价为 |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6 。
将所有元素变为其他回文数的总代价都大于 6 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>nums = [10,12,13,14,15]
<b>输出:</b>11
<b>解释:</b>我们可以将数组中所有元素变为回文数 11 得到等数数组,数组变成 [11,11,11,11,11] 需要执行 5 次特殊操作,代价为 |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11 。
将所有元素变为其他回文数的总代价都大于 11 。
</pre>
<p><strong class="example">示例 3 </strong></p>
<pre>
<b>输入:</b>nums = [22,33,22,33,22]
<b>输出:</b>22
<b>解释:</b>我们可以将数组中所有元素变为回文数 22 得到等数数组,数组变为 [22,22,22,22,22] 需要执行 2 次特殊操作,代价为 |33 - 22| + |33 - 22| = 22 。
将所有元素变为其他回文数的总代价都大于 22 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,75 @@
<p>一个公司在全国有 <code>n</code>&nbsp;个分部,它们之间有的有道路连接。一开始,所有分部通过这些道路两两之间互相可以到达。</p>
<p>公司意识到在分部之间旅行花费了太多时间,所以它们决定关闭一些分部(<b>也可能不关闭任何分部</b>),同时保证剩下的分部之间两两互相可以到达且最远距离不超过&nbsp;<code>maxDistance</code>&nbsp;</p>
<p>两个分部之间的 <strong>距离</strong> 是通过道路长度之和的 <strong>最小值</strong>&nbsp;</p>
<p>给你整数&nbsp;<code>n</code>&nbsp;<code>maxDistance</code>&nbsp;和下标从 <strong>0</strong>&nbsp;开始的二维整数数组&nbsp;<code>roads</code>&nbsp;,其中&nbsp;<code>roads[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code>&nbsp;表示一条从&nbsp;<code>u<sub>i</sub></code>&nbsp;&nbsp;<code>v<sub>i</sub></code>&nbsp;长度为&nbsp;<code>w<sub>i</sub></code>&nbsp;<strong>无向</strong>&nbsp;道路。</p>
<p>请你返回关闭分部的可行方案数目,满足每个方案里剩余分部之间的最远距离不超过<em>&nbsp;</em><code>maxDistance</code></p>
<p><strong>注意</strong>,关闭一个分部后,与之相连的所有道路不可通行。</p>
<p><b>注意</b>,两个分部之间可能会有多条道路。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/11/08/example11.png" style="width: 221px; height: 191px;" /></p>
<pre>
<b>输入:</b>n = 3, maxDistance = 5, roads = [[0,1,2],[1,2,10],[0,2,10]]
<b>输出:</b>5
<b>解释:</b>可行的关闭分部方案有:
- 关闭分部集合 [2] ,剩余分部为 [0,1] ,它们之间的距离为 2 。
- 关闭分部集合 [0,1] ,剩余分部为 [2] 。
- 关闭分部集合 [1,2] ,剩余分部为 [0] 。
- 关闭分部集合 [0,2] ,剩余分部为 [1] 。
- 关闭分部集合 [0,1,2] ,关闭后没有剩余分部。
总共有 5 种可行的关闭方案。
</pre>
<p><strong class="example">示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/11/08/example22.png" style="width: 221px; height: 241px;" /></p>
<pre>
<b>输入:</b>n = 3, maxDistance = 5, roads = [[0,1,20],[0,1,10],[1,2,2],[0,2,2]]
<b>输出:</b>7
<b>解释:</b>可行的关闭分部方案有:
- 关闭分部集合 [] ,剩余分部为 [0,1,2] ,它们之间的最远距离为 4 。
- 关闭分部集合 [0] ,剩余分部为 [1,2] ,它们之间的距离为 2 。
- 关闭分部集合 [1] ,剩余分部为 [0,2] ,它们之间的距离为 2 。
- 关闭分部集合 [0,1] ,剩余分部为 [2] 。
- 关闭分部集合 [1,2] ,剩余分部为 [0] 。
- 关闭分部集合 [0,2] ,剩余分部为 [1] 。
- 关闭分部集合 [0,1,2] ,关闭后没有剩余分部。
总共有 7 种可行的关闭方案。
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>n = 1, maxDistance = 10, roads = []
<b>输出:</b>2
<b>解释:</b>可行的关闭分部方案有:
- 关闭分部集合 [] ,剩余分部为 [0] 。
- 关闭分部集合 [0] ,关闭后没有剩余分部。
总共有 2 种可行的关闭方案。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10</code></li>
<li><code>1 &lt;= maxDistance &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= roads.length &lt;= 1000</code></li>
<li><code>roads[i].length == 3</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li><code>1 &lt;= w<sub>i</sub> &lt;= 1000</code></li>
<li>一开始所有分部之间通过道路互相可以到达。</li>
</ul>

View File

@@ -0,0 +1,42 @@
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code>,以及一个正整数 <code>k</code></p>
<p>将这个数组划分为一个或多个长度为 <code>3</code> 的子数组,并满足以下条件:</p>
<ul>
<li><code>nums</code> 中的 <strong>每个 </strong>元素都必须 <strong>恰好 </strong>存在于某个子数组中。</li>
<li>子数组中<strong> 任意 </strong>两个元素的差必须小于或等于 <code>k</code></li>
</ul>
<p>返回一个<em> </em><strong>二维数组 </strong>,包含所有的子数组。如果不可能满足条件,就返回一个空数组。如果有多个答案,返回 <strong>任意一个</strong> 即可。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [1,3,4,8,7,9,3,5,1], k = 2
<strong>输出:</strong>[[1,1,3],[3,4,5],[7,8,9]]
<strong>解释:</strong>可以将数组划分为以下子数组:[1,1,3][3,4,5] 和 [7,8,9] 。
每个子数组中任意两个元素的差都小于或等于 2 。
注意,元素的顺序并不重要。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [1,3,3,2,7,3], k = 3
<strong>输出:</strong>[]
<strong>解释:</strong>无法划分数组满足所有条件。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>n</code><code>3</code> 的倍数</li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@@ -0,0 +1,45 @@
<p>给你一个下标从 <strong>0 </strong>开始的二维数组 <code>variables</code> ,其中 <code>variables[i] = [a<sub>i</sub>, b<sub>i</sub>, c<sub>i,</sub> m<sub>i</sub>]</code>,以及一个整数 <code>target</code></p>
<p>如果满足以下公式,则下标 <code>i</code><strong>好下标</strong></p>
<ul>
<li><code>0 &lt;= i &lt; variables.length</code></li>
<li><code>((a<sub>i</sub><sup>b<sub>i</sub></sup> % 10)<sup>c<sub>i</sub></sup>) % m<sub>i</sub> == target</code></li>
</ul>
<p>返回一个由<strong> 好下标 </strong>组成的数组,<strong>顺序不限</strong></p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<strong>输入:</strong>variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2
<strong>输出:</strong>[0,2]
<strong>解释:</strong>对于 variables 数组中的每个下标 i
1) 对于下标 0 variables[0] = [2,3,3,10] (2<sup>3</sup> % 10)<sup>3</sup> % 10 = 2 。
2) 对于下标 1 variables[1] = [3,3,3,1] (3<sup>3</sup> % 10)<sup>3</sup> % 1 = 0 。
3) 对于下标 2 variables[2] = [6,1,1,4] (6<sup>1</sup> % 10)<sup>1</sup> % 4 = 2 。
因此,返回 [0,2] 作为答案。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<strong>输入:</strong>variables = [[39,3,1000,1000]], target = 17
<strong>输出:</strong>[]
<strong>解释:</strong>对于 variables 数组中的每个下标 i
1) 对于下标 0 variables[0] = [39,3,1000,1000] (39<sup>3</sup> % 10)<sup>1000</sup> % 1000 = 1 。
因此,返回 [] 作为答案。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= variables.length &lt;= 100</code></li>
<li><code>variables[i] == [a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>, m<sub>i</sub>]</code></li>
<li><code>1 &lt;= a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>, m<sub>i</sub> &lt;= 10<sup>3</sup></code></li>
<li><code><font face="monospace">0 &lt;= target &lt;= 10<sup>3</sup></font></code></li>
</ul>

View File

@@ -0,0 +1,46 @@
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;</p>
<p>你可以对数组执行 <strong>至多</strong>&nbsp;<code>k</code>&nbsp;次操作:</p>
<ul>
<li>从数组中选择一个下标 <code>i</code>&nbsp;,将&nbsp;<code>nums[i]</code> <strong>增加</strong>&nbsp;或者&nbsp;<strong>减少</strong>&nbsp;<code>1</code>&nbsp;</li>
</ul>
<p>最终数组的频率分数定义为数组中众数的 <strong>频率</strong>&nbsp;</p>
<p>请你返回你可以得到的 <strong>最大</strong>&nbsp;频率分数。</p>
<p>众数指的是数组中出现次数最多的数。一个元素的频率指的是数组中这个元素的出现次数。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>nums = [1,2,6,4], k = 3
<b>输出:</b>3
<b>解释:</b>我们可以对数组执行以下操作:
- 选择 i = 0 ,将 nums[0] 增加 1 。得到数组 [2,2,6,4] 。
- 选择 i = 3 ,将 nums[3] 减少 1 ,得到数组 [2,2,6,3] 。
- 选择 i = 3 ,将 nums[3] 减少 1 ,得到数组 [2,2,6,2] 。
元素 2 是最终数组中的众数,出现了 3 次,所以频率分数为 3 。
3 是所有可行方案里的最大频率分数。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>nums = [1,4,4,2,4], k = 0
<b>输出:</b>3
<b>解释:</b>我们无法执行任何操作,所以得到的频率分数是原数组中众数的频率 3 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>0 &lt;= k &lt;= 10<sup>14</sup></code></li>
</ul>

View File

@@ -0,0 +1,35 @@
<p>给你一个下标从<strong> 0 </strong>开始的二维整数矩阵 <code><font face="monospace">grid</font></code>,大小为 <code>n * n</code> ,其中的值在 <code>[1, n<sup>2</sup>]</code> 范围内。除了 <code>a</code> 出现 <strong>两次</strong><code>b</code> <strong>缺失</strong> 之外,每个整数都<strong> 恰好出现一次</strong></p>
<p>任务是找出重复的数字<code>a</code> 和缺失的数字 <code>b</code></p>
<p>返回一个下标从 0 开始、长度为 <code>2</code> 的整数数组 <code>ans</code> ,其中 <code>ans[0]</code> 等于 <code>a</code> <code>ans[1]</code> 等于 <code>b</code></p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<strong>输入:</strong>grid = [[1,3],[2,2]]
<strong>输出:</strong>[2,4]
<strong>解释:</strong>数字 2 重复,数字 4 缺失,所以答案是 [2,4] 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<strong>输入:</strong>grid = [[9,1,7],[8,9,2],[3,4,6]]
<strong>输出:</strong>[9,5]
<strong>解释:</strong>数字 9 重复,数字 5 缺失,所以答案是 [9,5] 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= n == grid.length == grid[i].length &lt;= 50</code></li>
<li><code>1 &lt;= grid[i][j] &lt;= n * n</code></li>
<li>对于所有满足<code>1 &lt;= x &lt;= n * n</code><code>x</code> ,恰好存在一个 <code>x</code> 与矩阵中的任何成员都不相等。</li>
<li>对于所有满足<code>1 &lt;= x &lt;= n * n</code><code>x</code> ,恰好存在一个 <code>x</code> 与矩阵中的两个成员相等。</li>
<li>除上述的两个之外,对于所有满足<code>1 &lt;= x &lt;= n * n</code><code>x</code> ,都恰好存在一对 <code>i, j</code> 满足 <code>0 &lt;= i, j &lt;= n - 1</code><code>grid[i][j] == x</code></li>
</ul>

View File

@@ -0,0 +1,41 @@
<p>给你两个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums1</code>&nbsp;&nbsp;<code>nums2</code>&nbsp;,它们分别含有 <code>n</code>&nbsp;<code>m</code>&nbsp;个元素。</p>
<p>请你计算以下两个数值:</p>
<ul>
<li>统计&nbsp;<code>0 &lt;= i &lt; n</code>&nbsp;中的下标&nbsp;<code>i</code>&nbsp;,满足&nbsp;<code>nums1[i]</code>&nbsp;<code>nums2</code>&nbsp;<strong>至少</strong>&nbsp;出现了一次。</li>
<li>统计&nbsp;<code>0 &lt;= i &lt; m</code>&nbsp;中的下标&nbsp;<code>i</code>&nbsp;,满足&nbsp;<code>nums2[i]</code>&nbsp;<code>nums1</code>&nbsp;<strong>至少</strong>&nbsp;出现了一次。</li>
</ul>
<p>请你返回一个长度为 <code>2</code>&nbsp;的整数数组<em>&nbsp;</em><code>answer</code>&nbsp;<strong>按顺序</strong>&nbsp;分别为以上两个数值。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<strong>输入:</strong>nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
<b>输出:</b>[3,4]
<b>解释:</b>分别计算两个数值:
- nums1 中下标为 1 2 和 3 的元素在 nums2 中至少出现了一次,所以第一个值为 3 。
- nums2 中下标为 0 1 3 和 4 的元素在 nums1 中至少出现了一次,所以第二个值为 4 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>nums1 = [3,4,2,3], nums2 = [1,5]
<b>输出:</b>[0,0]
<b>解释:</b>两个数组中没有公共元素,所以两个值都为 0 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>m == nums2.length</code></li>
<li><code>1 &lt;= n, m &lt;= 100</code></li>
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 100</code></li>
</ul>

View File

@@ -0,0 +1,48 @@
<p>给你一个整数数组&nbsp;<code>nums</code>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;</p>
<p>一个元素 <code>x</code>&nbsp;在数组中的 <strong>频率</strong>&nbsp;指的是它在数组中的出现次数。</p>
<p>如果一个数组中所有元素的频率都 <strong>小于等于&nbsp;</strong><code>k</code>&nbsp;,那么我们称这个数组是 <strong></strong>&nbsp;数组。</p>
<p>请你返回 <code>nums</code>&nbsp;<strong>最长好</strong>&nbsp;子数组的长度。</p>
<p><strong>子数组</strong> 指的是一个数组中一段连续非空的元素序列。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>nums = [1,2,3,1,2,3,1,2], k = 2
<b>输出:</b>6
<strong>解释:</strong>最长好子数组是 [1,2,3,1,2,3] ,值 1 2 和 3 在子数组中的频率都没有超过 k = 2 。[2,3,1,2,3,1] 和 [3,1,2,3,1,2] 也是好子数组。
最长好子数组的长度为 6 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,1,2,1,2,1,2], k = 1
<b>输出:</b>2
<b>解释:</b>最长好子数组是 [1,2] ,值 1 和 2 在子数组中的频率都没有超过 k = 1 。[2,1] 也是好子数组。
最长好子数组的长度为 2 。
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>nums = [5,5,5,5,5,5,5], k = 4
<b>输出:</b>4
<b>解释:</b>最长好子数组是 [5,5,5,5] ,值 5 在子数组中的频率没有超过 k = 4 。
最长好子数组的长度为 4 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>

View File

@@ -0,0 +1,44 @@
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的字符串&nbsp;<code>word</code>&nbsp;</p>
<p>一次操作中,你可以选择&nbsp;<code>word</code>&nbsp;中任意一个下标 <code>i</code>&nbsp;,将&nbsp;<code>word[i]</code> 修改成任意一个小写英文字母。</p>
<p>请你返回消除 <code>word</code>&nbsp;中所有相邻 <strong>近似相等</strong>&nbsp;字符的 <strong>最少</strong>&nbsp;操作次数。</p>
<p>两个字符&nbsp;<code>a</code>&nbsp;<code>b</code>&nbsp;如果满足&nbsp;<code>a == b</code>&nbsp;或者&nbsp;<code>a</code>&nbsp;<code>b</code>&nbsp;在字母表中是相邻的,那么我们称它们是 <strong>近似相等</strong>&nbsp;字符。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>word = "aaaaa"
<b>输出:</b>2
<b>解释:</b>我们将 word 变为 "a<em><strong>c</strong></em>a<em><strong>c</strong></em>a" ,该字符串没有相邻近似相等字符。
消除 word 中所有相邻近似相等字符最少需要 2 次操作。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>word = "abddez"
<b>输出:</b>2
<b>解释:</b>我们将 word 变为 "<em><strong>y</strong></em>bd<em><strong>o</strong></em>ez" ,该字符串没有相邻近似相等字符。
消除 word 中所有相邻近似相等字符最少需要 2 次操作。</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>word = "zyxyxyz"
<b>输出:</b>3
<b>解释:</b>我们将 word 变为 "z<em><strong>a</strong></em>x<em><strong>a</strong></em>x<em><strong>a</strong></em>z" ,该字符串没有相邻近似相等字符。
消除 word 中所有相邻近似相等字符最少需要 3 次操作
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= word.length &lt;= 100</code></li>
<li><code>word</code>&nbsp;只包含小写英文字母。</li>
</ul>

View File

@@ -0,0 +1,42 @@
<p>给你一个下标从 <strong>0</strong> 开始、由 <strong>正整数</strong> 组成的数组 <code>nums</code></p>
<p>将数组分割成一个或多个<strong> 连续</strong> 子数组,如果不存在包含了相同数字的两个子数组,则认为是一种 <strong>好分割方案</strong></p>
<p>返回 <code>nums</code><strong>好分割方案</strong><strong>数目</strong></p>
<p>由于答案可能很大,请返回答案对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 的结果。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,3,4]
<strong>输出:</strong>8
<strong>解释:</strong>有 8 种 <strong>好分割方案 </strong>([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]) 和 ([1,2,3,4]) 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [1,1,1,1]
<strong>输出:</strong>1
<strong>解释:</strong>唯一的 <strong>好分割方案</strong> 是:([1,1,1,1]) 。
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,1,3]
<strong>输出:</strong>2
<strong>解释:</strong>有 2 种<strong> 好分割方案 </strong>([1,2,1], [3]) 和 ([1,2,1,3]) 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,54 @@
<p>给你一个长度为 <code>n</code> 、下标从<strong> 0 </strong>开始的整数数组 <code>batteryPercentages</code> ,表示 <code>n</code> 个设备的电池百分比。</p>
<p>你的任务是按照顺序测试每个设备 <code>i</code>,执行以下测试操作:</p>
<ul>
<li>如果 <code>batteryPercentages[i]</code> <strong>大于</strong> <code>0</code>
<ul>
<li><strong>增加</strong> 已测试设备的计数。</li>
<li>将下标在 <code>[i + 1, n - 1]</code> 的所有设备的电池百分比减少 <code>1</code>,确保它们的电池百分比<strong> 不会低于</strong> <code>0</code> ,即 <code>batteryPercentages[j] = max(0, batteryPercentages[j] - 1)</code></li>
<li>移动到下一个设备。</li>
</ul>
</li>
<li>否则,移动到下一个设备而不执行任何测试。</li>
</ul>
<p>返回一个整数,表示按顺序执行测试操作后 <strong>已测试设备</strong> 的数量。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<strong>输入:</strong>batteryPercentages = [1,1,2,1,3]
<strong>输出:</strong>3
<strong>解释:</strong>按顺序从设备 0 开始执行测试操作:
在设备 0 上batteryPercentages[0] &gt; 0 ,现在有 1 个已测试设备batteryPercentages 变为 [1,0,1,0,2] 。
在设备 1 上batteryPercentages[1] == 0 ,移动到下一个设备而不进行测试。
在设备 2 上batteryPercentages[2] &gt; 0 ,现在有 2 个已测试设备batteryPercentages 变为 [1,0,1,0,1] 。
在设备 3 上batteryPercentages[3] == 0 ,移动到下一个设备而不进行测试。
在设备 4 上batteryPercentages[4] &gt; 0 ,现在有 3 个已测试设备batteryPercentages 保持不变。
因此,答案是 3 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<strong>输入:</strong>batteryPercentages = [0,1,2]
<strong>输出:</strong>2
<strong>解释:</strong>按顺序从设备 0 开始执行测试操作:
在设备 0 上batteryPercentages[0] == 0 ,移动到下一个设备而不进行测试。
在设备 1 上batteryPercentages[1] &gt; 0 ,现在有 1 个已测试设备batteryPercentages 变为 [0,1,1] 。
在设备 2 上batteryPercentages[2] &gt; 0 ,现在有 2 个已测试设备batteryPercentages 保持不变。
因此,答案是 2 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n == batteryPercentages.length &lt;= 100 </code></li>
<li><code>0 &lt;= batteryPercentages[i] &lt;= 100</code></li>
</ul>

View File

@@ -0,0 +1,33 @@
<p>给你一个整数数组 <code>nums</code> 和一个 <strong>正整数</strong> <code>k</code></p>
<p>请你统计有多少满足 「&nbsp;<code>nums</code> 中的 <strong>最大</strong> 元素」至少出现 <code>k</code> 次的子数组,并返回满足这一条件的子数组的数目。</p>
<p>子数组是数组中的一个连续元素序列。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [1,3,2,3,3], k = 2
<strong>输出:</strong>6
<strong>解释:</strong>包含元素 3 至少 2 次的子数组为:[1,3,2,3]、[1,3,2,3,3]、[3,2,3]、[3,2,3,3]、[2,3,3] 和 [3,3] 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [1,4,2,1], k = 3
<strong>输出:</strong>0
<strong>解释:</strong>没有子数组包含元素 4 至少 3 次。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
<li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>