import java.util.*;
public class Solution
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 10; i++)
{
int tc = sc.nextInt(); //테스트케이스 길이
int result = 0; //테스트케이스의 조망권이 확보된 세대수
int buildings[] = new int[tc];
for (int j = 0; j < buildings.length; j++)
{
buildings[j] = sc.nextInt();
}
for (int j = 2; j < buildings.length - 2; j++)
{
int near[] = new int[4];
near[0] = buildings[j] - buildings[j-1];
near[1] = buildings[j] - buildings[j-2];
near[2] = buildings[j] - buildings[j+1];
near[3] = buildings[j] - buildings[j+2];
if( near[0]<0 || near[1]<0 || near[2]<0 || near[3]<0 ) continue;
else
{
Arrays.sort(near);
result = result + near[0];
}
}
System.out.println("#" + i + " " + result);
}
}
}
조망권을 갖는 세대수를 계산하고자 하는 빌딩의 배열을 buildings[X] 라고 가정 했을 때,
buildings[X] 배열을 기준으로 왼쪽으로 두개의 배열 buildings[X-2], buildings[X-1]과
오른쪽으로 두개의 배열 buildings[X+1], buildings[X+2]의 값을 각각 buildings[X]값에서 뺀 차이를 구한다.
각 차이의 값 중 가장 작은 값이 조망권을 갖는 세대의 수가 되고,
각 차이의 값 중 음수인 값이 있을 경우에는 조망권을 갖는 세대수는 0으로 다음 배열로 넘어간다.
'JAVA 알고리즘 ' 카테고리의 다른 글
1218. [S/W 문제해결 기본] 4일차 - 괄호 짝짓기 (0) | 2019.08.30 |
---|---|
1208. [S/W 문제해결 기본] 1일차 - Flatten (0) | 2019.08.28 |
[SWEA] 1204. [S/W 문제해결 기본] 1일차 - 최빈수 구하기 (0) | 2019.08.23 |
1222. [S/W 문제해결 기본] 6일차 - 계산기1 (0) | 2019.08.17 |
SWEA 1217 (S/W 문제해결 기본) 4일차 - 거듭 제곱 (1) | 2019.08.16 |