-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy paths1.cpp
More file actions
26 lines (26 loc) · 740 Bytes
/
Copy paths1.cpp
File metadata and controls
26 lines (26 loc) · 740 Bytes
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
// OJ: https://leetcode.com/problems/reveal-cards-in-increasing-order/
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(N)
class Solution {
public:
vector<int> deckRevealedIncreasing(vector<int>& deck) {
sort(deck.begin(), deck.end());
int N = deck.size();
vector<int> ans(N), movedTo;
queue<int> q;
for (int i = 0; i < N; ++i) q.push(i);
while (q.size()) {
int n = q.front();
movedTo.push_back(n);
q.pop();
if (q.size()) {
int m = q.front();
q.pop();
q.push(m);
}
}
for (int i = 0; i < N; ++i) ans[movedTo[i]] = deck[i];
return ans;
}
};