문제 아이디어가 스택이라는건 금방 잡았다. 규칙성 찾고 디버깅 하면서 예외 빠르게 찾자. 클때 작을때 규칙성을 찾고 예외처리 해주기.
만약이라도, 문제의 예시가 예외를 포함하지 않을 수 있으니, 좀 더 꼼꼼하게 봐보기. 모든 경우의 수 생각해보기.
import java.util.*;
class Solution {
public int solution(int[] H) {
// write your code in Java SE 8
Stack s = new Stack();
int N = H.length;
s.add(H[0]);
int temp;
int count = 0;
for(int i = 1; i < N; i++) {
temp = (int) s.peek();
if(temp == H[i]) { // 같을때 스킵
}else if(temp > H[i]) { //위에게 크면 빼고 넣기
while(temp > H[i]) {
s.pop();
count++;
if(!s.isEmpty()) {
temp = (int)s.peek();
}else {
break;
}
}
if(temp == H[i]) {
continue;
}
s.add(H[i]);
}else { // 위에게 작으면 추가로 넣기
s.add(H[i]);
}
}
count += s.size();
return(count);
}
}