mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 23:41:41 +08:00
update
This commit is contained in:
@@ -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>
|
Reference in New Issue
Block a user