https://www.acmicpc.net/problem/2491
2491번: 수열
0에서부터 9까지의 숫자로 이루어진 N개의 숫자가 나열된 수열이 있다. 그 수열 안에서 연속해서 커지거나(같은 것 포함), 혹은 연속해서 작아지는(같은 것 포함) 수열 중 가장 길이가 긴 것을 찾
www.acmicpc.net

해석 및 팁
이 문제는 먼저 배열에 값을 넣은 뒤 반복문을 통해 증가할 때와 감소할 때의 길이를 비교하여 큰 값을 출력하면 되는 문제입니다.
Java 코드
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int max = 1;
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int count = 1;
for(int i = 0; i < n - 1; i++) {
if(arr[i] >= arr[i + 1]) {
count++;
max = Math.max(max, count);
}
else count = 1;
}
count = 1;
for(int i = 0; i < n - 1; i++) {
if(arr[i] <= arr[i + 1]) {
count++;
max = Math.max(max, count);
}
else count = 1;
}
System.out.println(max);
}
}
'백준' 카테고리의 다른 글
백준 2470 두 용액(Java) (0) | 2023.01.30 |
---|---|
백준 9507 Generations of Tribbles(Java) (0) | 2023.01.30 |
백준 2776 암기왕(Java) (0) | 2023.01.30 |
백준 1940 주몽(Java) (0) | 2023.01.30 |
백준 2847 게임을 만든 동준이(Java) (0) | 2023.01.29 |