Notice
Recent Posts
Link
- Today
- Total
hye-log
[SWEA]6730번 장애물 경주 난이도(JAVA) 본문
0. 문제 링크
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWefy5x65PoDFAUh
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
'CodingTest > SWEA' 카테고리의 다른 글
[SWEA]1210번 Ladder1(JAVA) (0) | 2023.08.06 |
---|---|
[SWEA]6958번 동철이의 프로그래밍 대회(JAVA) (0) | 2023.07.30 |
[SWEA]1859번 백만 장자 프로젝트(JAVA) (0) | 2023.07.30 |
[SWEA]1873번 상호의 배틀필드(JAVA) (0) | 2023.07.30 |
[SWEA]1289번 원재의 메모리 복구하기(JAVA) (0) | 2023.07.30 |
Comments