자윤이와고리즘/Code

[프로그래머스] 탑

EUJU 2019. 7. 22. 10:57
#include <string>
#include <vector>
#include <stack>
using namespace std;

vector<int> solution(vector<int> heights) {
    vector<int> answer(heights.size());
    stack <int> test;
    stack <int> tmp;
    int leng = heights.size();
    int k =0;
    for(int i=0; i < leng; i++){
        test.push(heights[i]);
    }
    for(int i=leng; i >0; i--){
        k = i;
        int now_n = test.top();
        test.pop();
        while(!test.empty()){
            int next_n = test.top();
            test.pop();
            tmp.push(next_n);
            k--;
            if(next_n > now_n){
                answer[i-1] = k;
                break;
            }

        }
         while(!tmp.empty()){
            test.push(tmp.top());
            tmp.pop();
        
         }
    }
    if(!test.empty()){
        for(int i=0; i < test.size(); i++){
            answer[i] = 0;
        }
    }
    return answer;
}