[SWEA] #1206 View

KwonSC·2021년 11월 6일
0

SWEA - Java

목록 보기
2/26
post-thumbnail

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV134DPqAA8CFAYh&categoryId=AV134DPqAA8CFAYh&categoryType=CODE


Code

import java.util.Scanner;

class Solution {
    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        for (int testCase = 1; testCase <= 10; testCase++) {
            int num = sc.nextInt();
            int result = 0;
            int arr[] = new int[num];
            for (int i = 0; i < num; i++) {
                arr[i] = sc.nextInt();
            }
            for (int i = 2; i < num - 2; i++) {
                int max = 0;
                for (int j = i - 2; j <= i + 2; j++) {
                    if (max <= arr[j] && i != j) {
                        max = arr[j];
                    }
                }
                if (arr[i] > max) {
                    result += arr[i] - max;
                }
            }
            System.out.printf("#%d %d\n", testCase, result);
        }
    }
}

Solution

배열 arr 입력받은 빌딩 높이를 순회
현재 위치에서 -2 ~ +2를 다시 순회하면서 최대 높이의 빌딩을 찾은후 자기 자신 제외
현재 빌딩의 높이가 최대 높이의 빌딩보다 클때 result 변수에 현재 빌딩 높이 - 최대 빌딩 높이를 더하여 출력

0개의 댓글