mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
update
This commit is contained in:
parent
3a14465651
commit
9e50b3cd07
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
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/find-if-array-can-be-sorted.json
Normal file
183
leetcode-cn/originData/find-if-array-can-be-sorted.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
@ -0,0 +1,47 @@
|
|||||||
|
<p>给你一个下标从 <strong>0</strong> 开始且全是 <strong>正</strong> 整数的数组 <code>nums</code> 。</p>
|
||||||
|
|
||||||
|
<p>一次 <b>操作</b> 中,如果两个 <strong>相邻</strong> 元素在二进制下数位为 <strong>1</strong> 的数目 <strong>相同</strong> ,那么你可以将这两个元素交换。你可以执行这个操作 <strong>任意次</strong> (<strong>也可以 0 次</strong>)。</p>
|
||||||
|
|
||||||
|
<p>如果你可以使数组变有序,请你返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<b>输入:</b>nums = [8,4,2,30,15]
|
||||||
|
<b>输出:</b>true
|
||||||
|
<b>解释:</b>我们先观察每个元素的二进制表示。 2 ,4 和 8 分别都只有一个数位为 1 ,分别为 "10" ,"100" 和 "1000" 。15 和 30 分别有 4 个数位为 1 :"1111" 和 "11110" 。
|
||||||
|
我们可以通过 4 个操作使数组有序:
|
||||||
|
- 交换 nums[0] 和 nums[1] 。8 和 4 分别只有 1 个数位为 1 。数组变为 [4,8,2,30,15] 。
|
||||||
|
- 交换 nums[1] 和 nums[2] 。8 和 2 分别只有 1 个数位为 1 。数组变为 [4,2,8,30,15] 。
|
||||||
|
- 交换 nums[0] 和 nums[1] 。4 和 2 分别只有 1 个数位为 1 。数组变为 [2,4,8,30,15] 。
|
||||||
|
- 交换 nums[3] 和 nums[4] 。30 和 15 分别有 4 个数位为 1 ,数组变为 [2,4,8,15,30] 。
|
||||||
|
数组变成有序的,所以我们返回 true 。
|
||||||
|
注意我们还可以通过其他的操作序列使数组变得有序。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<b>输入:</b>nums = [1,2,3,4,5]
|
||||||
|
<b>输出:</b>true
|
||||||
|
<b>解释:</b>数组已经是有序的,所以我们返回 true 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<b>输入:</b>nums = [3,16,8,4,2]
|
||||||
|
<b>输出:</b>false
|
||||||
|
<b>解释:</b>无法通过操作使数组变为有序。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= nums.length <= 100</code></li>
|
||||||
|
<li><code>1 <= nums[i] <= 2<sup>8</sup></code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,47 @@
|
|||||||
|
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code> 。</p>
|
||||||
|
|
||||||
|
<p>一个数组的 <strong>代价</strong> 是它的 <strong>第一个</strong> 元素。比方说,<code>[1,2,3]</code> 的代价是 <code>1</code> ,<code>[3,4,1]</code> 的代价是 <code>3</code> 。</p>
|
||||||
|
|
||||||
|
<p>你需要将 <code>nums</code> 分成 <code>3</code> 个 <strong>连续且没有交集</strong> 的子数组。</p>
|
||||||
|
|
||||||
|
<p>请你返回这些子数组的 <strong>最小</strong> 代价 <b>总和</b> 。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<b>输入:</b>nums = [1,2,3,12]
|
||||||
|
<b>输出:</b>6
|
||||||
|
<b>解释:</b>最佳分割成 3 个子数组的方案是:[1] ,[2] 和 [3,12] ,总代价为 1 + 2 + 3 = 6 。
|
||||||
|
其他得到 3 个子数组的方案是:
|
||||||
|
- [1] ,[2,3] 和 [12] ,总代价是 1 + 2 + 12 = 15 。
|
||||||
|
- [1,2] ,[3] 和 [12] ,总代价是 1 + 3 + 12 = 16 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<b>输入:</b>nums = [5,4,3]
|
||||||
|
<b>输出:</b>12
|
||||||
|
<b>解释:</b>最佳分割成 3 个子数组的方案是:[5] ,[4] 和 [3] ,总代价为 5 + 4 + 3 = 12 。
|
||||||
|
12 是所有分割方案里的最小总代价。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<b>输入:</b>nums = [10,3,1,1]
|
||||||
|
<b>输出:</b>12
|
||||||
|
<b>解释:</b>最佳分割成 3 个子数组的方案是:[10,3] ,[1] 和 [1] ,总代价为 10 + 1 + 1 = 12 。
|
||||||
|
12 是所有分割方案里的最小总代价。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>3 <= n <= 50</code></li>
|
||||||
|
<li><code>1 <= nums[i] <= 50</code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,49 @@
|
|||||||
|
<p>给你一个下标从 <strong>0</strong> 开始长度为 <code>n</code> 的整数数组 <code>nums</code> 和两个 <strong>正</strong> 整数 <code>k</code> 和 <code>dist</code> 。</p>
|
||||||
|
|
||||||
|
<p>一个数组的 <strong>代价</strong> 是数组中的 <strong>第一个</strong> 元素。比方说,<code>[1,2,3]</code> 的代价为 <code>1</code> ,<code>[3,4,1]</code> 的代价为 <code>3</code> 。</p>
|
||||||
|
|
||||||
|
<p>你需要将 <code>nums</code> 分割成 <code>k</code> 个 <strong>连续且互不相交</strong> 的子数组,满足 <strong>第二</strong> 个子数组与第 <code>k</code> 个子数组中第一个元素的下标距离 <strong>不超过</strong> <code>dist</code> 。换句话说,如果你将 <code>nums</code> 分割成子数组 <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code> ,那么它需要满足 <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code> 。</p>
|
||||||
|
|
||||||
|
<p>请你返回这些子数组的 <strong>最小</strong> 总代价。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<b>输入:</b>nums = [1,3,2,6,4,2], k = 3, dist = 3
|
||||||
|
<b>输出:</b>5
|
||||||
|
<b>解释:</b>将数组分割成 3 个子数组的最优方案是:[1,3] ,[2,6,4] 和 [2] 。这是一个合法分割,因为 i<sub>k-1</sub> - i<sub>1</sub> 等于 5 - 2 = 3 ,等于 dist 。总代价为 nums[0] + nums[2] + nums[5] ,也就是 1 + 2 + 2 = 5 。
|
||||||
|
5 是分割成 3 个子数组的最小总代价。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [10,1,2,2,2,1], k = 4, dist = 3
|
||||||
|
<b>输出:</b>15
|
||||||
|
<b>解释:</b>将数组分割成 4 个子数组的最优方案是:[10] ,[1] ,[2] 和 [2,2,1] 。这是一个合法分割,因为 i<sub>k-1</sub> - i<sub>1</sub> 等于 3 - 1 = 2 ,小于 dist 。总代价为 nums[0] + nums[1] + nums[2] + nums[3] ,也就是 10 + 1 + 2 + 2 = 15 。
|
||||||
|
分割 [10] ,[1] ,[2,2,2] 和 [1] 不是一个合法分割,因为 i<sub>k-1</sub> 和 i<sub>1</sub> 的差为 5 - 1 = 4 ,大于 dist 。
|
||||||
|
15 是分割成 4 个子数组的最小总代价。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<b>输入:</b>nums = [10,8,18,9], k = 3, dist = 1
|
||||||
|
<b>输出:</b>36
|
||||||
|
<b>解释:</b>将数组分割成 4 个子数组的最优方案是:[10] ,[8] 和 [18,9] 。这是一个合法分割,因为 i<sub>k-1</sub> - i<sub>1</sub> 等于 2 - 1 = 1 ,等于 dist 。总代价为 nums[0] + nums[1] + nums[2] ,也就是 10 + 8 + 18 = 36 。
|
||||||
|
分割 [10] ,[8,18] 和 [9] 不是一个合法分割,因为 i<sub>k-1</sub> 和 i<sub>1</sub> 的差为 3 - 1 = 2 ,大于 dist 。
|
||||||
|
36 是分割成 3 个子数组的最小总代价。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>3 <= n <= 10<sup>5</sup></code></li>
|
||||||
|
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||||
|
<li><code>3 <= k <= n</code></li>
|
||||||
|
<li><code>k - 2 <= dist <= n - 2</code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,58 @@
|
|||||||
|
<p>给你三个<strong> 正整数 </strong><code>n</code> 、<code>x</code> 和 <code>y</code> 。</p>
|
||||||
|
|
||||||
|
<p>在城市中,存在编号从 <code>1</code> 到 <code>n</code> 的房屋,由 <code>n</code> 条街道相连。对所有 <code>1 <= i < n</code> ,都存在一条街道连接编号为 <code>i</code> 的房屋与编号为 <code>i + 1</code> 的房屋。另存在一条街道连接编号为 <code>x</code> 的房屋与编号为 <code>y</code> 的房屋。</p>
|
||||||
|
|
||||||
|
<p>对于每个 <code>k</code>(<code>1 <= k <= n</code>),你需要找出所有满足要求的 <strong>房屋对 </strong><code>[house<sub>1</sub>, house<sub>2</sub>]</code> ,即从 <code>house<sub>1</sub></code> 到 <code>house<sub>2</sub></code> 需要经过的<strong> 最少</strong> 街道数为 <code>k</code> 。</p>
|
||||||
|
|
||||||
|
<p>返回一个下标从 <strong>1</strong> 开始且长度为 <code>n</code> 的数组 <code>result</code> ,其中 <code>result[k]</code> 表示所有满足要求的房屋对的数量,即从一个房屋到另一个房屋需要经过的<strong> 最少 </strong>街道数为 <code>k</code> 。</p>
|
||||||
|
|
||||||
|
<p><strong>注意</strong>,<code>x</code> 与 <code>y</code> 可以 <strong>相等 </strong>。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example2.png" style="width: 474px; height: 197px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 3, x = 1, y = 3
|
||||||
|
<strong>输出:</strong>[6,0,0]
|
||||||
|
<strong>解释:</strong>让我们检视每个房屋对
|
||||||
|
- 对于房屋对 (1, 2),可以直接从房屋 1 到房屋 2。
|
||||||
|
- 对于房屋对 (2, 1),可以直接从房屋 2 到房屋 1。
|
||||||
|
- 对于房屋对 (1, 3),可以直接从房屋 1 到房屋 3。
|
||||||
|
- 对于房屋对 (3, 1),可以直接从房屋 3 到房屋 1。
|
||||||
|
- 对于房屋对 (2, 3),可以直接从房屋 2 到房屋 3。
|
||||||
|
- 对于房屋对 (3, 2),可以直接从房屋 3 到房屋 2。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example3.png" style="width: 668px; height: 174px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 5, x = 2, y = 4
|
||||||
|
<strong>输出:</strong>[10,8,2,0,0]
|
||||||
|
<strong>解释:</strong>对于每个距离 k ,满足要求的房屋对如下:
|
||||||
|
- 对于 k == 1,满足要求的房屋对有 (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), 以及 (5, 4)。
|
||||||
|
- 对于 k == 2,满足要求的房屋对有 (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), 以及 (5, 3)。
|
||||||
|
- 对于 k == 3,满足要求的房屋对有 (1, 5),以及 (5, 1) 。
|
||||||
|
- 对于 k == 4 和 k == 5,不存在满足要求的房屋对。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example5.png" style="width: 544px; height: 130px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 4, x = 1, y = 1
|
||||||
|
<strong>输出:</strong>[6,4,2,0]
|
||||||
|
<strong>解释:</strong>对于每个距离 k ,满足要求的房屋对如下:
|
||||||
|
- 对于 k == 1,满足要求的房屋对有 (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), 以及 (4, 3)。
|
||||||
|
- 对于 k == 2,满足要求的房屋对有 (1, 3), (3, 1), (2, 4), 以及 (4, 2)。
|
||||||
|
- 对于 k == 3,满足要求的房屋对有 (1, 4), 以及 (4, 1)。
|
||||||
|
- 对于 k == 4,不存在满足要求的房屋对。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>2 <= n <= 100</code></li>
|
||||||
|
<li><code>1 <= x, y <= n</code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,58 @@
|
|||||||
|
<p>给你三个<strong> 正整数 </strong><code>n</code> 、<code>x</code> 和 <code>y</code> 。</p>
|
||||||
|
|
||||||
|
<p>在城市中,存在编号从 <code>1</code> 到 <code>n</code> 的房屋,由 <code>n</code> 条街道相连。对所有 <code>1 <= i < n</code> ,都存在一条街道连接编号为 <code>i</code> 的房屋与编号为 <code>i + 1</code> 的房屋。另存在一条街道连接编号为 <code>x</code> 的房屋与编号为 <code>y</code> 的房屋。</p>
|
||||||
|
|
||||||
|
<p>对于每个 <code>k</code>(<code>1 <= k <= n</code>),你需要找出所有满足要求的 <strong>房屋对 </strong><code>[house<sub>1</sub>, house<sub>2</sub>]</code> ,即从 <code>house<sub>1</sub></code> 到 <code>house<sub>2</sub></code> 需要经过的<strong> 最少</strong> 街道数为 <code>k</code> 。</p>
|
||||||
|
|
||||||
|
<p>返回一个下标从 <strong>1</strong> 开始且长度为 <code>n</code> 的数组 <code>result</code> ,其中 <code>result[k]</code> 表示所有满足要求的房屋对的数量,即从一个房屋到另一个房屋需要经过的<strong> 最少 </strong>街道数为 <code>k</code> 。</p>
|
||||||
|
|
||||||
|
<p><strong>注意</strong>,<code>x</code> 与 <code>y</code> 可以 <strong>相等 </strong>。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example2.png" style="width: 474px; height: 197px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 3, x = 1, y = 3
|
||||||
|
<strong>输出:</strong>[6,0,0]
|
||||||
|
<strong>解释:</strong>让我们检视每个房屋对
|
||||||
|
- 对于房屋对 (1, 2),可以直接从房屋 1 到房屋 2。
|
||||||
|
- 对于房屋对 (2, 1),可以直接从房屋 2 到房屋 1。
|
||||||
|
- 对于房屋对 (1, 3),可以直接从房屋 1 到房屋 3。
|
||||||
|
- 对于房屋对 (3, 1),可以直接从房屋 3 到房屋 1。
|
||||||
|
- 对于房屋对 (2, 3),可以直接从房屋 2 到房屋 3。
|
||||||
|
- 对于房屋对 (3, 2),可以直接从房屋 3 到房屋 2。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example3.png" style="width: 668px; height: 174px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 5, x = 2, y = 4
|
||||||
|
<strong>输出:</strong>[10,8,2,0,0]
|
||||||
|
<strong>解释:</strong>对于每个距离 k ,满足要求的房屋对如下:
|
||||||
|
- 对于 k == 1,满足要求的房屋对有 (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), 以及 (5, 4)。
|
||||||
|
- 对于 k == 2,满足要求的房屋对有 (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), 以及 (5, 3)。
|
||||||
|
- 对于 k == 3,满足要求的房屋对有 (1, 5),以及 (5, 1) 。
|
||||||
|
- 对于 k == 4 和 k == 5,不存在满足要求的房屋对。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example5.png" style="width: 544px; height: 130px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 4, x = 1, y = 1
|
||||||
|
<strong>输出:</strong>[6,4,2,0]
|
||||||
|
<strong>解释:</strong>对于每个距离 k ,满足要求的房屋对如下:
|
||||||
|
- 对于 k == 1,满足要求的房屋对有 (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), 以及 (4, 3)。
|
||||||
|
- 对于 k == 2,满足要求的房屋对有 (1, 3), (3, 1), (2, 4), 以及 (4, 2)。
|
||||||
|
- 对于 k == 3,满足要求的房屋对有 (1, 4), 以及 (4, 1)。
|
||||||
|
- 对于 k == 4,不存在满足要求的房屋对。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||||
|
<li><code>1 <= x, y <= n</code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,56 @@
|
|||||||
|
<p>给你一个字符串 <code>word</code>,由 <strong>不同 </strong>小写英文字母组成。</p>
|
||||||
|
|
||||||
|
<p>电话键盘上的按键与 <strong>不同 </strong>小写英文字母集合相映射,可以通过按压按键来组成单词。例如,按键 <code>2</code> 对应 <code>["a","b","c"]</code>,我们需要按一次键来输入 <code>"a"</code>,按两次键来输入 <code>"b"</code>,按三次键来输入 <code>"c"</code><em>。</em></p>
|
||||||
|
|
||||||
|
<p>现在允许你将编号为 <code>2</code> 到 <code>9</code> 的按键重新映射到 <strong>不同 </strong>字母集合。每个按键可以映射到<strong> 任意数量 </strong>的字母,但每个字母 <strong>必须</strong> <strong>恰好</strong> 映射到 <strong>一个 </strong>按键上。你需要找到输入字符串 <code>word</code> 所需的<strong> 最少 </strong>按键次数。</p>
|
||||||
|
|
||||||
|
<p>返回重新映射按键后输入 <code>word</code> 所需的 <strong>最少 </strong>按键次数。</p>
|
||||||
|
|
||||||
|
<p>下面给出了一种电话键盘上字母到按键的映射作为示例。注意 <code>1</code>,<code>*</code>,<code>#</code> 和 <code>0</code> <strong>不</strong> 对应任何字母。</p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>word = "abcde"
|
||||||
|
<strong>输出:</strong>5
|
||||||
|
<strong>解释:</strong>图片中给出的重新映射方案的输入成本最小。
|
||||||
|
"a" -> 在按键 2 上按一次
|
||||||
|
"b" -> 在按键 3 上按一次
|
||||||
|
"c" -> 在按键 4 上按一次
|
||||||
|
"d" -> 在按键 5 上按一次
|
||||||
|
"e" -> 在按键 6 上按一次
|
||||||
|
总成本为 1 + 1 + 1 + 1 + 1 = 5 。
|
||||||
|
可以证明不存在其他成本更低的映射方案。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>word = "xycdefghij"
|
||||||
|
<strong>输出:</strong>12
|
||||||
|
<strong>解释:</strong>图片中给出的重新映射方案的输入成本最小。
|
||||||
|
"x" -> 在按键 2 上按一次
|
||||||
|
"y" -> 在按键 2 上按两次
|
||||||
|
"c" -> 在按键 3 上按一次
|
||||||
|
"d" -> 在按键 3 上按两次
|
||||||
|
"e" -> 在按键 4 上按一次
|
||||||
|
"f" -> 在按键 5 上按一次
|
||||||
|
"g" -> 在按键 6 上按一次
|
||||||
|
"h" -> 在按键 7 上按一次
|
||||||
|
"i" -> 在按键 8 上按一次
|
||||||
|
"j" -> 在按键 9 上按一次
|
||||||
|
总成本为 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12 。
|
||||||
|
可以证明不存在其他成本更低的映射方案。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= word.length <= 26</code></li>
|
||||||
|
<li><code>word</code> 仅由小写英文字母组成。</li>
|
||||||
|
<li><code>word</code> 中的所有字母互不相同。</li>
|
||||||
|
</ul>
|
@ -0,0 +1,68 @@
|
|||||||
|
<p>给你一个字符串 <code>word</code>,由 <strong>不同 </strong>小写英文字母组成。</p>
|
||||||
|
|
||||||
|
<p>电话键盘上的按键与 <strong>不同 </strong>小写英文字母集合相映射,可以通过按压按键来组成单词。例如,按键 <code>2</code> 对应 <code>["a","b","c"]</code>,我们需要按一次键来输入 <code>"a"</code>,按两次键来输入 <code>"b"</code>,按三次键来输入 <code>"c"</code><em>。</em></p>
|
||||||
|
|
||||||
|
<p>现在允许你将编号为 <code>2</code> 到 <code>9</code> 的按键重新映射到 <strong>不同 </strong>字母集合。每个按键可以映射到<strong> 任意数量 </strong>的字母,但每个字母 <strong>必须</strong> <strong>恰好</strong> 映射到 <strong>一个 </strong>按键上。你需要找到输入字符串 <code>word</code> 所需的<strong> 最少 </strong>按键次数。</p>
|
||||||
|
|
||||||
|
<p>返回重新映射按键后输入 <code>word</code> 所需的 <strong>最少 </strong>按键次数。</p>
|
||||||
|
|
||||||
|
<p>下面给出了一种电话键盘上字母到按键的映射作为示例。注意 <code>1</code>,<code>*</code>,<code>#</code> 和 <code>0</code> <strong>不</strong> 对应任何字母。</p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>word = "abcde"
|
||||||
|
<strong>输出:</strong>5
|
||||||
|
<strong>解释:</strong>图片中给出的重新映射方案的输入成本最小。
|
||||||
|
"a" -> 在按键 2 上按一次
|
||||||
|
"b" -> 在按键 3 上按一次
|
||||||
|
"c" -> 在按键 4 上按一次
|
||||||
|
"d" -> 在按键 5 上按一次
|
||||||
|
"e" -> 在按键 6 上按一次
|
||||||
|
总成本为 1 + 1 + 1 + 1 + 1 = 5 。
|
||||||
|
可以证明不存在其他成本更低的映射方案。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>word = "xyzxyzxyzxyz"
|
||||||
|
<strong>输出:</strong>12
|
||||||
|
<strong>解释:</strong>图片中给出的重新映射方案的输入成本最小。
|
||||||
|
"x" -> 在按键 2 上按一次
|
||||||
|
"y" -> 在按键 3 上按一次
|
||||||
|
"z" -> 在按键 4 上按一次
|
||||||
|
总成本为 1 * 4 + 1 * 4 + 1 * 4 = 12 。
|
||||||
|
可以证明不存在其他成本更低的映射方案。
|
||||||
|
注意按键 9 没有映射到任何字母:不必让每个按键都存在与之映射的字母,但是每个字母都必须映射到按键上。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 3:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>word = "aabbccddeeffgghhiiiiii"
|
||||||
|
<strong>输出:</strong>24
|
||||||
|
<strong>解释:</strong>图片中给出的重新映射方案的输入成本最小。
|
||||||
|
"a" -> 在按键 2 上按一次
|
||||||
|
"b" -> 在按键 3 上按一次
|
||||||
|
"c" -> 在按键 4 上按一次
|
||||||
|
"d" -> 在按键 5 上按一次
|
||||||
|
"e" -> 在按键 6 上按一次
|
||||||
|
"f" -> 在按键 7 上按一次
|
||||||
|
"g" -> 在按键 8 上按一次
|
||||||
|
"h" -> 在按键 9 上按两次
|
||||||
|
"i" -> 在按键 9 上按一次
|
||||||
|
总成本为 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24 。
|
||||||
|
可以证明不存在其他成本更低的映射方案。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= word.length <= 10<sup>5</sup></code></li>
|
||||||
|
<li><code>word</code> 仅由小写英文字母组成。</li>
|
||||||
|
</ul>
|
@ -0,0 +1,65 @@
|
|||||||
|
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,它只包含 <strong>正</strong> 整数。</p>
|
||||||
|
|
||||||
|
<p>你的任务是通过进行以下操作 <strong>任意次</strong> (可以是 0 次) <strong>最小化</strong> <code>nums</code> 的长度:</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>在 <code>nums</code> 中选择 <strong>两个不同</strong> 的下标 <code>i</code> 和 <code>j</code> ,满足 <code>nums[i] > 0</code> 且 <code>nums[j] > 0</code> 。</li>
|
||||||
|
<li>将结果 <code>nums[i] % nums[j]</code> 插入 <code>nums</code> 的结尾。</li>
|
||||||
|
<li>将 <code>nums</code> 中下标为 <code>i</code> 和 <code>j</code> 的元素删除。</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>请你返回一个整数,它表示进行任意次操作以后<em> </em><code>nums</code> 的 <strong>最小长度</strong> 。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<b>输入:</b>nums = [1,4,3,1]
|
||||||
|
<b>输出:</b>1
|
||||||
|
<b>解释:</b>使数组长度最小的一种方法是:
|
||||||
|
操作 1 :选择下标 2 和 1 ,插入 nums[2] % nums[1] 到数组末尾,得到 [1,4,3,1,3] ,然后删除下标为 2 和 1 的元素。
|
||||||
|
nums 变为 [1,1,3] 。
|
||||||
|
操作 2 :选择下标 1 和 2 ,插入 nums[1] % nums[2] 到数组末尾,得到 [1,1,3,1] ,然后删除下标为 1 和 2 的元素。
|
||||||
|
nums 变为 [1,1] 。
|
||||||
|
操作 3 :选择下标 1 和 0 ,插入 nums[1] % nums[0] 到数组末尾,得到 [1,1,0] ,然后删除下标为 1 和 0 的元素。
|
||||||
|
nums 变为 [0] 。
|
||||||
|
nums 的长度无法进一步减小,所以答案为 1 。
|
||||||
|
1 是可以得到的最小长度。</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<b>输入:</b>nums = [5,5,5,10,5]
|
||||||
|
<b>输出:</b>2
|
||||||
|
<b>解释:</b>使数组长度最小的一种方法是:
|
||||||
|
操作 1 :选择下标 0 和 3 ,插入 nums[0] % nums[3] 到数组末尾,得到 [5,5,5,10,5,5] ,然后删除下标为 0 和 3 的元素。
|
||||||
|
nums 变为 [5,5,5,5] 。
|
||||||
|
操作 2 :选择下标 2 和 3 ,插入 nums[2] % nums[3] 到数组末尾,得到 [5,5,5,5,0] ,然后删除下标为 2 和 3 的元素。
|
||||||
|
nums 变为 [5,5,0] 。
|
||||||
|
操作 3 :选择下标 0 和 1 ,插入 nums[0] % nums[1] 到数组末尾,得到 [5,5,0,0] ,然后删除下标为 0 和 1 的元素。
|
||||||
|
nums 变为 [0,0] 。
|
||||||
|
nums 的长度无法进一步减小,所以答案为 2 。
|
||||||
|
2 是可以得到的最小长度。</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<b>输入:</b>nums = [2,3,4]
|
||||||
|
<b>输出:</b>1
|
||||||
|
<b>解释:</b>使数组长度最小的一种方法是:
|
||||||
|
操作 1 :选择下标 1 和 2 ,插入 nums[1] % nums[2] 到数组末尾,得到 [2,3,4,3] ,然后删除下标为 1 和 2 的元素。
|
||||||
|
nums 变为 [2,3] 。
|
||||||
|
操作 2 :选择下标 1 和 0 ,插入 nums[1] % nums[0] 到数组末尾,得到 [2,3,1] ,然后删除下标为 1 和 0 的元素。
|
||||||
|
nums 变为 [1] 。
|
||||||
|
nums 的长度无法进一步减小,所以答案为 1 。
|
||||||
|
1 是可以得到的最小长度。</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||||
|
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,45 @@
|
|||||||
|
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
|
||||||
|
|
||||||
|
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed to do this operation <strong>any</strong> number of times (<strong>including zero</strong>).</p>
|
||||||
|
|
||||||
|
<p>Return <code>true</code> <em>if you can sort the array, else return </em><code>false</code>.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [8,4,2,30,15]
|
||||||
|
<strong>Output:</strong> true
|
||||||
|
<strong>Explanation:</strong> Let's look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation "10", "100", and "1000" respectively. The numbers 15 and 30 have four set bits each with binary representation "1111" and "11110".
|
||||||
|
We can sort the array using 4 operations:
|
||||||
|
- Swap nums[0] with nums[1]. This operation is valid because 8 and 4 have one set bit each. The array becomes [4,8,2,30,15].
|
||||||
|
- Swap nums[1] with nums[2]. This operation is valid because 8 and 2 have one set bit each. The array becomes [4,2,8,30,15].
|
||||||
|
- Swap nums[0] with nums[1]. This operation is valid because 4 and 2 have one set bit each. The array becomes [2,4,8,30,15].
|
||||||
|
- Swap nums[3] with nums[4]. This operation is valid because 30 and 15 have four set bits each. The array becomes [2,4,8,15,30].
|
||||||
|
The array has become sorted, hence we return true.
|
||||||
|
Note that there may be other sequences of operations which also sort the array.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||||
|
<strong>Output:</strong> true
|
||||||
|
<strong>Explanation:</strong> The array is already sorted, hence we return true.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [3,16,8,4,2]
|
||||||
|
<strong>Output:</strong> false
|
||||||
|
<strong>Explanation:</strong> It can be shown that it is not possible to sort the input array using any number of operations.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= nums.length <= 100</code></li>
|
||||||
|
<li><code>1 <= nums[i] <= 2<sup>8</sup></code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,45 @@
|
|||||||
|
<p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
|
||||||
|
|
||||||
|
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
|
||||||
|
|
||||||
|
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
|
||||||
|
|
||||||
|
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [1,2,3,12]
|
||||||
|
<strong>Output:</strong> 6
|
||||||
|
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
|
||||||
|
The other possible ways to form 3 subarrays are:
|
||||||
|
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
|
||||||
|
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [5,4,3]
|
||||||
|
<strong>Output:</strong> 12
|
||||||
|
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
|
||||||
|
It can be shown that 12 is the minimum cost achievable.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [10,3,1,1]
|
||||||
|
<strong>Output:</strong> 12
|
||||||
|
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
|
||||||
|
It can be shown that 12 is the minimum cost achievable.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>3 <= n <= 50</code></li>
|
||||||
|
<li><code>1 <= nums[i] <= 50</code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,47 @@
|
|||||||
|
<p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
|
||||||
|
|
||||||
|
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
|
||||||
|
|
||||||
|
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
|
||||||
|
|
||||||
|
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
|
||||||
|
<strong>Output:</strong> 5
|
||||||
|
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
|
||||||
|
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
|
||||||
|
<strong>Output:</strong> 15
|
||||||
|
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
|
||||||
|
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
|
||||||
|
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
|
||||||
|
<strong>Output:</strong> 36
|
||||||
|
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
|
||||||
|
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
|
||||||
|
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>3 <= n <= 10<sup>5</sup></code></li>
|
||||||
|
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||||
|
<li><code>3 <= k <= n</code></li>
|
||||||
|
<li><code>k - 2 <= dist <= n - 2</code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,56 @@
|
|||||||
|
<p>You are given three <strong>positive</strong> integers <code>n</code>, <code>x</code>, and <code>y</code>.</p>
|
||||||
|
|
||||||
|
<p>In a city, there exist houses numbered <code>1</code> to <code>n</code> connected by <code>n</code> streets. There is a street connecting the house numbered <code>i</code> with the house numbered <code>i + 1</code> for all <code>1 <= i <= n - 1</code> . An additional street connects the house numbered <code>x</code> with the house numbered <code>y</code>.</p>
|
||||||
|
|
||||||
|
<p>For each <code>k</code>, such that <code>1 <= k <= n</code>, you need to find the number of <strong>pairs of houses</strong> <code>(house<sub>1</sub>, house<sub>2</sub>)</code> such that the <strong>minimum</strong> number of streets that need to be traveled to reach <code>house<sub>2</sub></code> from <code>house<sub>1</sub></code> is <code>k</code>.</p>
|
||||||
|
|
||||||
|
<p>Return <em>a <strong>1-indexed</strong> array </em><code>result</code><em> of length </em><code>n</code><em> where </em><code>result[k]</code><em> represents the <strong>total</strong> number of pairs of houses such that the <strong>minimum</strong> streets required to reach one house from the other is </em><code>k</code>.</p>
|
||||||
|
|
||||||
|
<p><strong>Note</strong> that <code>x</code> and <code>y</code> can be <strong>equal</strong>.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example2.png" style="width: 474px; height: 197px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> n = 3, x = 1, y = 3
|
||||||
|
<strong>Output:</strong> [6,0,0]
|
||||||
|
<strong>Explanation:</strong> Let's look at each pair of houses:
|
||||||
|
- For the pair (1, 2), we can go from house 1 to house 2 directly.
|
||||||
|
- For the pair (2, 1), we can go from house 2 to house 1 directly.
|
||||||
|
- For the pair (1, 3), we can go from house 1 to house 3 directly.
|
||||||
|
- For the pair (3, 1), we can go from house 3 to house 1 directly.
|
||||||
|
- For the pair (2, 3), we can go from house 2 to house 3 directly.
|
||||||
|
- For the pair (3, 2), we can go from house 3 to house 2 directly.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example3.png" style="width: 668px; height: 174px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> n = 5, x = 2, y = 4
|
||||||
|
<strong>Output:</strong> [10,8,2,0,0]
|
||||||
|
<strong>Explanation:</strong> For each distance k the pairs are:
|
||||||
|
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), and (5, 4).
|
||||||
|
- For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3).
|
||||||
|
- For k == 3, the pairs are (1, 5), and (5, 1).
|
||||||
|
- For k == 4 and k == 5, there are no pairs.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example5.png" style="width: 544px; height: 130px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> n = 4, x = 1, y = 1
|
||||||
|
<strong>Output:</strong> [6,4,2,0]
|
||||||
|
<strong>Explanation:</strong> For each distance k the pairs are:
|
||||||
|
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3).
|
||||||
|
- For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2).
|
||||||
|
- For k == 3, the pairs are (1, 4), and (4, 1).
|
||||||
|
- For k == 4, there are no pairs.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>2 <= n <= 100</code></li>
|
||||||
|
<li><code>1 <= x, y <= n</code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,56 @@
|
|||||||
|
<p>You are given three <strong>positive</strong> integers <code>n</code>, <code>x</code>, and <code>y</code>.</p>
|
||||||
|
|
||||||
|
<p>In a city, there exist houses numbered <code>1</code> to <code>n</code> connected by <code>n</code> streets. There is a street connecting the house numbered <code>i</code> with the house numbered <code>i + 1</code> for all <code>1 <= i <= n - 1</code> . An additional street connects the house numbered <code>x</code> with the house numbered <code>y</code>.</p>
|
||||||
|
|
||||||
|
<p>For each <code>k</code>, such that <code>1 <= k <= n</code>, you need to find the number of <strong>pairs of houses</strong> <code>(house<sub>1</sub>, house<sub>2</sub>)</code> such that the <strong>minimum</strong> number of streets that need to be traveled to reach <code>house<sub>2</sub></code> from <code>house<sub>1</sub></code> is <code>k</code>.</p>
|
||||||
|
|
||||||
|
<p>Return <em>a <strong>1-indexed</strong> array </em><code>result</code><em> of length </em><code>n</code><em> where </em><code>result[k]</code><em> represents the <strong>total</strong> number of pairs of houses such that the <strong>minimum</strong> streets required to reach one house from the other is </em><code>k</code>.</p>
|
||||||
|
|
||||||
|
<p><strong>Note</strong> that <code>x</code> and <code>y</code> can be <strong>equal</strong>.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example2.png" style="width: 474px; height: 197px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> n = 3, x = 1, y = 3
|
||||||
|
<strong>Output:</strong> [6,0,0]
|
||||||
|
<strong>Explanation:</strong> Let's look at each pair of houses:
|
||||||
|
- For the pair (1, 2), we can go from house 1 to house 2 directly.
|
||||||
|
- For the pair (2, 1), we can go from house 2 to house 1 directly.
|
||||||
|
- For the pair (1, 3), we can go from house 1 to house 3 directly.
|
||||||
|
- For the pair (3, 1), we can go from house 3 to house 1 directly.
|
||||||
|
- For the pair (2, 3), we can go from house 2 to house 3 directly.
|
||||||
|
- For the pair (3, 2), we can go from house 3 to house 2 directly.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example3.png" style="width: 668px; height: 174px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> n = 5, x = 2, y = 4
|
||||||
|
<strong>Output:</strong> [10,8,2,0,0]
|
||||||
|
<strong>Explanation:</strong> For each distance k the pairs are:
|
||||||
|
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), and (5, 4).
|
||||||
|
- For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3).
|
||||||
|
- For k == 3, the pairs are (1, 5), and (5, 1).
|
||||||
|
- For k == 4 and k == 5, there are no pairs.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example5.png" style="width: 544px; height: 130px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> n = 4, x = 1, y = 1
|
||||||
|
<strong>Output:</strong> [6,4,2,0]
|
||||||
|
<strong>Explanation:</strong> For each distance k the pairs are:
|
||||||
|
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3).
|
||||||
|
- For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2).
|
||||||
|
- For k == 3, the pairs are (1, 4), and (4, 1).
|
||||||
|
- For k == 4, there are no pairs.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||||
|
<li><code>1 <= x, y <= n</code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,54 @@
|
|||||||
|
<p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
|
||||||
|
|
||||||
|
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
|
||||||
|
|
||||||
|
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
|
||||||
|
|
||||||
|
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
|
||||||
|
|
||||||
|
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> word = "abcde"
|
||||||
|
<strong>Output:</strong> 5
|
||||||
|
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
|
||||||
|
"a" -> one push on key 2
|
||||||
|
"b" -> one push on key 3
|
||||||
|
"c" -> one push on key 4
|
||||||
|
"d" -> one push on key 5
|
||||||
|
"e" -> one push on key 6
|
||||||
|
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
|
||||||
|
It can be shown that no other mapping can provide a lower cost.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> word = "xycdefghij"
|
||||||
|
<strong>Output:</strong> 12
|
||||||
|
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
|
||||||
|
"x" -> one push on key 2
|
||||||
|
"y" -> two pushes on key 2
|
||||||
|
"c" -> one push on key 3
|
||||||
|
"d" -> two pushes on key 3
|
||||||
|
"e" -> one push on key 4
|
||||||
|
"f" -> one push on key 5
|
||||||
|
"g" -> one push on key 6
|
||||||
|
"h" -> one push on key 7
|
||||||
|
"i" -> one push on key 8
|
||||||
|
"j" -> one push on key 9
|
||||||
|
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
|
||||||
|
It can be shown that no other mapping can provide a lower cost.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= word.length <= 26</code></li>
|
||||||
|
<li><code>word</code> consists of lowercase English letters.</li>
|
||||||
|
<li>All letters in <code>word</code> are distinct.</li>
|
||||||
|
</ul>
|
@ -0,0 +1,66 @@
|
|||||||
|
<p>You are given a string <code>word</code> containing lowercase English letters.</p>
|
||||||
|
|
||||||
|
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
|
||||||
|
|
||||||
|
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
|
||||||
|
|
||||||
|
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
|
||||||
|
|
||||||
|
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> word = "abcde"
|
||||||
|
<strong>Output:</strong> 5
|
||||||
|
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
|
||||||
|
"a" -> one push on key 2
|
||||||
|
"b" -> one push on key 3
|
||||||
|
"c" -> one push on key 4
|
||||||
|
"d" -> one push on key 5
|
||||||
|
"e" -> one push on key 6
|
||||||
|
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
|
||||||
|
It can be shown that no other mapping can provide a lower cost.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> word = "xyzxyzxyzxyz"
|
||||||
|
<strong>Output:</strong> 12
|
||||||
|
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
|
||||||
|
"x" -> one push on key 2
|
||||||
|
"y" -> one push on key 3
|
||||||
|
"z" -> one push on key 4
|
||||||
|
Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12
|
||||||
|
It can be shown that no other mapping can provide a lower cost.
|
||||||
|
Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> word = "aabbccddeeffgghhiiiiii"
|
||||||
|
<strong>Output:</strong> 24
|
||||||
|
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
|
||||||
|
"a" -> one push on key 2
|
||||||
|
"b" -> one push on key 3
|
||||||
|
"c" -> one push on key 4
|
||||||
|
"d" -> one push on key 5
|
||||||
|
"e" -> one push on key 6
|
||||||
|
"f" -> one push on key 7
|
||||||
|
"g" -> one push on key 8
|
||||||
|
"h" -> two pushes on key 9
|
||||||
|
"i" -> one push on key 9
|
||||||
|
Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24.
|
||||||
|
It can be shown that no other mapping can provide a lower cost.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= word.length <= 10<sup>5</sup></code></li>
|
||||||
|
<li><code>word</code> consists of lowercase English letters.</li>
|
||||||
|
</ul>
|
@ -0,0 +1,63 @@
|
|||||||
|
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
|
||||||
|
|
||||||
|
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>Select <strong>two</strong> <strong>distinct</strong> indices <code>i</code> and <code>j</code> from <code>nums</code>, such that <code>nums[i] > 0</code> and <code>nums[j] > 0</code>.</li>
|
||||||
|
<li>Insert the result of <code>nums[i] % nums[j]</code> at the end of <code>nums</code>.</li>
|
||||||
|
<li>Delete the elements at indices <code>i</code> and <code>j</code> from <code>nums</code>.</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>Return <em>an integer denoting the <strong>minimum</strong> <strong>length</strong> of </em><code>nums</code><em> after performing the operation any number of times.</em></p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [1,4,3,1]
|
||||||
|
<strong>Output:</strong> 1
|
||||||
|
<strong>Explanation:</strong> One way to minimize the length of the array is as follows:
|
||||||
|
Operation 1: Select indices 2 and 1, insert nums[2] % nums[1] at the end and it becomes [1,4,3,1,3], then delete elements at indices 2 and 1.
|
||||||
|
nums becomes [1,1,3].
|
||||||
|
Operation 2: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [1,1,3,1], then delete elements at indices 1 and 2.
|
||||||
|
nums becomes [1,1].
|
||||||
|
Operation 3: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [1,1,0], then delete elements at indices 1 and 0.
|
||||||
|
nums becomes [0].
|
||||||
|
The length of nums cannot be reduced further. Hence, the answer is 1.
|
||||||
|
It can be shown that 1 is the minimum achievable length. </pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [5,5,5,10,5]
|
||||||
|
<strong>Output:</strong> 2
|
||||||
|
<strong>Explanation:</strong> One way to minimize the length of the array is as follows:
|
||||||
|
Operation 1: Select indices 0 and 3, insert nums[0] % nums[3] at the end and it becomes [5,5,5,10,5,5], then delete elements at indices 0 and 3.
|
||||||
|
nums becomes [5,5,5,5].
|
||||||
|
Operation 2: Select indices 2 and 3, insert nums[2] % nums[3] at the end and it becomes [5,5,5,5,0], then delete elements at indices 2 and 3.
|
||||||
|
nums becomes [5,5,0].
|
||||||
|
Operation 3: Select indices 0 and 1, insert nums[0] % nums[1] at the end and it becomes [5,5,0,0], then delete elements at indices 0 and 1.
|
||||||
|
nums becomes [0,0].
|
||||||
|
The length of nums cannot be reduced further. Hence, the answer is 2.
|
||||||
|
It can be shown that 2 is the minimum achievable length. </pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [2,3,4]
|
||||||
|
<strong>Output:</strong> 1
|
||||||
|
<strong>Explanation:</strong> One way to minimize the length of the array is as follows:
|
||||||
|
Operation 1: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [2,3,4,3], then delete elements at indices 1 and 2.
|
||||||
|
nums becomes [2,3].
|
||||||
|
Operation 2: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [2,3,1], then delete elements at indices 1 and 0.
|
||||||
|
nums becomes [1].
|
||||||
|
The length of nums cannot be reduced further. Hence, the answer is 1.
|
||||||
|
It can be shown that 1 is the minimum achievable length.</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>
|
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
File diff suppressed because one or more lines are too long
180
leetcode/originData/find-if-array-can-be-sorted.json
Normal file
180
leetcode/originData/find-if-array-can-be-sorted.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/originData/minimum-number-of-pushes-to-type-word-i.json
Normal file
180
leetcode/originData/minimum-number-of-pushes-to-type-word-i.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
@ -0,0 +1,56 @@
|
|||||||
|
<p>You are given three <strong>positive</strong> integers <code>n</code>, <code>x</code>, and <code>y</code>.</p>
|
||||||
|
|
||||||
|
<p>In a city, there exist houses numbered <code>1</code> to <code>n</code> connected by <code>n</code> streets. There is a street connecting the house numbered <code>i</code> with the house numbered <code>i + 1</code> for all <code>1 <= i <= n - 1</code> . An additional street connects the house numbered <code>x</code> with the house numbered <code>y</code>.</p>
|
||||||
|
|
||||||
|
<p>For each <code>k</code>, such that <code>1 <= k <= n</code>, you need to find the number of <strong>pairs of houses</strong> <code>(house<sub>1</sub>, house<sub>2</sub>)</code> such that the <strong>minimum</strong> number of streets that need to be traveled to reach <code>house<sub>2</sub></code> from <code>house<sub>1</sub></code> is <code>k</code>.</p>
|
||||||
|
|
||||||
|
<p>Return <em>a <strong>1-indexed</strong> array </em><code>result</code><em> of length </em><code>n</code><em> where </em><code>result[k]</code><em> represents the <strong>total</strong> number of pairs of houses such that the <strong>minimum</strong> streets required to reach one house from the other is </em><code>k</code>.</p>
|
||||||
|
|
||||||
|
<p><strong>Note</strong> that <code>x</code> and <code>y</code> can be <strong>equal</strong>.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example2.png" style="width: 474px; height: 197px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> n = 3, x = 1, y = 3
|
||||||
|
<strong>Output:</strong> [6,0,0]
|
||||||
|
<strong>Explanation:</strong> Let's look at each pair of houses:
|
||||||
|
- For the pair (1, 2), we can go from house 1 to house 2 directly.
|
||||||
|
- For the pair (2, 1), we can go from house 2 to house 1 directly.
|
||||||
|
- For the pair (1, 3), we can go from house 1 to house 3 directly.
|
||||||
|
- For the pair (3, 1), we can go from house 3 to house 1 directly.
|
||||||
|
- For the pair (2, 3), we can go from house 2 to house 3 directly.
|
||||||
|
- For the pair (3, 2), we can go from house 3 to house 2 directly.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example3.png" style="width: 668px; height: 174px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> n = 5, x = 2, y = 4
|
||||||
|
<strong>Output:</strong> [10,8,2,0,0]
|
||||||
|
<strong>Explanation:</strong> For each distance k the pairs are:
|
||||||
|
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), and (5, 4).
|
||||||
|
- For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3).
|
||||||
|
- For k == 3, the pairs are (1, 5), and (5, 1).
|
||||||
|
- For k == 4 and k == 5, there are no pairs.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example5.png" style="width: 544px; height: 130px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> n = 4, x = 1, y = 1
|
||||||
|
<strong>Output:</strong> [6,4,2,0]
|
||||||
|
<strong>Explanation:</strong> For each distance k the pairs are:
|
||||||
|
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3).
|
||||||
|
- For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2).
|
||||||
|
- For k == 3, the pairs are (1, 4), and (4, 1).
|
||||||
|
- For k == 4, there are no pairs.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>2 <= n <= 100</code></li>
|
||||||
|
<li><code>1 <= x, y <= n</code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,56 @@
|
|||||||
|
<p>You are given three <strong>positive</strong> integers <code>n</code>, <code>x</code>, and <code>y</code>.</p>
|
||||||
|
|
||||||
|
<p>In a city, there exist houses numbered <code>1</code> to <code>n</code> connected by <code>n</code> streets. There is a street connecting the house numbered <code>i</code> with the house numbered <code>i + 1</code> for all <code>1 <= i <= n - 1</code> . An additional street connects the house numbered <code>x</code> with the house numbered <code>y</code>.</p>
|
||||||
|
|
||||||
|
<p>For each <code>k</code>, such that <code>1 <= k <= n</code>, you need to find the number of <strong>pairs of houses</strong> <code>(house<sub>1</sub>, house<sub>2</sub>)</code> such that the <strong>minimum</strong> number of streets that need to be traveled to reach <code>house<sub>2</sub></code> from <code>house<sub>1</sub></code> is <code>k</code>.</p>
|
||||||
|
|
||||||
|
<p>Return <em>a <strong>1-indexed</strong> array </em><code>result</code><em> of length </em><code>n</code><em> where </em><code>result[k]</code><em> represents the <strong>total</strong> number of pairs of houses such that the <strong>minimum</strong> streets required to reach one house from the other is </em><code>k</code>.</p>
|
||||||
|
|
||||||
|
<p><strong>Note</strong> that <code>x</code> and <code>y</code> can be <strong>equal</strong>.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example2.png" style="width: 474px; height: 197px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> n = 3, x = 1, y = 3
|
||||||
|
<strong>Output:</strong> [6,0,0]
|
||||||
|
<strong>Explanation:</strong> Let's look at each pair of houses:
|
||||||
|
- For the pair (1, 2), we can go from house 1 to house 2 directly.
|
||||||
|
- For the pair (2, 1), we can go from house 2 to house 1 directly.
|
||||||
|
- For the pair (1, 3), we can go from house 1 to house 3 directly.
|
||||||
|
- For the pair (3, 1), we can go from house 3 to house 1 directly.
|
||||||
|
- For the pair (2, 3), we can go from house 2 to house 3 directly.
|
||||||
|
- For the pair (3, 2), we can go from house 3 to house 2 directly.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example3.png" style="width: 668px; height: 174px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> n = 5, x = 2, y = 4
|
||||||
|
<strong>Output:</strong> [10,8,2,0,0]
|
||||||
|
<strong>Explanation:</strong> For each distance k the pairs are:
|
||||||
|
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), and (5, 4).
|
||||||
|
- For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3).
|
||||||
|
- For k == 3, the pairs are (1, 5), and (5, 1).
|
||||||
|
- For k == 4 and k == 5, there are no pairs.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/20/example5.png" style="width: 544px; height: 130px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> n = 4, x = 1, y = 1
|
||||||
|
<strong>Output:</strong> [6,4,2,0]
|
||||||
|
<strong>Explanation:</strong> For each distance k the pairs are:
|
||||||
|
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3).
|
||||||
|
- For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2).
|
||||||
|
- For k == 3, the pairs are (1, 4), and (4, 1).
|
||||||
|
- For k == 4, there are no pairs.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||||
|
<li><code>1 <= x, y <= n</code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,45 @@
|
|||||||
|
<p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
|
||||||
|
|
||||||
|
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
|
||||||
|
|
||||||
|
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
|
||||||
|
|
||||||
|
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [1,2,3,12]
|
||||||
|
<strong>Output:</strong> 6
|
||||||
|
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
|
||||||
|
The other possible ways to form 3 subarrays are:
|
||||||
|
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
|
||||||
|
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [5,4,3]
|
||||||
|
<strong>Output:</strong> 12
|
||||||
|
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
|
||||||
|
It can be shown that 12 is the minimum cost achievable.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [10,3,1,1]
|
||||||
|
<strong>Output:</strong> 12
|
||||||
|
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
|
||||||
|
It can be shown that 12 is the minimum cost achievable.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>3 <= n <= 50</code></li>
|
||||||
|
<li><code>1 <= nums[i] <= 50</code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,47 @@
|
|||||||
|
<p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
|
||||||
|
|
||||||
|
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
|
||||||
|
|
||||||
|
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
|
||||||
|
|
||||||
|
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
|
||||||
|
<strong>Output:</strong> 5
|
||||||
|
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
|
||||||
|
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
|
||||||
|
<strong>Output:</strong> 15
|
||||||
|
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
|
||||||
|
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
|
||||||
|
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
|
||||||
|
<strong>Output:</strong> 36
|
||||||
|
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
|
||||||
|
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
|
||||||
|
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>3 <= n <= 10<sup>5</sup></code></li>
|
||||||
|
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||||
|
<li><code>3 <= k <= n</code></li>
|
||||||
|
<li><code>k - 2 <= dist <= n - 2</code></li>
|
||||||
|
</ul>
|
45
leetcode/problem/find-if-array-can-be-sorted.html
Normal file
45
leetcode/problem/find-if-array-can-be-sorted.html
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
|
||||||
|
|
||||||
|
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed to do this operation <strong>any</strong> number of times (<strong>including zero</strong>).</p>
|
||||||
|
|
||||||
|
<p>Return <code>true</code> <em>if you can sort the array, else return </em><code>false</code>.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [8,4,2,30,15]
|
||||||
|
<strong>Output:</strong> true
|
||||||
|
<strong>Explanation:</strong> Let's look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation "10", "100", and "1000" respectively. The numbers 15 and 30 have four set bits each with binary representation "1111" and "11110".
|
||||||
|
We can sort the array using 4 operations:
|
||||||
|
- Swap nums[0] with nums[1]. This operation is valid because 8 and 4 have one set bit each. The array becomes [4,8,2,30,15].
|
||||||
|
- Swap nums[1] with nums[2]. This operation is valid because 8 and 2 have one set bit each. The array becomes [4,2,8,30,15].
|
||||||
|
- Swap nums[0] with nums[1]. This operation is valid because 4 and 2 have one set bit each. The array becomes [2,4,8,30,15].
|
||||||
|
- Swap nums[3] with nums[4]. This operation is valid because 30 and 15 have four set bits each. The array becomes [2,4,8,15,30].
|
||||||
|
The array has become sorted, hence we return true.
|
||||||
|
Note that there may be other sequences of operations which also sort the array.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||||
|
<strong>Output:</strong> true
|
||||||
|
<strong>Explanation:</strong> The array is already sorted, hence we return true.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [3,16,8,4,2]
|
||||||
|
<strong>Output:</strong> false
|
||||||
|
<strong>Explanation:</strong> It can be shown that it is not possible to sort the input array using any number of operations.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= nums.length <= 100</code></li>
|
||||||
|
<li><code>1 <= nums[i] <= 2<sup>8</sup></code></li>
|
||||||
|
</ul>
|
@ -0,0 +1,63 @@
|
|||||||
|
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
|
||||||
|
|
||||||
|
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>Select <strong>two</strong> <strong>distinct</strong> indices <code>i</code> and <code>j</code> from <code>nums</code>, such that <code>nums[i] > 0</code> and <code>nums[j] > 0</code>.</li>
|
||||||
|
<li>Insert the result of <code>nums[i] % nums[j]</code> at the end of <code>nums</code>.</li>
|
||||||
|
<li>Delete the elements at indices <code>i</code> and <code>j</code> from <code>nums</code>.</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>Return <em>an integer denoting the <strong>minimum</strong> <strong>length</strong> of </em><code>nums</code><em> after performing the operation any number of times.</em></p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [1,4,3,1]
|
||||||
|
<strong>Output:</strong> 1
|
||||||
|
<strong>Explanation:</strong> One way to minimize the length of the array is as follows:
|
||||||
|
Operation 1: Select indices 2 and 1, insert nums[2] % nums[1] at the end and it becomes [1,4,3,1,3], then delete elements at indices 2 and 1.
|
||||||
|
nums becomes [1,1,3].
|
||||||
|
Operation 2: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [1,1,3,1], then delete elements at indices 1 and 2.
|
||||||
|
nums becomes [1,1].
|
||||||
|
Operation 3: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [1,1,0], then delete elements at indices 1 and 0.
|
||||||
|
nums becomes [0].
|
||||||
|
The length of nums cannot be reduced further. Hence, the answer is 1.
|
||||||
|
It can be shown that 1 is the minimum achievable length. </pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [5,5,5,10,5]
|
||||||
|
<strong>Output:</strong> 2
|
||||||
|
<strong>Explanation:</strong> One way to minimize the length of the array is as follows:
|
||||||
|
Operation 1: Select indices 0 and 3, insert nums[0] % nums[3] at the end and it becomes [5,5,5,10,5,5], then delete elements at indices 0 and 3.
|
||||||
|
nums becomes [5,5,5,5].
|
||||||
|
Operation 2: Select indices 2 and 3, insert nums[2] % nums[3] at the end and it becomes [5,5,5,5,0], then delete elements at indices 2 and 3.
|
||||||
|
nums becomes [5,5,0].
|
||||||
|
Operation 3: Select indices 0 and 1, insert nums[0] % nums[1] at the end and it becomes [5,5,0,0], then delete elements at indices 0 and 1.
|
||||||
|
nums becomes [0,0].
|
||||||
|
The length of nums cannot be reduced further. Hence, the answer is 2.
|
||||||
|
It can be shown that 2 is the minimum achievable length. </pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> nums = [2,3,4]
|
||||||
|
<strong>Output:</strong> 1
|
||||||
|
<strong>Explanation:</strong> One way to minimize the length of the array is as follows:
|
||||||
|
Operation 1: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [2,3,4,3], then delete elements at indices 1 and 2.
|
||||||
|
nums becomes [2,3].
|
||||||
|
Operation 2: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [2,3,1], then delete elements at indices 1 and 0.
|
||||||
|
nums becomes [1].
|
||||||
|
The length of nums cannot be reduced further. Hence, the answer is 1.
|
||||||
|
It can be shown that 1 is the minimum achievable length.</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,54 @@
|
|||||||
|
<p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
|
||||||
|
|
||||||
|
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
|
||||||
|
|
||||||
|
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
|
||||||
|
|
||||||
|
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
|
||||||
|
|
||||||
|
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> word = "abcde"
|
||||||
|
<strong>Output:</strong> 5
|
||||||
|
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
|
||||||
|
"a" -> one push on key 2
|
||||||
|
"b" -> one push on key 3
|
||||||
|
"c" -> one push on key 4
|
||||||
|
"d" -> one push on key 5
|
||||||
|
"e" -> one push on key 6
|
||||||
|
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
|
||||||
|
It can be shown that no other mapping can provide a lower cost.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> word = "xycdefghij"
|
||||||
|
<strong>Output:</strong> 12
|
||||||
|
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
|
||||||
|
"x" -> one push on key 2
|
||||||
|
"y" -> two pushes on key 2
|
||||||
|
"c" -> one push on key 3
|
||||||
|
"d" -> two pushes on key 3
|
||||||
|
"e" -> one push on key 4
|
||||||
|
"f" -> one push on key 5
|
||||||
|
"g" -> one push on key 6
|
||||||
|
"h" -> one push on key 7
|
||||||
|
"i" -> one push on key 8
|
||||||
|
"j" -> one push on key 9
|
||||||
|
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
|
||||||
|
It can be shown that no other mapping can provide a lower cost.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= word.length <= 26</code></li>
|
||||||
|
<li><code>word</code> consists of lowercase English letters.</li>
|
||||||
|
<li>All letters in <code>word</code> are distinct.</li>
|
||||||
|
</ul>
|
@ -0,0 +1,66 @@
|
|||||||
|
<p>You are given a string <code>word</code> containing lowercase English letters.</p>
|
||||||
|
|
||||||
|
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
|
||||||
|
|
||||||
|
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
|
||||||
|
|
||||||
|
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
|
||||||
|
|
||||||
|
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> word = "abcde"
|
||||||
|
<strong>Output:</strong> 5
|
||||||
|
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
|
||||||
|
"a" -> one push on key 2
|
||||||
|
"b" -> one push on key 3
|
||||||
|
"c" -> one push on key 4
|
||||||
|
"d" -> one push on key 5
|
||||||
|
"e" -> one push on key 6
|
||||||
|
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
|
||||||
|
It can be shown that no other mapping can provide a lower cost.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> word = "xyzxyzxyzxyz"
|
||||||
|
<strong>Output:</strong> 12
|
||||||
|
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
|
||||||
|
"x" -> one push on key 2
|
||||||
|
"y" -> one push on key 3
|
||||||
|
"z" -> one push on key 4
|
||||||
|
Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12
|
||||||
|
It can be shown that no other mapping can provide a lower cost.
|
||||||
|
Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> word = "aabbccddeeffgghhiiiiii"
|
||||||
|
<strong>Output:</strong> 24
|
||||||
|
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
|
||||||
|
"a" -> one push on key 2
|
||||||
|
"b" -> one push on key 3
|
||||||
|
"c" -> one push on key 4
|
||||||
|
"d" -> one push on key 5
|
||||||
|
"e" -> one push on key 6
|
||||||
|
"f" -> one push on key 7
|
||||||
|
"g" -> one push on key 8
|
||||||
|
"h" -> two pushes on key 9
|
||||||
|
"i" -> one push on key 9
|
||||||
|
Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24.
|
||||||
|
It can be shown that no other mapping can provide a lower cost.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= word.length <= 10<sup>5</sup></code></li>
|
||||||
|
<li><code>word</code> consists of lowercase English letters.</li>
|
||||||
|
</ul>
|
Loading…
Reference in New Issue
Block a user