[SW Expert Academy] 1206. View - Java

2019. 8. 7. 17:18프로그래밍/알고리즘

반응형

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

 

문제

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

 

 

문제풀이

조망권을 갖는 세대수를 계산하고자 하는 빌딩의 배열을 buildings[X] 라고 가정 했을 때,


buildings[X] 배열을 기준으로 왼쪽으로 두개의 배열 buildings[X-2], buildings[X-1]과 

오른쪽으로 두개의 배열 buildings[X+1], buildings[X+2]의 값을 각각 buildings[X]값에서 뺀 차이를 구한다.


각 차이의 값 중 가장 작은 값이 조망권을 갖는 세대의 수가 되고,
각 차이의 값 중 음수인 값이 있을 경우에는 조망권을 갖는 세대수는 0으로 다음 배열로 넘어간다.

 

 

전체소스                                               
import java.util.*;

public class Solution
{
    public static void main(String args[])
    {
	Scanner sc = new Scanner(System.in);
		
	for (int i = 1; i <= 10; i++)
	{    
	    int tc = sc.nextInt();          //테스트케이스 길이
            int result = 0;                 //테스트케이스의 조망권이 확보된 세대수
	    int buildings[] = new int[tc];
	    for (int j = 0; j < buildings.length; j++) 
            {
                buildings[j] = sc.nextInt();
            }

	    for (int j = 2; j < buildings.length - 2; j++)
	    {
	    	int near[] = new int[4];
	    	near[0] = buildings[j] - buildings[j-1];
	    	near[1] = buildings[j] - buildings[j-2];
	    	near[2] = buildings[j] - buildings[j+1];
	    	near[3] = buildings[j] - buildings[j+2];
	    	
	    	if( near[0]<0 || near[1]<0 || near[2]<0 || near[3]<0 ) continue;
	    	else
	    	{
                    Arrays.sort(near);
                    result = result + near[0];
	    	}
	    }
            System.out.println("#" + i + " " + result);
	}
    }
}

 

반응형