hye-log

[SWEA]6730번 장애물 경주 난이도(JAVA) 본문

CodingTest/SWEA

[SWEA]6730번 장애물 경주 난이도(JAVA)

iihye_ 2023. 7. 30. 21:02

0. 문제 링크

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWefy5x65PoDFAUh 

 

SW Expert Academy

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

swexpertacademy.com

 

1. 문제 설명

1) 낮은 곳에서 높은 곳으로 올라갈 때 가장 심한 높이 변화와 높은 곳에서 낮은 곳으로 내려갈 때 가장 심한 높이 변화 구하기

 

2. 입출력

// input
5
5
10 70 30 50 90
2
30 100
2
100 20
3
40 40 40
7
12 345 678 901 23 45 6

// output
#1 60 40
#2 70 0
#3 0 80
#4 0 0
#5 333 878

 

3. 코드

import java.util.*;

public class swea6730 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int T = sc.nextInt(); // 테스트 케이스의 개수
		for(int t = 1; t <= T; t++) {
			int N = sc.nextInt(); // 직사각형 블록의 개수
			int[] arr = new int[N]; // 직사각형 블록
			for(int n = 0; n < N; n++) {
				arr[n] = sc.nextInt();
			}
			
			int min = 0; 
			int max = 0;
			
			for(int n = 1; n < N; n++) {
				int sub = arr[n] - arr[n-1];
				
				if(sub > 0) { // 올라갈 때
					max = Math.max(sub, max);
				} else if(sub < 0) { // 내려갈 때
					min = Math.max(-sub, min);
				}
			}
			
			System.out.println("#" + t + " " + max + " " + min);
			
		}
	}
}

 

실행 결과

 

4. 회고

1) 연속된 두 블럭의 차와 현재의 min, max 값을 가지고 Math.max를 이용하여 높이 변화를 구함

 

5. Github

https://github.com/iihye/Algorithm/blob/main/SWEA/swea6730.java

 

728x90
Comments