알고리즘/SWEA

4796. 의석이의 우뚝 선 산

  • -

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWS2h6AKBCoDFAVT

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

www.swexpertacademy.com

아마도 입력 데이터에 문제가 있는듯하다. BufferedReader를 사용하면 runtime 오류 발생 --> 반드시 Scanner 사용할 것

 

public class Solution { static Scanner scanner = new Scanner(System.in); static StringBuilder sb; static StringTokenizer tokens; static int T, N; static int[] map; public static void main(String[] args) throws NumberFormatException, IOException { sb = new StringBuilder(); scanner = new Scanner(src); T = scanner.nextInt(); for (int t = 1; t <= T; t++) { int answer = 0; N = scanner.nextInt(); map = new int[N]; for (int i = 0; i < N; i++) { map[i] = scanner.nextInt(); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// for (int i = 1; i < N - 1; i++) { if (map[i] > map[i - 1] && map[i] > map[i + 1]) { // 우뚝 솟은 지점 int downL = getDownL(i); int downR = getDownR(i); answer+=downL*downR; } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// sb.append(String.format("#%d %d\n", t, answer)); } System.out.println(sb); } public static int getDownL(int from) { int n = 0; for(int i=from; i>0; i--) { if(map[i]> map[i-1]) { // 이전 요소와 계속 비교 n++; }else { break; } } return n; } public static int getDownR(int from) { int n = 0; for(int i=from; i<N-1; i++) { if(map[i]> map[i+1]) { // 다음 요소와 계속 비교 n++; }else { break; } } return n; } static String src = "3\r\n" + "3\r\n" + "1 3 2\r\n" + "3\r\n" + "3 2 1\r\n" + "9\r\n" + "1 4 6 5 3 7 9 2 8"; }
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.