[프로그래머스]#42588 탑

SeungBird·2020년 8월 26일
0

문제

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

풀이

import java.util.*;

class Solution {
    public int[] solution(int[] heights) {
        int[] answer = new int[heights.length];
        Stack<Integer> tower = new Stack<Integer>();
        
        for(int i=heights.length-1; i>=0; i--) {
            tower.push(heights[i]);
        }
        
        for(int i=0; i<heights.length; i++) {
            int k = tower.pop();
            if(i==0)
                answer[i]=0;
            else {
                for(int j=i-1; j>=0; j--) {
                    if(heights[j]>k){
                        answer[i]=j+1;
                        break;
                    }
                }
            }
        }
        
        return answer;
    }
}
profile
👶🏻Jr. Programmer

0개의 댓글