코딩테스트/프로그래머스

프로그래머스 문제풀이 - 주식가격 (스택)

글로벌디노 2020. 10. 21. 22:40

프로그래머스 문제풀이

코딩테스트 연습 > 스택/큐 > 주식가격

programmers.co.kr/learn/courses/30/lessons/42584

 

 

C++

#include <string>
#include <vector>
#include <stack>

using namespace std;

vector<int> solution(vector<int> prices) 
{
    int size = prices.size();
	vector<int> answer(size);

	stack<int> st;

	int idx = 0;
	for (auto n : prices)
	{
		while (!st.empty() && prices[st.top()] > n)
		{
			answer[st.top()] = idx - st.top();
			st.pop();
		}
		st.push(idx++);
	}
	--idx;

	while (!st.empty())
	{
		answer[st.top()] = idx - st.top();
		st.pop();
	}

	return answer;
}