SSAFY/SWEA

[SWEA] 1206 : View

믕비 2023. 4. 17. 19:27
반응형

https://swexpertacademy.com/main/talk/solvingClub/problemView.do?solveclubId=AV6kld8aisgDFASb&contestProbId=AV134DPqAA8CFAYh&probBoxId=AV-HZfeqN3ADFASP&type=PROBLEM&problemBoxTitle=%5BD2~D3+%EB%AC%B8%EC%A0%9C%ED%92%80%EC%9D%B4%5D+%EA%B8%B0%EC%B4%88+%EB%8B%A4%EC%A7%80%EA%B8%B0+Part3&problemBoxCnt=14 

 

SW Expert Academy

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

swexpertacademy.com

배열로 풀었고 현재 탐색 중인 인덱스의 왼쪽과 오른쪽의 요소들 2개씩 모두가 현재 인덱스보다 작음을 충족시켰을 때 가장 차이가 적은 값을 더해주며 완전탐색해준다.

 

[내 코드]

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Solution {
 
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st;
         
        for(int t = 1; t <= 10; t++) {
            sb.append('#').append(t).append(' ');
            int result = 0;
             
            int N = Integer.parseInt(br.readLine());
            int[] height = new int[N];
            st = new StringTokenizer(br.readLine());
            for(int n = 0; n < N; n++) {
                height[n] = Integer.parseInt(st.nextToken());
            }
             
            for(int i = 2; i < N -2; i++) {
                int min = 256;
                if(height[i] > height[i - 2] && height[i] > height[i - 1] && height[i] > height[i + 1] && height[i] > height[i + 2]) {
                        min = Math.min(min, height[i] - height[i - 2]);
                        min = Math.min(min, height[i] - height[i - 1]);
                        min = Math.min(min, height[i] - height[i + 1]);
                        min = Math.min(min, height[i] - height[i + 2]);
                         
                        result += min;
                }
            }
            sb.append(result).append('\n');
        }
        System.out.print(sb);
    }
 
}
반응형

'SSAFY > SWEA' 카테고리의 다른 글

[SWEA] 1229 : 암호문 2  (0) 2023.04.26
[SWEA] 1228 : 암호문 1  (0) 2023.04.26
[SWEA] 1234 : 비밀번호  (0) 2023.04.26
[SWEA] 1298 : 원재의 메모리 복구하기  (0) 2023.04.17
[SWEA] 1491 : 원재의 벽 꾸미기  (0) 2023.04.17