Byte by Byte

950. Reveal Cards In Increasing Order.cpp 본문

개발 로그/알고리즘

950. Reveal Cards In Increasing Order.cpp

CyberSoak 2021. 10. 7. 16:27

double ended queue 활용. 매우 유용한 stl.

class Solution {
public:
    
    vector<int> deckRevealedIncreasing(vector<int>& deck) {
        vector<int>res;
        deque<int>tmp;

        sort(deck.begin(),deck.end(),greater<int>());
        
        for(int i =0; i<deck.size(); i++){
            if(i==0) {
                tmp.push_back(deck[i]);
            }
            else{
                tmp.push_front(tmp[tmp.size()-1]);
                tmp.pop_back();
                tmp.push_front(deck[i]);
            }
            // cout<< i << "th : ";
            // for(auto K : tmp) cout<< K << " ";
            // cout << endl;
        }
        for(auto k : tmp){
            res.push_back(k);
        }
        
        return res;
    }
};