본문 바로가기

Programmers/Level3

풍선 터트리기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
class Solution {
    
    public static int checkBalloon(int[] a){      
        if(a.length<=3)
            return a.length;
        int cnt = 2;
        int left=1,right=a.length-2;
        int minLeft = a[0], minRight = a[a.length-1];
        boolean flag = true;
 
        while(left<right){
            int n;
            if(flag)
                n = a[left];
            else
                n = a[right];
            if(n > minLeft && n > minRight){
                if(flag)
                    left++;
                else
                    right--;
            } else{
                if(flag){
                    if(n < minLeft) {
                        minLeft = n;
                        left++;
                        cnt++;
                    } else {
                        flag = !flag;    
                    }
                } else{
                    if(n < minRight) {
                        minRight = n;
                        right--;
                        cnt++;
                    } else {
                        flag = !flag;    
                    }
                }
            }
        }
        if(a[left] <= minLeft || a[left] <= minRight)
            cnt++;
        return cnt;
    }
 
    public int solution(int[] a) {
        int answer = 0;
        answer = checkBalloon(a);
        
        return answer;
    }
}
cs

왼쪽, 오른쪽으로 검사 방향을 변경하며 수행하였다.

'Programmers > Level3' 카테고리의 다른 글

멀리 뛰기  (0) 2021.09.14
거스름돈  (0) 2021.09.14
[1차] 추석 트래픽  (0) 2021.09.08
길 찾기 게임  (0) 2021.09.07
자물쇠와 열쇠  (0) 2021.08.26