-
Notifications
You must be signed in to change notification settings - Fork 0
/
0950.cpp
53 lines (48 loc) · 1.07 KB
/
0950.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
class Solution {
public:
vector<int> deckRevealedIncreasing(vector<int> &deck) {
// return func1(deck);
return func2(deck);
}
// ** referance queue
vector<int> func1(vector<int> &deck) {
vector<int> res(deck.size());
sort(deck.begin(), deck.end());
queue<int> que;
for (int i = 0; i < deck.size(); i++) {
que.push(i);
}
for (int i = 0; i < deck.size(); i++) {
res[que.front()] = deck[i];
que.pop();
if (que.size() > 0) {
que.push(que.front());
que.pop();
}
}
return res;
}
// ** more readable
vector<int> func2(vector<int> &deck) {
vector<int> res(deck.size());
queue<int> que;
for (int i = 0; i < deck.size(); i++) {
que.push(i);
}
int idx = 0;
while (que.size() > 0) {
res[que.front()] = idx;
idx++;
que.pop();
if (que.size() > 0) {
que.push(que.front());
que.pop();
}
}
sort(deck.begin(), deck.end());
for (int i = 0; i < res.size(); i++) {
res[i] = deck[res[i]];
}
return res;
}
};