public int[] solution(int[] arr) {
ArrayList<Integer> stk = new ArrayList<Integer>();
int i = 0;
int[] answer;
while (i < arr.length)
{
if(stk.size() == 0)
{
stk.add(arr[i]);
i++;
}
else if(stk.size() > 0)
{
if(stk.get(stk.size() - 1) < arr[i])
{
stk.add(arr[i]);
i++;
}
else if(stk.get(stk.size() - 1) >= arr[i])
{
stk.remove(stk.size() - 1);
}
}
}
answer = new int[stk.size()];
for(int j = 0; j < stk.size(); j++)
{
answer[j] = stk.get(j);
}
return answer;
}