-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily107.cpp
More file actions
99 lines (83 loc) · 2.37 KB
/
Copy pathdaily107.cpp
File metadata and controls
99 lines (83 loc) · 2.37 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Solution 1
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& arr) {
/*
store copy of sorted array,
map sorted array to ranks
*/
auto sorted = arr;
std::sort(sorted.begin(), sorted.end());
auto rank_num = std::unordered_map<int, int>{};
auto rank = 1;
for (auto i = 0; i < arr.size(); ++i) {
if (i == 0 or sorted[i] != sorted[i - 1]) {
rank_num[sorted[i]] = rank;
rank++;
}
}
auto res = std::vector<int>(arr.size());
for (auto i = 0; i < arr.size(); ++i) {
res[i] = rank_num[arr[i]];
}
return res;
}
};
// Solution 2
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& arr) {
if (arr.empty()) return {};
// Find min and max values
auto [min_it, max_it] = minmax_element(arr.begin(), arr.end());
int min_val = *min_it;
int max_val = *max_it;
// Create a count array
int range = max_val - min_val + 1;
if (range > 2'000'000) {
// Fall back to sorting method if range is too large
return sortingMethod(arr);
}
vector<bool> count(range, false);
for (int num : arr) {
count[num - min_val] = true;
}
// Calculate ranks
vector<int> ranks(range);
int rank = 1;
for (int i = 0; i < range; i++) {
if (count[i]) {
ranks[i] = rank++;
}
}
// Assign ranks to original array
for (int& num : arr) {
num = ranks[num - min_val];
}
return arr;
}
private:
vector<int> sortingMethod(vector<int>& arr) {
vector<int> sortedArr = arr;
sort(sortedArr.begin(), sortedArr.end());
unordered_map<int, int> rankMap;
int rank = 1;
for (int num : sortedArr) {
if (rankMap.find(num) == rankMap.end()) {
rankMap[num] = rank++;
}
}
vector<int> result(arr.size());
for (int i = 0; i < arr.size(); i++) {
result[i] = rankMap[arr[i]];
}
return result;
}
};
static const int kds = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 0;
}();
//KDS