Information Security Study
[SWEA][S/W 문제해결 기본] 1206. 1일차-view 본문
[SWEA][S/W 문제해결 기본] 1206. 1일차-view
문제 링크
https://swexpertacademy.com/main/solvingProblem/solvingProblem.do
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
문제 요약
각 건물의 조망권을 확인하여 확보된 세대의 수를 계산하는 문제
문제 풀이
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final int TEST_CASES = 10;
for (int t = 1; t <= TEST_CASES; t++) {
int n = sc.nextInt(); // 건물의 개수
int[] buildings = new int[n];
// 건물 높이 입력
for (int i = 0; i < n; i++) {
buildings[i] = sc.nextInt();
}
int viewCount = 0; // 조망권 확보된 세대 수
// 맨 왼쪽 두 칸과 맨 오른쪽 두 칸은 건물이 없으므로 무시
for (int i = 2; i < n - 2; i++) {
int currentHeight = buildings[i];
// 주변 4개의 건물 중 가장 높은 높이
int maxNeighborHeight = Math.max(
Math.max(buildings[i - 2], buildings[i - 1]),
Math.max(buildings[i + 1], buildings[i + 2])
);
// 현재 건물이 주변 건물보다 높으면 조망권 확보
if (currentHeight > maxNeighborHeight) {
viewCount += currentHeight - maxNeighborHeight;
}
}
// 결과 출력
System.out.printf("#%d %d\n", t, viewCount);
}
sc.close();
}
}
입력 처리
- 각 테스트케이스마다 건물 개수와 높이를 입력받는다.
조망권 확인
- 각 건물의 높이와 주변 4개의 건물(왼쪽 2개, 오른쪽 2개)의 높이를 비교한다.
- 현재 건물이 주변 건물보다 높다면 조망권 확보된 세대 수는 현재 건물 높이 - 주변 최고 높이다.
결과 출력
- 테스트케이스 번호와 조망권이 확보된 세대의 수를 출력한다.
핵심 로직
- Math.max를 이용해 주변 4칸의 최고 높이를 구한다.
- 현재 건물 높이 - 주변 최고 높이가 양수라면 이를 더해 조망권 확보된 세대 수를 계산한다.
'Programming > JAVA' 카테고리의 다른 글
[SWEA] 17299. 최소 덧셈 (0) | 2024.11.16 |
---|---|
[SWEA] 17938. 문자열 세 개 (1) | 2024.11.15 |
[프로그래머스] 모의고사(level 1, 완전탐색) (0) | 2024.11.13 |
[프로그래머스] 최소직사각형(level1, 완전탐색) (1) | 2024.11.12 |
[프로그래머스] 게임 맵 최단거리(level2, BFS) (0) | 2024.11.11 |