-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
sort-array-by-moving-items-to-empty-space.cpp
61 lines (57 loc) · 1.7 KB
/
sort-array-by-moving-items-to-empty-space.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
// Time: O(n)
// Space: O(n)
// greedy, sort
class Solution {
public:
int sortArray(vector<int>& nums) {
const auto& min_moves = [&](int d) {
const auto& index = [&](int x) {
return (x == 0) ? d * (size(nums) - 1) : x - d;
};
vector<bool> lookup(size(nums));
int result = size(nums);
for (int i = 0; i < size(nums); ++i) {
if (lookup[nums[i]]) {
continue;
}
int l = 0;
for (; !lookup[nums[i]]; ++l, i = index(nums[i])) {
lookup[nums[i]] = true;
}
--result;
if (l >= 2) {
result += 2;
}
}
return result - 2 * static_cast<int>(nums[d * (size(nums) - 1)] != 0);
};
return min(min_moves(0), min_moves(1));
}
};
// Time: O(n)
// Space: O(n)
// greedy, sort
class Solution2 {
public:
int sortArray(vector<int>& nums) {
const auto& min_moves = [&](int d) {
const auto& index = [&](int x) {
return (x == 0) ? d * (size(nums) - 1) : x - d;
};
vector<int> a(nums);
int result = 0;
for (int i = 0; i < size(a); ++i) {
int l = 1;
bool has_zero = (a[i] == 0);
for (; index(a[i]) != i; ++l, has_zero |= (a[i] == 0)) {
swap(a[i], a[index(a[i])]);
}
if (l >= 2) {
result += has_zero ? l - 1 : l + 1;
}
}
return result;
};
return min(min_moves(0), min_moves(1));
}
};