on
풍선 터트리기
풍선 터트리기
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; } } Colored by Color Scripter
from http://zzunsik.tistory.com/196 by ccl(A) rewrite - 2021-09-13 21:27:01