DOTY

프로그래머스) Lv.2 - 탑 본문

Algorithm/ect

프로그래머스) Lv.2 - 탑

증식세포 2020. 10. 5. 20:16
728x90
반응형

<문제>

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

 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

<코드>

#include <string>
#include <vector>

using namespace std;

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

무식하게 생각해서 쉬웠던거 같다.

그냥 계속 왼쪽으로 가면서 높이가 더 큰거 나올때 break로 끝냈다.

stack 방법도 있는거 같던데 나중에 풀어봐야지 ㅎㅎㅎㅎㅎㅎㅎ

728x90
반응형
Comments