Tiny Bunny
본문 바로가기

programmers

프로그래머스 3단계 : 풍선 터뜨리기 (Java 자바)

728x90

 

class Solution {
    public int solution(int[] a) {
        int len = a.length;
        if(a.length<=2) return a.length;
        
        int[] leftMin = new int[len];
        int[] rightMin = new int[len];
        
        int minTemp = Integer.MAX_VALUE;
        for(int i=0; i<len; i++){
            minTemp = Math.min(minTemp, a[i]);
            leftMin[i] = minTemp;
        }
        
        minTemp = Integer.MAX_VALUE;
        for(int i=len-1; i>=0; i--){
            minTemp = Math.min(minTemp, a[i]);
            rightMin[i] = minTemp;
        }
        
        int answer = 2;
        for(int i=1; i<len-1; i++){
            if( !(leftMin[i-1]<a[i] && rightMin[i+1]<a[i]) )
                answer++;
        }
        return answer;
    }
}
728x90