Skip to content

Commit 8bf1a23

Browse files
committed
[Silver II] Title: 가장 긴 감소하는 부분 수열, Time: 124 ms, Memory: 14444 KB -BaekjoonHub
1 parent b4c6375 commit 8bf1a23

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Silver II] 가장 긴 감소하는 부분 수열 - 11722
2+
3+
[문제 링크](https://www.acmicpc.net/problem/11722)
4+
5+
### 성능 요약
6+
7+
메모리: 14444 KB, 시간: 124 ms
8+
9+
### 분류
10+
11+
다이나믹 프로그래밍
12+
13+
### 제출 일자
14+
15+
2024년 12월 24일 10:18:32
16+
17+
### 문제 설명
18+
19+
<p>수열 A가 주어졌을 때, 가장 긴 감소하는 부분 수열을 구하는 프로그램을 작성하시오.</p>
20+
21+
<p>예를 들어, 수열 A = {10, 30, 10, 20, 20, 10} 인 경우에 가장 긴 감소하는 부분 수열은 A = {10, <strong>30</strong>, 10, <strong>20</strong>, 20, <strong>10</strong>} 이고, 길이는 3이다.</p>
22+
23+
### 입력
24+
25+
<p>첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000)이 주어진다.</p>
26+
27+
<p>둘째 줄에는 수열 A를 이루고 있는 A<sub>i</sub>가 주어진다. (1 ≤ A<sub>i</sub> ≤ 1,000)</p>
28+
29+
### 출력
30+
31+
<p>첫째 줄에 수열 A의 가장 긴 감소하는 부분 수열의 길이를 출력한다.</p>
32+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
public static void main(String[] args) throws IOException {
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
8+
int N = Integer.parseInt(br.readLine());
9+
10+
int[] arr = new int[N];
11+
int[] dp = new int[N];
12+
StringTokenizer st = new StringTokenizer(br.readLine());
13+
for(int i = 0; i < N; i++) {
14+
arr[i] = Integer.parseInt(st.nextToken());
15+
dp[i] = 1;
16+
}
17+
18+
for(int i = 1; i < N; i++) {
19+
for(int j = 0; j < i; j++) {
20+
if(arr[j] > arr[i]) {
21+
dp[i] = Math.max(dp[j] + 1, dp[i]);
22+
}
23+
}
24+
}
25+
26+
int answer = 0;
27+
for(int i = 0; i < N; i++) {
28+
answer = Math.max(answer, dp[i]);
29+
}
30+
31+
System.out.println(answer);
32+
}
33+
}

0 commit comments

Comments
 (0)