본문 바로가기

Algo/백준

2491: 수열 (DP)

풀이

연속해서 커지는 경우가 가장 긴 길이를 한번 체크하고

연속해서 작아지는 경우가 가장 긴 길이를 한번 체크해서

둘 중에 더 큰 길이의 값을 출력한다.

코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		int[] arr = new int[N];
		StringTokenizer st = new StringTokenizer(br.readLine());
		for (int i = 0; i < N; i++) {
			arr[i] = Integer.parseInt(st.nextToken());
		}

		int max = 1;
		int cnt = 1;

		for (int i = 1; i < N; i++) {
			if (arr[i - 1] >= arr[i])
				cnt++;
			else
				cnt = 1;
			if (max < cnt)
				max = cnt;
		}

		cnt = 1;
		for (int i = 1; i < N; i++) {
			if (arr[i - 1] <= arr[i])
				cnt++;
			else
				cnt = 1;
			if (max < cnt)
				max = cnt;
		}
		System.out.println(max);
	}
}

'Algo > 백준' 카테고리의 다른 글

8911: 거북이 (구현)  (0) 2021.02.19
11060: 점프점프 (DP) - 실패  (0) 2021.02.19
13398: 연속합2 (DP)  (0) 2021.02.19
15990: 1,2,3 더하기 5 (DP)  (0) 2021.02.19
1520: 내리막길 (DP)  (0) 2021.02.19