Notes:
This question is a very interesting one. After thinking for a while, it turns out that we need to start with the last one (or the biggest). After figuring this out, it is easy to code.
See the code below:
- class Solution {
- public:
- vector<int> deckRevealedIncreasing(vector<int>& deck) {
- if(deck.size() < 2) return deck;
- list<int> res;
- sort(deck.begin(), deck.end());
- int len = deck.size(), cur = len-1;
- res.push_back(deck[cur]);
- while(res.size() < len - 1) {
- res.push_front(deck[--cur]);
- res.push_front(res.back());
- res.pop_back();
- }
- res.push_front(deck[--cur]);
- return vector<int>(res.begin(), res.end());
- }
- };
No comments:
Post a Comment