#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> heights) {
    vector<int> answer;
    int j=heights.size()-2;
    //heights 벡터의 가장 오른쪽 부터 기록을 진행
    for(int i=j+1;i>=0;i--)
    {   
        //송신탑의 높이가 더 높은 경우 신호가 닿음
        //answer에 push_back하여 신호가 닿은 송신탑 위치 저장
        if(heights[i]<heights[j]){
            answer.push_back(j+1);
            j=i-1;
        }
        //현재 송신탑의 위치에서 끝까지 신호가 닿지 않거나
        //더이상 송신탑이 존재하지 않았을 때는 0 을 push_back
        else if(j==0||i==0)
        {
            answer.push_back(0);
            j=i-1;
        }
        //송신탑이 닿지 않은 경우 현재 송신탑에 대한 탐색
        //j++를 통해 그 다음 송신탑과 통신 확인
        else
        {
            j--;
            i++;
        }
    }
    //answer에 반대의 순서로 push_back해주었기 때문에 reverse
    reverse(answer.begin(),answer.end());
    return answer;
}

+ Recent posts