삐까냥의 파도타기

1206. [S/W 문제해결 기본] 1일차 - View 본문

코딩/SW Expert Academy

1206. [S/W 문제해결 기본] 1일차 - View

금손형아 2017. 11. 13. 19:05

문제 출처 : https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV134DPqAA8CFAYh&categoryId=AV134DPqAA8CFAYh&categoryType=CODE


import java.util.Scanner;


public class Solution{


public static void main(String[] args) {

for ( int k = 1; k < 11; k++ ) {

Scanner sc = new Scanner(System.in);

int num = sc.nextInt();

int[] buildings = new int[num];

for ( int i = 0; i < num; i++ ) {

buildings[i] = sc.nextInt();

}

int result = 0;

for ( int i = 2; i < num - 2; i++ ) {

int leftMax = getMax(buildings[i-1], buildings[i-2]);

int rightMax = getMax(buildings[i+1], buildings[i+2]);

int max = getMax(leftMax, rightMax);

if ( max < buildings[i] ) {

result += buildings[i] - max;

}

}

System.out.println("#"+k + " " + result);

}

}

public static int getMax(int first, int second) {

return first >= second ? first : second;

}

}