-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path767_Reorganize-String.cpp
More file actions
117 lines (110 loc) · 2.68 KB
/
767_Reorganize-String.cpp
File metadata and controls
117 lines (110 loc) · 2.68 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
class Solution
{
public:
string reorganizeString(string s)
{
priority_queue<pair<int, char>> pq;
int l = s.size();
vector<int> vec(26);
int sum = 0;
for (auto i : s)
{
vec[i - 'a']++;
}
int mx = *max_element(vec.begin(), vec.end());
if (mx > l - mx + 1)
{
return "";
}
for (int i = 0; i < 26; ++i)
{
if (vec[i] == 0)
{
continue;
}
pq.push(make_pair(vec[i], i + 'a'));
}
string ans = "";
while (!pq.empty())
{
auto t = pq.top();
pq.pop();
vector<pair<int, char>> temp;
if (t.first == 0)
{
break;
}
if (!ans.empty() && t.second == ans.back())
{
temp.push_back(t);
t = pq.top();
pq.pop();
ans += t.second;
t.first--;
pq.push(t);
pq.push(temp.back());
continue;
}
ans += t.second;
t.first--;
pq.push(t);
}
return ans;
}
};
// https://zhuanlan.zhihu.com/p/1945782212176909162
// 最重要知道的是只有 m <= n - m + 1 才能进行
// 然后用大根堆,先放大的再放小的
// 大到小排就好,若是ans.back() = 堆顶的元素时,取下一个放进去就好了
// 先贴上我的方法,明天补上更好的
class Solution
{
public:
string reorganizeString(string s)
{
vector<int> vec(26);
int l = s.size();
for (int i = 0; i < l; ++i)
{
vec[s[i] - 'a']++;
}
int mx = *max_element(vec.begin(), vec.end());
if (mx > l - mx + 1)
{
return "";
}
priority_queue<pair<int, char>> pq;
for (int i = 0; i < 26; ++i)
{
if (vec[i] == 0)
{
continue;
}
pq.push(make_pair(vec[i], i + 'a'));
}
int i = 0;
string ans(l, 0);
while (!pq.empty())
{
auto t = pq.top();
pq.pop();
while (t.first > 0)
{
ans[i] = t.second;
t.first--;
i += 2;
if (i >= l)
{
i = 1;
}
}
}
return ans;
}
};
// 先排偶数,再排计数,虽然感觉这个还是有点怪怪的吧,感觉不用堆也行