Jun's Development Journey

[SWEA] 1206. [S/W 문제해결 기본] Array1 - View 본문

SWEA/Intermediate

[SWEA] 1206. [S/W 문제해결 기본] Array1 - View

J_Jayce 2019. 7. 31. 13:00

문제

https://www.swexpertacademy.com/main/learn/course/lectureProblemViewer.do

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

www.swexpertacademy.com

 

 

풀이

양 쪽의 두 칸의 간격의 높이들과 비교해서 조망권이 확보되는 지에 대한 판별하는 문제이다. 

왼쪽과 오른쪽을 나누어 각각의 두 칸의 간격에 있는 높이들 중 큰 값과의 차이를 이용해서 구했다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.*;
public class Solution {
    static Scanner scan = new Scanner(System.in);
    static int[] heights;
    
    static int get_gens(int width) {
        int answer=0;
        for(int i=2;i<width-2;i++) {
            int left=0,right=0;//왼쪽과 오른쪽의 각각의 두 칸의 높이 중 큰 것과의 차이를 이용
            left = heights[i] - Math.max(heights[i-1], heights[i-2]);
            right = heights[i] - Math.max(heights[i+1], heights[i+2]);
            if(left>0 && right>0)//차이가 음수일 경우 조망권 확보가 안되므로 이것을 판별 
                answer+=Math.min(left, right);
        }
        return answer;
    }
    static void solution() {
        int width=0;
        for(int i=1;i<=10;i++) {
            width = scan.nextInt();
            heights = new int[width];
            for(int j=0;j<width;j++
                heights[j] = scan.nextInt();
            System.out.println("#"+i+" "+get_gens(width));
        }
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        solution();
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter