코테연습/프로그래머스
코딩 기초 트레이닝 > 배열 만들기 4 - Java
메각이
2023. 7. 26. 11:13
728x90
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
import java.util.ArrayList;
class Solution {
public int[] solution(int[] arr) {
ArrayList<Integer> stkList = new ArrayList<>();
int i = 0;
while (i < arr.length) {
if (stkList.isEmpty()) {
stkList.add(arr[i]);
i++;
} else if (stkList.get(stkList.size() - 1) < arr[i]) {
stkList.add(arr[i]);
i++;
} else if (stkList.get(stkList.size() - 1) >= arr[i]) {
stkList.remove(stkList.size() - 1);
}
}
int[] stk = new int[stkList.size()];
for (int j = 0; j < stkList.size(); j++) {
stk[j] = stkList.get(j);
}
return stk;
}
}
728x90