배열 만들기

gotcha!!·2023년 8월 4일
0

코딩테스트

목록 보기
19/36

문제

내 코드

import java.util.*;
class Solution {
    public int[] solution(int[] arr) {
        ArrayList<Integer> stk = new ArrayList<Integer>();
        int i = 0;
        
        while(arr.length > i){
            if(stk.isEmpty() || stk.get(stk.size() -1 ) < arr[i]){
                stk.add(arr[i]);
                i++;
            }else{
                stk.remove(stk.size()-1);
            }
        }
        
        int[] result = new int[stk.size()];
        for(int j = 0; j < stk.size(); j++){
            result[j] = stk.get(j);
        }
        return result;
    }
}
profile
ha lee :)

0개의 댓글