Skip to content

Commit 841fba8

Browse files
Sync LeetCode submission Runtime - 434 ms (62.29%), Memory - 216.4 MB (46.19%)
1 parent 5441d88 commit 841fba8

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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>&nbsp;</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] &rarr; [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] &rarr; [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] &rarr; [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>&nbsp;</p>
57+
<p><strong>Constraints:</strong></p>
58+
59+
<ul>
60+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
61+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
62+
<li><code>nums</code> consists of <strong>distinct</strong> positive integers.</li>
63+
</ul>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
struct Num {
2+
int val;
3+
int digitSum;
4+
5+
Num(int val) : val(val), digitSum(0) {
6+
int temp = val;
7+
while (temp) {
8+
digitSum += temp % 10;
9+
temp /= 10;
10+
}
11+
}
12+
13+
bool operator<(const Num& other) const {
14+
if (digitSum < other.digitSum) return true;
15+
if (digitSum > other.digitSum) return false;
16+
return val < other.val;
17+
}
18+
};
19+
20+
class Solution {
21+
public:
22+
int minSwaps(vector<int>& nums) {
23+
int n = nums.size();
24+
unordered_map<int, int> loc;
25+
for (int i=0; i<n; i++) loc[nums[i]] = i;
26+
vector<Num> a(nums.begin(), nums.end());
27+
sort(a.begin(), a.end());
28+
29+
int ans = 0;
30+
for (int i=0; i<n; i++) {
31+
if (nums[i] != a[i].val) {
32+
int correctVal = a[i].val;
33+
int oldIdx = loc[correctVal];
34+
int currentVal = nums[i];
35+
loc[currentVal] = oldIdx;
36+
loc[correctVal] = i;
37+
swap(nums[i], nums[oldIdx]);
38+
ans++;
39+
}
40+
}
41+
42+
return ans;
43+
}
44+
};

0 commit comments

Comments
 (0)