mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 10:38:13 +08:00
update
This commit is contained in:
parent
3770b44d1e
commit
359df08458
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2023.12.09**
|
||||
> 最后更新日期: **2023.12.20**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
190
leetcode-cn/originData/count-the-number-of-good-partitions.json
Normal file
190
leetcode-cn/originData/count-the-number-of-good-partitions.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
180
leetcode-cn/originData/double-modular-exponentiation.json
Normal file
180
leetcode-cn/originData/double-modular-exponentiation.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
161
leetcode-cn/originData/find-missing-and-repeated-values.json
Normal file
161
leetcode-cn/originData/find-missing-and-repeated-values.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,53 @@
|
||||
<p>给你一个长度为 <code>n</code> 下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>你可以对 <code>nums</code> 执行特殊操作 <strong>任意次</strong> (也可以 <strong>0</strong> 次)。每一次特殊操作中,你需要 <strong>按顺序</strong> 执行以下步骤:</p>
|
||||
|
||||
<ul>
|
||||
<li>从范围 <code>[0, n - 1]</code> 里选择一个下标 <code>i</code> 和一个 <strong>正</strong> 整数 <code>x</code> 。</li>
|
||||
<li>将 <code>|nums[i] - x|</code> 添加到总代价里。</li>
|
||||
<li>将 <code>nums[i]</code> 变为 <code>x</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果一个正整数正着读和反着读都相同,那么我们称这个数是<strong> 回文数</strong> 。比方说,<code>121</code> ,<code>2552</code> 和 <code>65756</code> 都是回文数,但是 <code>24</code> ,<code>46</code> ,<code>235</code> 都不是回文数。</p>
|
||||
|
||||
<p>如果一个数组中的所有元素都等于一个整数 <code>y</code> ,且 <code>y</code> 是一个小于 <code>10<sup>9</sup></code> 的 <strong>回文数</strong> ,那么我们称这个数组是一个 <strong>等数数组 </strong>。</p>
|
||||
|
||||
<p>请你返回一个整数,表示执行任意次特殊操作后使 <code>nums</code> 成为 <strong>等数数组</strong> 的 <strong>最小</strong> 总代价。</p>
|
||||
|
||||
<p> </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> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,75 @@
|
||||
<p>一个公司在全国有 <code>n</code> 个分部,它们之间有的有道路连接。一开始,所有分部通过这些道路两两之间互相可以到达。</p>
|
||||
|
||||
<p>公司意识到在分部之间旅行花费了太多时间,所以它们决定关闭一些分部(<b>也可能不关闭任何分部</b>),同时保证剩下的分部之间两两互相可以到达且最远距离不超过 <code>maxDistance</code> 。</p>
|
||||
|
||||
<p>两个分部之间的 <strong>距离</strong> 是通过道路长度之和的 <strong>最小值</strong> 。</p>
|
||||
|
||||
<p>给你整数 <code>n</code> ,<code>maxDistance</code> 和下标从 <strong>0</strong> 开始的二维整数数组 <code>roads</code> ,其中 <code>roads[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> 表示一条从 <code>u<sub>i</sub></code> 到 <code>v<sub>i</sub></code> 长度为 <code>w<sub>i</sub></code>的 <strong>无向</strong> 道路。</p>
|
||||
|
||||
<p>请你返回关闭分部的可行方案数目,满足每个方案里剩余分部之间的最远距离不超过<em> </em><code>maxDistance</code>。</p>
|
||||
|
||||
<p><strong>注意</strong>,关闭一个分部后,与之相连的所有道路不可通行。</p>
|
||||
|
||||
<p><b>注意</b>,两个分部之间可能会有多条道路。</p>
|
||||
|
||||
<p> </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> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10</code></li>
|
||||
<li><code>1 <= maxDistance <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= roads.length <= 1000</code></li>
|
||||
<li><code>roads[i].length == 3</code></li>
|
||||
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
|
||||
<li><code>1 <= w<sub>i</sub> <= 1000</code></li>
|
||||
<li>一开始所有分部之间通过道路互相可以到达。</li>
|
||||
</ul>
|
@ -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> </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> </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>n</code> 是 <code>3</code> 的倍数</li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -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 <= i < 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> </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> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= variables.length <= 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 <= a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>, m<sub>i</sub> <= 10<sup>3</sup></code></li>
|
||||
<li><code><font face="monospace">0 <= target <= 10<sup>3</sup></font></code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>你可以对数组执行 <strong>至多</strong> <code>k</code> 次操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>从数组中选择一个下标 <code>i</code> ,将 <code>nums[i]</code> <strong>增加</strong> 或者 <strong>减少</strong> <code>1</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>最终数组的频率分数定义为数组中众数的 <strong>频率</strong> 。</p>
|
||||
|
||||
<p>请你返回你可以得到的 <strong>最大</strong> 频率分数。</p>
|
||||
|
||||
<p>众数指的是数组中出现次数最多的数。一个元素的频率指的是数组中这个元素的出现次数。</p>
|
||||
|
||||
<p> </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> </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>0 <= k <= 10<sup>14</sup></code></li>
|
||||
</ul>
|
@ -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> </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> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == grid.length == grid[i].length <= 50</code></li>
|
||||
<li><code>1 <= grid[i][j] <= n * n</code></li>
|
||||
<li>对于所有满足<code>1 <= x <= n * n</code> 的 <code>x</code> ,恰好存在一个 <code>x</code> 与矩阵中的任何成员都不相等。</li>
|
||||
<li>对于所有满足<code>1 <= x <= n * n</code> 的 <code>x</code> ,恰好存在一个 <code>x</code> 与矩阵中的两个成员相等。</li>
|
||||
<li>除上述的两个之外,对于所有满足<code>1 <= x <= n * n</code> 的 <code>x</code> ,都恰好存在一对 <code>i, j</code> 满足 <code>0 <= i, j <= n - 1</code> 且 <code>grid[i][j] == x</code> 。</li>
|
||||
</ul>
|
@ -0,0 +1,41 @@
|
||||
<p>给你两个下标从 <strong>0</strong> 开始的整数数组 <code>nums1</code> 和 <code>nums2</code> ,它们分别含有 <code>n</code> 和 <code>m</code> 个元素。</p>
|
||||
|
||||
<p>请你计算以下两个数值:</p>
|
||||
|
||||
<ul>
|
||||
<li>统计 <code>0 <= i < n</code> 中的下标 <code>i</code> ,满足 <code>nums1[i]</code> 在 <code>nums2</code> 中 <strong>至少</strong> 出现了一次。</li>
|
||||
<li>统计 <code>0 <= i < m</code> 中的下标 <code>i</code> ,满足 <code>nums2[i]</code> 在 <code>nums1</code> 中 <strong>至少</strong> 出现了一次。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回一个长度为 <code>2</code> 的整数数组<em> </em><code>answer</code> ,<strong>按顺序</strong> 分别为以上两个数值。</p>
|
||||
|
||||
<p> </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> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length</code></li>
|
||||
<li><code>m == nums2.length</code></li>
|
||||
<li><code>1 <= n, m <= 100</code></li>
|
||||
<li><code>1 <= nums1[i], nums2[i] <= 100</code></li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>一个元素 <code>x</code> 在数组中的 <strong>频率</strong> 指的是它在数组中的出现次数。</p>
|
||||
|
||||
<p>如果一个数组中所有元素的频率都 <strong>小于等于 </strong><code>k</code> ,那么我们称这个数组是 <strong>好</strong> 数组。</p>
|
||||
|
||||
<p>请你返回 <code>nums</code> 中 <strong>最长好</strong> 子数组的长度。</p>
|
||||
|
||||
<p><strong>子数组</strong> 指的是一个数组中一段连续非空的元素序列。</p>
|
||||
|
||||
<p> </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> </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 <= nums.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>word</code> 。</p>
|
||||
|
||||
<p>一次操作中,你可以选择 <code>word</code> 中任意一个下标 <code>i</code> ,将 <code>word[i]</code> 修改成任意一个小写英文字母。</p>
|
||||
|
||||
<p>请你返回消除 <code>word</code> 中所有相邻 <strong>近似相等</strong> 字符的 <strong>最少</strong> 操作次数。</p>
|
||||
|
||||
<p>两个字符 <code>a</code> 和 <code>b</code> 如果满足 <code>a == b</code> 或者 <code>a</code> 和 <code>b</code> 在字母表中是相邻的,那么我们称它们是 <strong>近似相等</strong> 字符。</p>
|
||||
|
||||
<p> </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> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 100</code></li>
|
||||
<li><code>word</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@ -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> </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> </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>
|
||||
</ul>
|
@ -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> </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] > 0 ,现在有 1 个已测试设备,batteryPercentages 变为 [1,0,1,0,2] 。
|
||||
在设备 1 上,batteryPercentages[1] == 0 ,移动到下一个设备而不进行测试。
|
||||
在设备 2 上,batteryPercentages[2] > 0 ,现在有 2 个已测试设备,batteryPercentages 变为 [1,0,1,0,1] 。
|
||||
在设备 3 上,batteryPercentages[3] == 0 ,移动到下一个设备而不进行测试。
|
||||
在设备 4 上,batteryPercentages[4] > 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] > 0 ,现在有 1 个已测试设备,batteryPercentages 变为 [0,1,1] 。
|
||||
在设备 2 上,batteryPercentages[2] > 0 ,现在有 2 个已测试设备,batteryPercentages 保持不变。
|
||||
因此,答案是 2 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == batteryPercentages.length <= 100 </code></li>
|
||||
<li><code>0 <= batteryPercentages[i] <= 100</code></li>
|
||||
</ul>
|
@ -0,0 +1,33 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个 <strong>正整数</strong> <code>k</code> 。</p>
|
||||
|
||||
<p>请你统计有多少满足 「 <code>nums</code> 中的 <strong>最大</strong> 元素」至少出现 <code>k</code> 次的子数组,并返回满足这一条件的子数组的数目。</p>
|
||||
|
||||
<p>子数组是数组中的一个连续元素序列。</p>
|
||||
|
||||
<p> </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> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> having length <code>n</code>.</p>
|
||||
|
||||
<p>You are allowed to perform a special move <strong>any</strong> number of times (<strong>including zero</strong>) on <code>nums</code>. In one <strong>special</strong> <strong>move</strong> you perform the following steps <strong>in order</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose an index <code>i</code> in the range <code>[0, n - 1]</code>, and a <strong>positive</strong> integer <code>x</code>.</li>
|
||||
<li>Add <code>|nums[i] - x|</code> to the total cost.</li>
|
||||
<li>Change the value of <code>nums[i]</code> to <code>x</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A <strong>palindromic number</strong> is a positive integer that remains the same when its digits are reversed. For example, <code>121</code>, <code>2552</code> and <code>65756</code> are palindromic numbers whereas <code>24</code>, <code>46</code>, <code>235</code> are not palindromic numbers.</p>
|
||||
|
||||
<p>An array is considered <strong>equalindromic</strong> if all the elements in the array are equal to an integer <code>y</code>, where <code>y</code> is a <strong>palindromic number</strong> less than <code>10<sup>9</sup></code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>minimum</strong> possible total cost to make </em><code>nums</code><em> <strong>equalindromic</strong> by performing any number of special moves.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> We can make the array equalindromic by changing all elements to 3 which is a palindromic number. The cost of changing the array to [3,3,3,3,3] using 4 special moves is given by |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6.
|
||||
It can be shown that changing all elements to any palindromic number other than 3 cannot be achieved at a lower cost.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,12,13,14,15]
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> We can make the array equalindromic by changing all elements to 11 which is a palindromic number. The cost of changing the array to [11,11,11,11,11] using 5 special moves is given by |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11.
|
||||
It can be shown that changing all elements to any palindromic number other than 11 cannot be achieved at a lower cost.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [22,33,22,33,22]
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong> We can make the array equalindromic by changing all elements to 22 which is a palindromic number. The cost of changing the array to [22,22,22,22,22] using 2 special moves is given by |33 - 22| + |33 - 22| = 22.
|
||||
It can be shown that changing all elements to any palindromic number other than 22 cannot be achieved at a lower cost.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,69 @@
|
||||
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p>
|
||||
|
||||
<p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, they have decided to close down some of these branches (<strong>possibly none</strong>). However, they want to ensure that the remaining branches have a distance of at most <code>maxDistance</code> from each other.</p>
|
||||
|
||||
<p>The <strong>distance</strong> between two branches is the <strong>minimum</strong> total traveled length needed to reach one branch from another.</p>
|
||||
|
||||
<p>You are given integers <code>n</code>, <code>maxDistance</code>, and a <strong>0-indexed</strong> 2D array <code>roads</code>, where <code>roads[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> represents the <strong>undirected</strong> road between branches <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> with length <code>w<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Return <em>the number of possible sets of closing branches, so that any branch has a distance of at most </em><code>maxDistance</code><em> from any other</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that, after closing a branch, the company will no longer have access to any roads connected to it.</p>
|
||||
|
||||
<p><strong>Note</strong> that, multiple roads are allowed.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/11/08/example11.png" style="width: 221px; height: 191px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, maxDistance = 5, roads = [[0,1,2],[1,2,10],[0,2,10]]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The possible sets of closing branches are:
|
||||
- The set [2], after closing, active branches are [0,1] and they are reachable to each other within distance 2.
|
||||
- The set [0,1], after closing, the active branch is [2].
|
||||
- The set [1,2], after closing, the active branch is [0].
|
||||
- The set [0,2], after closing, the active branch is [1].
|
||||
- The set [0,1,2], after closing, there are no active branches.
|
||||
It can be proven, that there are only 5 possible sets of closing branches.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/11/08/example22.png" style="width: 221px; height: 241px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, maxDistance = 5, roads = [[0,1,20],[0,1,10],[1,2,2],[0,2,2]]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> The possible sets of closing branches are:
|
||||
- The set [], after closing, active branches are [0,1,2] and they are reachable to each other within distance 4.
|
||||
- The set [0], after closing, active branches are [1,2] and they are reachable to each other within distance 2.
|
||||
- The set [1], after closing, active branches are [0,2] and they are reachable to each other within distance 2.
|
||||
- The set [0,1], after closing, the active branch is [2].
|
||||
- The set [1,2], after closing, the active branch is [0].
|
||||
- The set [0,2], after closing, the active branch is [1].
|
||||
- The set [0,1,2], after closing, there are no active branches.
|
||||
It can be proven, that there are only 7 possible sets of closing branches.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1, maxDistance = 10, roads = []
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The possible sets of closing branches are:
|
||||
- The set [], after closing, the active branch is [0].
|
||||
- The set [0], after closing, there are no active branches.
|
||||
It can be proven, that there are only 2 possible sets of closing branches.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10</code></li>
|
||||
<li><code>1 <= maxDistance <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= roads.length <= 1000</code></li>
|
||||
<li><code>roads[i].length == 3</code></li>
|
||||
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
|
||||
<li><code>1 <= w<sub>i</sub> <= 1000</code></li>
|
||||
<li>All branches are reachable from each other by traveling some roads.</li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>You are given an integer array <code>nums</code> of size <code>n</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>Divide the array into one or more arrays of size <code>3</code> satisfying the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Each</strong> element of <code>nums</code> should be in <strong>exactly</strong> one array.</li>
|
||||
<li>The difference between <strong>any</strong> two elements in one array is less than or equal to <code>k</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>a </em><strong>2D</strong><em> array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return <strong>any</strong> of them.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,4,8,7,9,3,5,1], k = 2
|
||||
<strong>Output:</strong> [[1,1,3],[3,4,5],[7,8,9]]
|
||||
<strong>Explanation:</strong> We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9].
|
||||
The difference between any two elements in each array is less than or equal to 2.
|
||||
Note that the order of elements is not important.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,3,2,7,3], k = 3
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> It is not possible to divide the array satisfying all the conditions.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>n</code> is a multiple of <code>3</code>.</li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D array <code>variables</code> where <code>variables[i] = [a<sub>i</sub>, b<sub>i</sub>, c<sub>i,</sub> m<sub>i</sub>]</code>, and an integer <code>target</code>.</p>
|
||||
|
||||
<p>An index <code>i</code> is <strong>good</strong> if the following formula holds:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < 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>Return <em>an array consisting of <strong>good</strong> indices in <strong>any order</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2
|
||||
<strong>Output:</strong> [0,2]
|
||||
<strong>Explanation:</strong> For each index i in the variables array:
|
||||
1) For the index 0, variables[0] = [2,3,3,10], (2<sup>3</sup> % 10)<sup>3</sup> % 10 = 2.
|
||||
2) For the index 1, variables[1] = [3,3,3,1], (3<sup>3</sup> % 10)<sup>3</sup> % 1 = 0.
|
||||
3) For the index 2, variables[2] = [6,1,1,4], (6<sup>1</sup> % 10)<sup>1</sup> % 4 = 2.
|
||||
Therefore we return [0,2] as the answer.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> variables = [[39,3,1000,1000]], target = 17
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> For each index i in the variables array:
|
||||
1) For the index 0, variables[0] = [39,3,1000,1000], (39<sup>3</sup> % 10)<sup>1000</sup> % 1000 = 1.
|
||||
Therefore we return [] as the answer.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= variables.length <= 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 <= a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>, m<sub>i</sub> <= 10<sup>3</sup></code></li>
|
||||
<li><code><font face="monospace">0 <= target <= 10<sup>3</sup></font></code></li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>You can perform the following operation on the array <strong>at most</strong> <code>k</code> times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose any index <code>i</code> from the array and <strong>increase</strong> or <strong>decrease</strong> <code>nums[i]</code> by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The score of the final array is the <strong>frequency</strong> of the most frequent element in the array.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> score you can achieve</em>.</p>
|
||||
|
||||
<p>The frequency of an element is the number of occurences of that element in the array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,6,4], k = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can do the following operations on the array:
|
||||
- Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4].
|
||||
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3].
|
||||
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2].
|
||||
The element 2 is the most frequent in the final array so our score is 3.
|
||||
It can be shown that we cannot achieve a better score.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,4,2,4], k = 0
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>14</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,31 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D integer matrix <code><font face="monospace">grid</font></code> of size <code>n * n</code> with values in the range <code>[1, n<sup>2</sup>]</code>. Each integer appears <strong>exactly once</strong> except <code>a</code> which appears <strong>twice</strong> and <code>b</code> which is <strong>missing</strong>. The task is to find the repeating and missing numbers <code>a</code> and <code>b</code>.</p>
|
||||
|
||||
<p>Return <em>a <strong>0-indexed </strong>integer array </em><code>ans</code><em> of size </em><code>2</code><em> where </em><code>ans[0]</code><em> equals to </em><code>a</code><em> and </em><code>ans[1]</code><em> equals to </em><code>b</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,3],[2,2]]
|
||||
<strong>Output:</strong> [2,4]
|
||||
<strong>Explanation:</strong> Number 2 is repeated and number 4 is missing so the answer is [2,4].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[9,1,7],[8,9,2],[3,4,6]]
|
||||
<strong>Output:</strong> [9,5]
|
||||
<strong>Explanation:</strong> Number 9 is repeated and number 5 is missing so the answer is [9,5].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == grid.length == grid[i].length <= 50</code></li>
|
||||
<li><code>1 <= grid[i][j] <= n * n</code></li>
|
||||
<li>For all <code>x</code> that <code>1 <= x <= n * n</code> there is exactly one <code>x</code> that is not equal to any of the grid members.</li>
|
||||
<li>For all <code>x</code> that <code>1 <= x <= n * n</code> there is exactly one <code>x</code> that is equal to exactly two of the grid members.</li>
|
||||
<li>For all <code>x</code> that <code>1 <= x <= n * n</code> except two of them there is exatly one pair of <code>i, j</code> that <code>0 <= i, j <= n - 1</code> and <code>grid[i][j] == x</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,39 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of sizes <code>n</code> and <code>m</code>, respectively.</p>
|
||||
|
||||
<p>Consider calculating the following values:</p>
|
||||
|
||||
<ul>
|
||||
<li>The number of indices <code>i</code> such that <code>0 <= i < n</code> and <code>nums1[i]</code> occurs <strong>at least</strong> once in <code>nums2</code>.</li>
|
||||
<li>The number of indices <code>i</code> such that <code>0 <= i < m</code> and <code>nums2[i]</code> occurs <strong>at least</strong> once in <code>nums1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer array </em><code>answer</code><em> of size </em><code>2</code><em> containing the two values <strong>in the above order</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
|
||||
<strong>Output:</strong> [3,4]
|
||||
<strong>Explanation:</strong> We calculate the values as follows:
|
||||
- The elements at indices 1, 2, and 3 in nums1 occur at least once in nums2. So the first value is 3.
|
||||
- The elements at indices 0, 1, 3, and 4 in nums2 occur at least once in nums1. So the second value is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,4,2,3], nums2 = [1,5]
|
||||
<strong>Output:</strong> [0,0]
|
||||
<strong>Explanation:</strong> There are no common elements between the two arrays, so the two values will be 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length</code></li>
|
||||
<li><code>m == nums2.length</code></li>
|
||||
<li><code>1 <= n, m <= 100</code></li>
|
||||
<li><code>1 <= nums1[i], nums2[i] <= 100</code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p>
|
||||
|
||||
<p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equal</strong> to <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest</strong> <strong>good</strong> subarray of</em> <code>nums</code><em>.</em></p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,1,2,3,1,2], k = 2
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The longest possible good subarray is [1,2,3,1,2,3] since the values 1, 2, and 3 occur at most twice in this subarray. Note that the subarrays [2,3,1,2,3,1] and [3,1,2,3,1,2] are also good.
|
||||
It can be shown that there are no good subarrays with length more than 6.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1,2,1,2,1,2], k = 1
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The longest possible good subarray is [1,2] since the values 1 and 2 occur at most once in this subarray. Note that the subarray [2,1] is also good.
|
||||
It can be shown that there are no good subarrays with length more than 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,5,5,5,5,5,5], k = 4
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The longest possible good subarray is [5,5,5,5] since the value 5 occurs 4 times in this subarray.
|
||||
It can be shown that there are no good subarrays with length more than 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= nums.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,42 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p>
|
||||
|
||||
<p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent <strong>almost-equal</strong> characters from</em> <code>word</code>.</p>
|
||||
|
||||
<p>Two characters <code>a</code> and <code>b</code> are <strong>almost-equal</strong> if <code>a == b</code> or <code>a</code> and <code>b</code> are adjacent in the alphabet.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "aaaaa"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can change word into "a<strong><u>c</u></strong>a<u><strong>c</strong></u>a" which does not have any adjacent almost-equal characters.
|
||||
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abddez"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can change word into "<strong><u>y</u></strong>bd<u><strong>o</strong></u>ez" which does not have any adjacent almost-equal characters.
|
||||
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "zyxyxyz"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can change word into "z<u><strong>a</strong></u>x<u><strong>a</strong></u>x<strong><u>a</u></strong>z" which does not have any adjacent almost-equal characters.
|
||||
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 100</code></li>
|
||||
<li><code>word</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <strong>positive</strong> integers.</p>
|
||||
|
||||
<p>A partition of an array into one or more <strong>contiguous</strong> subarrays is called <strong>good</strong> if no two subarrays contain the same number.</p>
|
||||
|
||||
<p>Return <em>the <strong>total number</strong> of good partitions of </em><code>nums</code>.</p>
|
||||
|
||||
<p>Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> The 8 possible good partitions are: ([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]), and ([1,2,3,4]).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1,1]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The only possible good partition is: ([1,1,1,1]).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1,3]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>batteryPercentages</code> having length <code>n</code>, denoting the battery percentages of <code>n</code> <strong>0-indexed</strong> devices.</p>
|
||||
|
||||
<p>Your task is to test each device <code>i</code> <strong>in order</strong> from <code>0</code> to <code>n - 1</code>, by performing the following test operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>batteryPercentages[i]</code> is <strong>greater</strong> than <code>0</code>:
|
||||
|
||||
<ul>
|
||||
<li><strong>Increment</strong> the count of tested devices.</li>
|
||||
<li><strong>Decrease</strong> the battery percentage of all devices with indices <code>j</code> in the range <code>[i + 1, n - 1]</code> by <code>1</code>, ensuring their battery percentage <strong>never goes below</strong> <code>0</code>, i.e, <code>batteryPercentages[j] = max(0, batteryPercentages[j] - 1)</code>.</li>
|
||||
<li>Move to the next device.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>Otherwise, move to the next device without performing any test.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting the number of devices that will be tested after performing the test operations in order.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> batteryPercentages = [1,1,2,1,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation: </strong>Performing the test operations in order starting from device 0:
|
||||
At device 0, batteryPercentages[0] > 0, so there is now 1 tested device, and batteryPercentages becomes [1,0,1,0,2].
|
||||
At device 1, batteryPercentages[1] == 0, so we move to the next device without testing.
|
||||
At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages becomes [1,0,1,0,1].
|
||||
At device 3, batteryPercentages[3] == 0, so we move to the next device without testing.
|
||||
At device 4, batteryPercentages[4] > 0, so there are now 3 tested devices, and batteryPercentages stays the same.
|
||||
So, the answer is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> batteryPercentages = [0,1,2]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Performing the test operations in order starting from device 0:
|
||||
At device 0, batteryPercentages[0] == 0, so we move to the next device without testing.
|
||||
At device 1, batteryPercentages[1] > 0, so there is now 1 tested device, and batteryPercentages becomes [0,1,1].
|
||||
At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages stays the same.
|
||||
So, the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == batteryPercentages.length <= 100 </code></li>
|
||||
<li><code>0 <= batteryPercentages[i] <= 100</code></li>
|
||||
</ul>
|
@ -0,0 +1,31 @@
|
||||
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,2,3,3], k = 2
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,2,1], k = 3
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> No subarray contains the element 4 at least 3 times.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
187
leetcode/originData/count-the-number-of-good-partitions.json
Normal file
187
leetcode/originData/count-the-number-of-good-partitions.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
177
leetcode/originData/double-modular-exponentiation.json
Normal file
177
leetcode/originData/double-modular-exponentiation.json
Normal file
File diff suppressed because one or more lines are too long
174
leetcode/originData/find-common-elements-between-two-arrays.json
Normal file
174
leetcode/originData/find-common-elements-between-two-arrays.json
Normal file
File diff suppressed because one or more lines are too long
158
leetcode/originData/find-missing-and-repeated-values.json
Normal file
158
leetcode/originData/find-missing-and-repeated-values.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
180
leetcode/originData/remove-adjacent-almost-equal-characters.json
Normal file
180
leetcode/originData/remove-adjacent-almost-equal-characters.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,44 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>You can perform the following operation on the array <strong>at most</strong> <code>k</code> times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose any index <code>i</code> from the array and <strong>increase</strong> or <strong>decrease</strong> <code>nums[i]</code> by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The score of the final array is the <strong>frequency</strong> of the most frequent element in the array.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> score you can achieve</em>.</p>
|
||||
|
||||
<p>The frequency of an element is the number of occurences of that element in the array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,6,4], k = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can do the following operations on the array:
|
||||
- Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4].
|
||||
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3].
|
||||
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2].
|
||||
The element 2 is the most frequent in the final array so our score is 3.
|
||||
It can be shown that we cannot achieve a better score.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,4,2,4], k = 0
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>14</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,31 @@
|
||||
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,2,3,3], k = 2
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,2,1], k = 3
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> No subarray contains the element 4 at least 3 times.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>batteryPercentages</code> having length <code>n</code>, denoting the battery percentages of <code>n</code> <strong>0-indexed</strong> devices.</p>
|
||||
|
||||
<p>Your task is to test each device <code>i</code> <strong>in order</strong> from <code>0</code> to <code>n - 1</code>, by performing the following test operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>batteryPercentages[i]</code> is <strong>greater</strong> than <code>0</code>:
|
||||
|
||||
<ul>
|
||||
<li><strong>Increment</strong> the count of tested devices.</li>
|
||||
<li><strong>Decrease</strong> the battery percentage of all devices with indices <code>j</code> in the range <code>[i + 1, n - 1]</code> by <code>1</code>, ensuring their battery percentage <strong>never goes below</strong> <code>0</code>, i.e, <code>batteryPercentages[j] = max(0, batteryPercentages[j] - 1)</code>.</li>
|
||||
<li>Move to the next device.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>Otherwise, move to the next device without performing any test.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting the number of devices that will be tested after performing the test operations in order.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> batteryPercentages = [1,1,2,1,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation: </strong>Performing the test operations in order starting from device 0:
|
||||
At device 0, batteryPercentages[0] > 0, so there is now 1 tested device, and batteryPercentages becomes [1,0,1,0,2].
|
||||
At device 1, batteryPercentages[1] == 0, so we move to the next device without testing.
|
||||
At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages becomes [1,0,1,0,1].
|
||||
At device 3, batteryPercentages[3] == 0, so we move to the next device without testing.
|
||||
At device 4, batteryPercentages[4] > 0, so there are now 3 tested devices, and batteryPercentages stays the same.
|
||||
So, the answer is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> batteryPercentages = [0,1,2]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Performing the test operations in order starting from device 0:
|
||||
At device 0, batteryPercentages[0] == 0, so we move to the next device without testing.
|
||||
At device 1, batteryPercentages[1] > 0, so there is now 1 tested device, and batteryPercentages becomes [0,1,1].
|
||||
At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages stays the same.
|
||||
So, the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == batteryPercentages.length <= 100 </code></li>
|
||||
<li><code>0 <= batteryPercentages[i] <= 100</code></li>
|
||||
</ul>
|
40
leetcode/problem/count-the-number-of-good-partitions.html
Normal file
40
leetcode/problem/count-the-number-of-good-partitions.html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <strong>positive</strong> integers.</p>
|
||||
|
||||
<p>A partition of an array into one or more <strong>contiguous</strong> subarrays is called <strong>good</strong> if no two subarrays contain the same number.</p>
|
||||
|
||||
<p>Return <em>the <strong>total number</strong> of good partitions of </em><code>nums</code>.</p>
|
||||
|
||||
<p>Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> The 8 possible good partitions are: ([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]), and ([1,2,3,4]).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1,1]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The only possible good partition is: ([1,1,1,1]).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1,3]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>You are given an integer array <code>nums</code> of size <code>n</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>Divide the array into one or more arrays of size <code>3</code> satisfying the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Each</strong> element of <code>nums</code> should be in <strong>exactly</strong> one array.</li>
|
||||
<li>The difference between <strong>any</strong> two elements in one array is less than or equal to <code>k</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>a </em><strong>2D</strong><em> array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return <strong>any</strong> of them.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,4,8,7,9,3,5,1], k = 2
|
||||
<strong>Output:</strong> [[1,1,3],[3,4,5],[7,8,9]]
|
||||
<strong>Explanation:</strong> We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9].
|
||||
The difference between any two elements in each array is less than or equal to 2.
|
||||
Note that the order of elements is not important.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,3,2,7,3], k = 3
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> It is not possible to divide the array satisfying all the conditions.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>n</code> is a multiple of <code>3</code>.</li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
43
leetcode/problem/double-modular-exponentiation.html
Normal file
43
leetcode/problem/double-modular-exponentiation.html
Normal file
@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D array <code>variables</code> where <code>variables[i] = [a<sub>i</sub>, b<sub>i</sub>, c<sub>i,</sub> m<sub>i</sub>]</code>, and an integer <code>target</code>.</p>
|
||||
|
||||
<p>An index <code>i</code> is <strong>good</strong> if the following formula holds:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < 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>Return <em>an array consisting of <strong>good</strong> indices in <strong>any order</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2
|
||||
<strong>Output:</strong> [0,2]
|
||||
<strong>Explanation:</strong> For each index i in the variables array:
|
||||
1) For the index 0, variables[0] = [2,3,3,10], (2<sup>3</sup> % 10)<sup>3</sup> % 10 = 2.
|
||||
2) For the index 1, variables[1] = [3,3,3,1], (3<sup>3</sup> % 10)<sup>3</sup> % 1 = 0.
|
||||
3) For the index 2, variables[2] = [6,1,1,4], (6<sup>1</sup> % 10)<sup>1</sup> % 4 = 2.
|
||||
Therefore we return [0,2] as the answer.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> variables = [[39,3,1000,1000]], target = 17
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> For each index i in the variables array:
|
||||
1) For the index 0, variables[0] = [39,3,1000,1000], (39<sup>3</sup> % 10)<sup>1000</sup> % 1000 = 1.
|
||||
Therefore we return [] as the answer.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= variables.length <= 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 <= a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>, m<sub>i</sub> <= 10<sup>3</sup></code></li>
|
||||
<li><code><font face="monospace">0 <= target <= 10<sup>3</sup></font></code></li>
|
||||
</ul>
|
@ -0,0 +1,39 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of sizes <code>n</code> and <code>m</code>, respectively.</p>
|
||||
|
||||
<p>Consider calculating the following values:</p>
|
||||
|
||||
<ul>
|
||||
<li>The number of indices <code>i</code> such that <code>0 <= i < n</code> and <code>nums1[i]</code> occurs <strong>at least</strong> once in <code>nums2</code>.</li>
|
||||
<li>The number of indices <code>i</code> such that <code>0 <= i < m</code> and <code>nums2[i]</code> occurs <strong>at least</strong> once in <code>nums1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer array </em><code>answer</code><em> of size </em><code>2</code><em> containing the two values <strong>in the above order</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
|
||||
<strong>Output:</strong> [3,4]
|
||||
<strong>Explanation:</strong> We calculate the values as follows:
|
||||
- The elements at indices 1, 2, and 3 in nums1 occur at least once in nums2. So the first value is 3.
|
||||
- The elements at indices 0, 1, 3, and 4 in nums2 occur at least once in nums1. So the second value is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,4,2,3], nums2 = [1,5]
|
||||
<strong>Output:</strong> [0,0]
|
||||
<strong>Explanation:</strong> There are no common elements between the two arrays, so the two values will be 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length</code></li>
|
||||
<li><code>m == nums2.length</code></li>
|
||||
<li><code>1 <= n, m <= 100</code></li>
|
||||
<li><code>1 <= nums1[i], nums2[i] <= 100</code></li>
|
||||
</ul>
|
31
leetcode/problem/find-missing-and-repeated-values.html
Normal file
31
leetcode/problem/find-missing-and-repeated-values.html
Normal file
@ -0,0 +1,31 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D integer matrix <code><font face="monospace">grid</font></code> of size <code>n * n</code> with values in the range <code>[1, n<sup>2</sup>]</code>. Each integer appears <strong>exactly once</strong> except <code>a</code> which appears <strong>twice</strong> and <code>b</code> which is <strong>missing</strong>. The task is to find the repeating and missing numbers <code>a</code> and <code>b</code>.</p>
|
||||
|
||||
<p>Return <em>a <strong>0-indexed </strong>integer array </em><code>ans</code><em> of size </em><code>2</code><em> where </em><code>ans[0]</code><em> equals to </em><code>a</code><em> and </em><code>ans[1]</code><em> equals to </em><code>b</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,3],[2,2]]
|
||||
<strong>Output:</strong> [2,4]
|
||||
<strong>Explanation:</strong> Number 2 is repeated and number 4 is missing so the answer is [2,4].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[9,1,7],[8,9,2],[3,4,6]]
|
||||
<strong>Output:</strong> [9,5]
|
||||
<strong>Explanation:</strong> Number 9 is repeated and number 5 is missing so the answer is [9,5].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == grid.length == grid[i].length <= 50</code></li>
|
||||
<li><code>1 <= grid[i][j] <= n * n</code></li>
|
||||
<li>For all <code>x</code> that <code>1 <= x <= n * n</code> there is exactly one <code>x</code> that is not equal to any of the grid members.</li>
|
||||
<li>For all <code>x</code> that <code>1 <= x <= n * n</code> there is exactly one <code>x</code> that is equal to exactly two of the grid members.</li>
|
||||
<li>For all <code>x</code> that <code>1 <= x <= n * n</code> except two of them there is exatly one pair of <code>i, j</code> that <code>0 <= i, j <= n - 1</code> and <code>grid[i][j] == x</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>The <strong>frequency</strong> of an element <code>x</code> is the number of times it occurs in an array.</p>
|
||||
|
||||
<p>An array is called <strong>good</strong> if the frequency of each element in this array is <strong>less than or equal</strong> to <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest</strong> <strong>good</strong> subarray of</em> <code>nums</code><em>.</em></p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,1,2,3,1,2], k = 2
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The longest possible good subarray is [1,2,3,1,2,3] since the values 1, 2, and 3 occur at most twice in this subarray. Note that the subarrays [2,3,1,2,3,1] and [3,1,2,3,1,2] are also good.
|
||||
It can be shown that there are no good subarrays with length more than 6.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1,2,1,2,1,2], k = 1
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The longest possible good subarray is [1,2] since the values 1 and 2 occur at most once in this subarray. Note that the subarray [2,1] is also good.
|
||||
It can be shown that there are no good subarrays with length more than 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,5,5,5,5,5,5], k = 4
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The longest possible good subarray is [5,5,5,5] since the value 5 occurs 4 times in this subarray.
|
||||
It can be shown that there are no good subarrays with length more than 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= nums.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> having length <code>n</code>.</p>
|
||||
|
||||
<p>You are allowed to perform a special move <strong>any</strong> number of times (<strong>including zero</strong>) on <code>nums</code>. In one <strong>special</strong> <strong>move</strong> you perform the following steps <strong>in order</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose an index <code>i</code> in the range <code>[0, n - 1]</code>, and a <strong>positive</strong> integer <code>x</code>.</li>
|
||||
<li>Add <code>|nums[i] - x|</code> to the total cost.</li>
|
||||
<li>Change the value of <code>nums[i]</code> to <code>x</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A <strong>palindromic number</strong> is a positive integer that remains the same when its digits are reversed. For example, <code>121</code>, <code>2552</code> and <code>65756</code> are palindromic numbers whereas <code>24</code>, <code>46</code>, <code>235</code> are not palindromic numbers.</p>
|
||||
|
||||
<p>An array is considered <strong>equalindromic</strong> if all the elements in the array are equal to an integer <code>y</code>, where <code>y</code> is a <strong>palindromic number</strong> less than <code>10<sup>9</sup></code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>minimum</strong> possible total cost to make </em><code>nums</code><em> <strong>equalindromic</strong> by performing any number of special moves.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> We can make the array equalindromic by changing all elements to 3 which is a palindromic number. The cost of changing the array to [3,3,3,3,3] using 4 special moves is given by |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6.
|
||||
It can be shown that changing all elements to any palindromic number other than 3 cannot be achieved at a lower cost.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,12,13,14,15]
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> We can make the array equalindromic by changing all elements to 11 which is a palindromic number. The cost of changing the array to [11,11,11,11,11] using 5 special moves is given by |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11.
|
||||
It can be shown that changing all elements to any palindromic number other than 11 cannot be achieved at a lower cost.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [22,33,22,33,22]
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong> We can make the array equalindromic by changing all elements to 22 which is a palindromic number. The cost of changing the array to [22,22,22,22,22] using 2 special moves is given by |33 - 22| + |33 - 22| = 22.
|
||||
It can be shown that changing all elements to any palindromic number other than 22 cannot be achieved at a lower cost.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,69 @@
|
||||
<p>There is a company with <code>n</code> branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.</p>
|
||||
|
||||
<p>The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, they have decided to close down some of these branches (<strong>possibly none</strong>). However, they want to ensure that the remaining branches have a distance of at most <code>maxDistance</code> from each other.</p>
|
||||
|
||||
<p>The <strong>distance</strong> between two branches is the <strong>minimum</strong> total traveled length needed to reach one branch from another.</p>
|
||||
|
||||
<p>You are given integers <code>n</code>, <code>maxDistance</code>, and a <strong>0-indexed</strong> 2D array <code>roads</code>, where <code>roads[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> represents the <strong>undirected</strong> road between branches <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> with length <code>w<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Return <em>the number of possible sets of closing branches, so that any branch has a distance of at most </em><code>maxDistance</code><em> from any other</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that, after closing a branch, the company will no longer have access to any roads connected to it.</p>
|
||||
|
||||
<p><strong>Note</strong> that, multiple roads are allowed.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/11/08/example11.png" style="width: 221px; height: 191px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, maxDistance = 5, roads = [[0,1,2],[1,2,10],[0,2,10]]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The possible sets of closing branches are:
|
||||
- The set [2], after closing, active branches are [0,1] and they are reachable to each other within distance 2.
|
||||
- The set [0,1], after closing, the active branch is [2].
|
||||
- The set [1,2], after closing, the active branch is [0].
|
||||
- The set [0,2], after closing, the active branch is [1].
|
||||
- The set [0,1,2], after closing, there are no active branches.
|
||||
It can be proven, that there are only 5 possible sets of closing branches.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/11/08/example22.png" style="width: 221px; height: 241px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, maxDistance = 5, roads = [[0,1,20],[0,1,10],[1,2,2],[0,2,2]]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> The possible sets of closing branches are:
|
||||
- The set [], after closing, active branches are [0,1,2] and they are reachable to each other within distance 4.
|
||||
- The set [0], after closing, active branches are [1,2] and they are reachable to each other within distance 2.
|
||||
- The set [1], after closing, active branches are [0,2] and they are reachable to each other within distance 2.
|
||||
- The set [0,1], after closing, the active branch is [2].
|
||||
- The set [1,2], after closing, the active branch is [0].
|
||||
- The set [0,2], after closing, the active branch is [1].
|
||||
- The set [0,1,2], after closing, there are no active branches.
|
||||
It can be proven, that there are only 7 possible sets of closing branches.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1, maxDistance = 10, roads = []
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The possible sets of closing branches are:
|
||||
- The set [], after closing, the active branch is [0].
|
||||
- The set [0], after closing, there are no active branches.
|
||||
It can be proven, that there are only 2 possible sets of closing branches.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10</code></li>
|
||||
<li><code>1 <= maxDistance <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= roads.length <= 1000</code></li>
|
||||
<li><code>roads[i].length == 3</code></li>
|
||||
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
|
||||
<li><code>1 <= w<sub>i</sub> <= 1000</code></li>
|
||||
<li>All branches are reachable from each other by traveling some roads.</li>
|
||||
</ul>
|
@ -0,0 +1,42 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>word</code>.</p>
|
||||
|
||||
<p>In one operation, you can pick any index <code>i</code> of <code>word</code> and change <code>word[i]</code> to any lowercase English letter.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations needed to remove all adjacent <strong>almost-equal</strong> characters from</em> <code>word</code>.</p>
|
||||
|
||||
<p>Two characters <code>a</code> and <code>b</code> are <strong>almost-equal</strong> if <code>a == b</code> or <code>a</code> and <code>b</code> are adjacent in the alphabet.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "aaaaa"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can change word into "a<strong><u>c</u></strong>a<u><strong>c</strong></u>a" which does not have any adjacent almost-equal characters.
|
||||
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abddez"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can change word into "<strong><u>y</u></strong>bd<u><strong>o</strong></u>ez" which does not have any adjacent almost-equal characters.
|
||||
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "zyxyxyz"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can change word into "z<u><strong>a</strong></u>x<u><strong>a</strong></u>x<strong><u>a</u></strong>z" which does not have any adjacent almost-equal characters.
|
||||
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 100</code></li>
|
||||
<li><code>word</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user