1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/邻位交换的最小次数 [minimum-adjacent-swaps-to-reach-the-kth-smallest-number].html
2022-03-29 12:43:11 +08:00

60 lines
2.5 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个表示大整数的字符串 <code>num</code> ,和一个整数 <code>k</code></p>
<p>如果某个整数是 <code>num</code> 中各位数字的一个 <strong>排列</strong> 且它的 <strong>值大于</strong> <code>num</code> ,则称这个整数为 <strong>妙数</strong> 。可能存在很多妙数,但是只需要关注 <strong>值最小</strong> 的那些。</p>
<ul>
<li>例如,<code>num = "5489355142"</code>
<ul>
<li>第 1 个最小妙数是 <code>"5489355214"</code></li>
<li>第 2 个最小妙数是 <code>"5489355241"</code></li>
<li>第 3 个最小妙数是 <code>"5489355412"</code></li>
<li>第 4 个最小妙数是 <code>"5489355421"</code></li>
</ul>
</li>
</ul>
<p>返回要得到第 <code>k</code><strong>最小妙数</strong> 需要对 <code>num</code> 执行的 <strong>相邻位数字交换的最小次数</strong></p>
<p>测试用例是按存在第 <code>k</code> 个最小妙数而生成的。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>num = "5489355142", k = 4
<strong>输出:</strong>2
<strong>解释:</strong>第 4 个最小妙数是 "5489355421" ,要想得到这个数字:
- 交换下标 7 和下标 8 对应的位:"5489355<strong>14</strong>2" -&gt; "5489355<strong>41</strong>2"
- 交换下标 8 和下标 9 对应的位:"54893554<strong>12</strong>" -&gt; "54893554<strong>21</strong>"
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>num = "11112", k = 4
<strong>输出:</strong>4
<strong>解释:</strong>第 4 个最小妙数是 "21111" ,要想得到这个数字:
- 交换下标 3 和下标 4 对应的位:"111<strong>12</strong>" -&gt; "111<strong>21</strong>"
- 交换下标 2 和下标 3 对应的位:"11<strong>12</strong>1" -&gt; "11<strong>21</strong>1"
- 交换下标 1 和下标 2 对应的位:"1<strong>12</strong>11" -&gt; "1<strong>21</strong>11"
- 交换下标 0 和下标 1 对应的位:"<strong>12</strong>111" -&gt; "<strong>21</strong>111"
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>num = "00123", k = 1
<strong>输出:</strong>1
<strong>解释:</strong>第 1 个最小妙数是 "00132" ,要想得到这个数字:
- 交换下标 3 和下标 4 对应的位:"001<strong>23</strong>" -&gt; "001<strong>32</strong>"
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= num.length &lt;= 1000</code></li>
<li><code>1 &lt;= k &lt;= 1000</code></li>
<li><code>num</code> 仅由数字组成</li>
</ul>