오늘의 문제
주식가격

나의 풀이
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<int> prices) {
    vector<int> answer;
    int len = prices.size();
    
    for(int i=0 ; i<len ; i++){
        answer.push_back(1);
        for(int j = i+1 ; j<len-1 ; j++){
           if(prices[i] <= prices[j]) // 상승 보존
               answer[i] ++;
            else // 하락
                break;
        }
    }
    answer[len-1] = 0;
    return answer;
}
모범 답안
#include <string>
#include <vector>
#include <stack>
using namespace std;
vector<int> solution(vector<int> prices) {
    vector<int> answer(prices.size());
    stack<int> s;
    int size = prices.size();
    for(int i=0;i<size;i++){
        while(!s.empty()&&prices[s.top()]>prices[i]){
            answer[s.top()] = i-s.top();
            s.pop();
        }
        s.push(i);
    }
    while(!s.empty()){
        answer[s.top()] = size-s.top()-1;
        s.pop();
    }
    return answer;
}
배울 점
- stack을 여기에 사용하다니 이렇게 또 배운다.
 
- stack의 활용에 대해 생각해볼 수 있었다.