그럴듯한 개발 블로그
article thumbnail
반응형

 
https://school.programmers.co.kr/learn/courses/30/lessons/42584?language=cpp 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> prices) {
    vector<int> answer(prices.size());
    int cur;
    
    for (int i = 0; i < prices.size(); i++)
    {
        int j = i;
        while (++j < prices.size())
            if (prices[i] > prices[j])
                break ;
        if (j == prices.size()) // 떨어진 적이 없으면 
            answer[i] = j - i - 1;
        else
            answer[i] = j - i;
    }
    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;
    for(int i = 0;i < prices.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()] = prices.size() - s.top() - 1;
        s.pop();
    }
    return answer;
}
반응형
profile

그럴듯한 개발 블로그

@donghyk2

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!