337. 주식가격

아현·2021년 11월 12일
0

Algorithm

목록 보기
361/400
post-custom-banner

프로그래머스



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





profile
For the sake of someone who studies computer science
post-custom-banner

0개의 댓글