forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-sum-of-four-digit-number-after-splitting-digits.cpp
66 lines (62 loc) · 1.71 KB
/
minimum-sum-of-four-digit-number-after-splitting-digits.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
// Time: O(d) = O(1), d is the number of digits
// Space: O(d) = O(1)
// greedy
class Solution {
public:
int minimumSum(int num) {
vector<int> nums;
for (; num; num /= 10) {
nums.emplace_back(num % 10);
}
inplace_counting_sort(&nums, false);
int a = 0, b = 0;
for (const auto& x: nums) {
a = a * 10 + x;
swap(a, b);
}
return a + b;
}
private:
void inplace_counting_sort(vector<int> *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) = O(1), d is the number of digits
// Space: O(d) = O(1)
// greedy
class Solution2 {
public:
int minimumSum(int num) {
vector<int> nums;
for (; num; num /= 10) {
nums.emplace_back(num % 10);
}
sort(begin(nums), end(nums));
int a = 0, b = 0;
for (const auto& x: nums) {
a = a * 10 + x;
swap(a, b);
}
return a + b;
}
};