문제출처: https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV134DPqAA8CFAYh
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
문제 이해
목표
왼쪽, 오른쪽이 2칸이상 비은 세대는 조망권이라고 한다. 조망권이 확보된 세대의 수를 출력하면 된다.
풀이
1. 현재 빌딩을 중심으로 좌,우로 두개의 빌딩을 조사해서 가장 높은 층을 구한다. -> maxFloor
2. 현재 빌딩에서 조망권이 확보된 세대수 = (현재 중심이 된 빌딩의 높이 - maxFloor)
단, 현재 중심이된 빌딩의 높이가 다른 4개의 빌딩의 높이보다 커야한다.
3. 1,2의 로직으로 확보된 세대수를 각 빌딩마다 구하고 배열에 저장하여 나중에 다 더하면 answer가 된다.
소스코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
// https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV134DPqAA8CFAYh
public class View {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int test_case = 1; test_case <= 10; test_case++) {
int N = Integer.parseInt(br.readLine());
int[] buildings = new int[N];
int[] counts = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int i = 0;
while (st.hasMoreTokens()) {
buildings[i++] = Integer.parseInt(st.nextToken());
}
for (i = 2; i < N - 2; i++) {
if (buildings[i] > buildings[i - 2] && buildings[i] > buildings[i - 1] &&
buildings[i] > buildings[i + 1] && buildings[i] > buildings[i + 2]) {
int maxFloor = 0;
if (maxFloor < buildings[i - 2]) {
maxFloor = buildings[i - 2];
}
if (maxFloor < buildings[i - 1]) {
maxFloor = buildings[i - 1];
}
if (maxFloor < buildings[i + 1]) {
maxFloor = buildings[i + 1];
}
if (maxFloor < buildings[i + 2]) {
maxFloor = buildings[i + 2];
}
// 현재 빌딩에서 조망권이 확보된 세대수 = (현재 중심이 된 빌딩의 높이 - maxFloor)
counts[i] = buildings[i] - maxFloor;
}
}
int answer = 0;
for (i = 0; i < N; i++) {
answer += counts[i];
}
System.out.println("#" + test_case + " " + answer);
}
}
}
개선전략
없다. -> 너무 어렵게 생각하지말자