프로그래머스
1. Python
def solution(prices):
answer = [0]*len(prices)
stack = []
for i, price in enumerate(prices):
while stack and price < prices[stack[-1]]:
j = stack.pop()
answer[j] = i - j
stack.append(i)
while stack:
j = stack.pop()
answer[j] = len(prices) - 1 - j
return answer
2. 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> s;
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;
}
3. JavaScript