-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path451.cpp
58 lines (57 loc) · 1.73 KB
/
451.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
__________________________________________________________________________________________________
sample 4 ms submission
void beforeMain(void) __attribute__((constructor));
void beforeMain(void) {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
}
class Solution {
public:
inline static string frequencySort(string& s) {
typedef struct {
char ch;
int frq;
} data;
data Freq[128];
for (int i = 0; i < 128; ++i) {
Freq[i].ch = i;
Freq[i].frq = 0;
}
for (const char& c : s) {
++Freq[c].frq;
}
auto comp = [](const data& d1, const data& d2) { return d1.frq < d2.frq; };
std::make_heap(Freq, Freq+128, comp);
std::pop_heap(Freq, Freq+128, comp);
s.clear();
for (int i = 127; i >= 0 && Freq[i].frq > 0; --i) {
s.append(Freq[i].frq, Freq[i].ch);
std::pop_heap(Freq, Freq + i, comp);
}
return s;
}
};
__________________________________________________________________________________________________
sample 10308 kb submission
vector<int> counts(256,0);
//int counts[256] = {0};
bool myCompare(char& a, char& b){
cout << a << "xx" << b << endl;
cout << counts[a] << "vs" << counts[b] << endl;
return counts[a] > counts[b] || (counts[a] == counts[b] && a < b);
}
class Solution {
public:
string frequencySort(string s) {
int counts[256] = {0};
for (char ch : s)
++counts[ch];
for(int i = 0; i < 256; i++)
cout << counts[i];
sort(s.begin(), s.end(), [&](char a, char b) {
return counts[a] > counts[b] || (counts[a] == counts[b] && a < b);
});
return s;
}
};
__________________________________________________________________________________________________