forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smallest-value-of-the-rearranged-number.cpp
67 lines (64 loc) · 1.93 KB
/
smallest-value-of-the-rearranged-number.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Time: O(d), d is the number of digits
// Space: O(d)
// greedy, counting sort
class Solution {
public:
long long smallestNumber(long long num) {
int64_t sign = (num >= 0) ? 1 : -1;
string nums = to_string(abs(num));
inplace_counting_sort(&nums, sign == -1);
auto it = find_if(begin(nums), end(nums), [](const auto& x) {
return x != '0';
});
if (it != end(nums)) {
swap(nums[0], *it);
}
return sign * stoll(nums);
}
private:
void inplace_counting_sort(string *nums, bool is_reverse) {
const int max_num = *max_element(cbegin(*nums), cend(*nums));
vector<int> count(max_num + 1);
for (const auto& num : *nums) {
++count[num];
}
for (int i = 1; i < size(count); ++i) {
count[i] += count[i - 1];
}
for (int i = size(*nums) - 1; i >= 0; --i) { // inplace but unstable sort
while ((*nums)[i] >= 0) {
--count[(*nums)[i]];
const int j = count[(*nums)[i]];
tie((*nums)[i], (*nums)[j]) = pair((*nums)[j], ~(*nums)[i]);
}
}
for (auto& num : *nums) {
num = ~num; // restore values
}
if (is_reverse) { // unstable sort
reverse(begin(*nums), end(*nums));
}
}
};
// Time: O(dlogd), d is the number of digits
// Space: O(d)
// greedy
class Solution2 {
public:
long long smallestNumber(long long num) {
int64_t sign = (num >= 0) ? 1 : -1;
string nums = to_string(abs(num));
if (sign == 1) {
sort(begin(nums), end(nums));
} else {
sort(rbegin(nums), rend(nums));
}
auto it = find_if(begin(nums), end(nums), [](const auto& x) {
return x != '0';
});
if (it != end(nums)) {
swap(nums[0], *it);
}
return sign * stoll(nums);
}
};