SWEA
1206. [S/W 문제해결 기본] 1일차 - View [D3] by Java
배준형
2023. 11. 15. 13:40
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
문제에 대한 내용은 위의 링크에서 확인하실 수 있습니다.
나의 풀이
이 문제는 브루트포스로 해결했습니다.
건물을 완전탐색하면서, 해당 건물 위치에서 -2, -1, 1, 2에 위치한 건물의 높이를 비교하여 조망권이 확보되었는지 확인하였습니다.
import java.io.*;
import java.util.*;
class Solution
{
public static void main(String args[]) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = 10;
for(int test_case = 1; test_case <= T; test_case++)
{
int n = Integer.parseInt(br.readLine());
int[] buildingHeights = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
buildingHeights[i] = Integer.parseInt(st.nextToken());
}
int clearViewCnt = getClearViewCount(n, buildingHeights);
System.out.println("#" + test_case + " " + clearViewCnt);
}
}
static int[] checkArray = new int[] {-2, -1, 1, 2};
public static int getClearViewCount(int n, int[] buildingHeights) {
int cnt = 0;
for (int i = 2; i < n - 2; i++) {
boolean isClear = true;
int nowBuildHeights = buildingHeights[i];
int max = 0;
for (int check : checkArray) {
max = Math.max(max, buildingHeights[i + check]);
if (buildingHeights[i + check] >= nowBuildHeights) {
isClear = false;
}
}
if (isClear) {
cnt += nowBuildHeights - max;
}
}
return cnt;
}
}