forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
47 lines (35 loc) · 1003 Bytes
/
main.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
/// Source : https://leetcode.com/problems/reveal-cards-in-increasing-order/
/// Author : liuyubobobo
/// Time : 2018-12-01
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
/// Simulation with deque
/// Time Complexity: O(nlogn)
/// Space Complexity: O(n)
class Solution {
public:
vector<int> deckRevealedIncreasing(vector<int>& deck) {
sort(deck.begin(), deck.end(), greater<int>());
deque<int> deque;
deque.push_back(deck[0]);
for(int i = 1; i < deck.size(); i ++){
int back = deque.back();
deque.pop_back();
deque.push_front(back);
deque.push_front(deck[i]);
}
return vector<int>(deque.begin(), deque.end());
}
};
void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
vector<int> deck1 = {17, 13, 11, 2, 3, 5, 7};
print_vec(Solution().deckRevealedIncreasing(deck1));
return 0;
}