|
| 1 | +<p>You are given an array <code>nums</code> of <strong>distinct</strong> positive integers. You need to sort the array in <strong>increasing</strong> order based on the sum of the digits of each number. If two numbers have the same digit sum, the <strong>smaller</strong> number appears first in the sorted order.</p> |
| 2 | + |
| 3 | +<p>Return the <strong>minimum</strong> number of swaps required to rearrange <code>nums</code> into this sorted order.</p> |
| 4 | + |
| 5 | +<p>A <strong>swap</strong> is defined as exchanging the values at two distinct positions in the array.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<div class="example-block"> |
| 11 | +<p><strong>Input:</strong> <span class="example-io">nums = [37,100]</span></p> |
| 12 | + |
| 13 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 14 | + |
| 15 | +<p><strong>Explanation:</strong></p> |
| 16 | + |
| 17 | +<ul> |
| 18 | + <li>Compute the digit sum for each integer: <code>[3 + 7 = 10, 1 + 0 + 0 = 1] → [10, 1]</code></li> |
| 19 | + <li>Sort the integers based on digit sum: <code>[100, 37]</code>. Swap <code>37</code> with <code>100</code> to obtain the sorted order.</li> |
| 20 | + <li>Thus, the minimum number of swaps required to rearrange <code>nums</code> is 1.</li> |
| 21 | +</ul> |
| 22 | +</div> |
| 23 | + |
| 24 | +<p><strong class="example">Example 2:</strong></p> |
| 25 | + |
| 26 | +<div class="example-block"> |
| 27 | +<p><strong>Input:</strong> <span class="example-io">nums = [22,14,33,7]</span></p> |
| 28 | + |
| 29 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 30 | + |
| 31 | +<p><strong>Explanation:</strong></p> |
| 32 | + |
| 33 | +<ul> |
| 34 | + <li>Compute the digit sum for each integer: <code>[2 + 2 = 4, 1 + 4 = 5, 3 + 3 = 6, 7 = 7] → [4, 5, 6, 7]</code></li> |
| 35 | + <li>Sort the integers based on digit sum: <code>[22, 14, 33, 7]</code>. The array is already sorted.</li> |
| 36 | + <li>Thus, the minimum number of swaps required to rearrange <code>nums</code> is 0.</li> |
| 37 | +</ul> |
| 38 | +</div> |
| 39 | + |
| 40 | +<p><strong class="example">Example 3:</strong></p> |
| 41 | + |
| 42 | +<div class="example-block"> |
| 43 | +<p><strong>Input:</strong> <span class="example-io">nums = [18,43,34,16]</span></p> |
| 44 | + |
| 45 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 46 | + |
| 47 | +<p><strong>Explanation:</strong></p> |
| 48 | + |
| 49 | +<ul> |
| 50 | + <li>Compute the digit sum for each integer: <code>[1 + 8 = 9, 4 + 3 = 7, 3 + 4 = 7, 1 + 6 = 7] → [9, 7, 7, 7]</code></li> |
| 51 | + <li>Sort the integers based on digit sum: <code>[16, 34, 43, 18]</code>. Swap <code>18</code> with <code>16</code>, and swap <code>43</code> with <code>34</code> to obtain the sorted order.</li> |
| 52 | + <li>Thus, the minimum number of swaps required to rearrange <code>nums</code> is 2.</li> |
| 53 | +</ul> |
| 54 | +</div> |
| 55 | + |
| 56 | +<p> </p> |
| 57 | +<p><strong>Constraints:</strong></p> |
| 58 | + |
| 59 | +<ul> |
| 60 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 61 | + <li><code>1 <= nums[i] <= 10<sup>9</sup></code></li> |
| 62 | + <li><code>nums</code> consists of <strong>distinct</strong> positive integers.</li> |
| 63 | +</ul> |
0 commit comments