본문 바로가기

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

[프로그래머스] 탑

프로그래머스 탑 C++ 풀이

 

https://programmers.co.kr/learn/courses/30/lessons/42588

 

 

// 일단 answer배열 heights 사이즈만큼 0으로 resize 해주고
// i는 num-1부터 하고 j = 0부터 시작해서 j가 오른쪽으로 점점 가니까 값 갱신해 주게 되니까 결국에는 i와 가장 가까운 쪽의 탑이 됨
#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> heights) {
    vector<int> answer;
    
    int num = heights.size();
    
    answer.resize(num, 0);
    
    for (int i = num -1; i >= 0; i--){
        for (int j = 0; j < i; j++){
            if (heights[i] < heights[j])
                answer[i] = j+1;
        }
    }
    return answer;
}