[BOJ][11053] 가장 긴 증가하는 부분 수열
Updated:
1. 문제 링크
https://www.acmicpc.net/problem/11053
2. 사용 알고리즘
DP
3. 풀이
d[n] : a[n]을 마지막으로 하는 가장 긴 증가하는 부분 수열의 길이
d[n] = max(n 앞의 수 중, a[n]보다 작은 수의 가장 긴 증가하는 부분 수열의 길이) + 1
d[n] = max(d[i]) + 1 (i < n, a[i] < a[n])
∴ max(d[i]) (1 <= i <= n)
4. 소스 코드
4-1. C++
4-1-1. Top-Down
https://github.com/dev-aiden/problem-solving/blob/main/boj/11053.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;
int d[1003], a[1003];
int solve(int n) {
if (n == 1) return 1;
if (d[n] > 0) return d[n];
d[n] = 1;
for (int i = n - 1; i >= 1; --i) {
if (a[n] > a[i] && d[n] < solve(i) + 1) d[n] = d[i] + 1;
}
return d[n];
}
int main(void) {
ios_base::sync_with_stdio(false);
int n; cin >> n;
int ans = 0;
d[1] = 1;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
solve(i);
}
for (int i = 1; i <= n; ++i) if(ans < d[i]) ans = d[i];
cout << ans << "\n";
return 0;
}
4-1-2. Bottom-Up
https://github.com/dev-aiden/problem-solving/blob/main/boj/11053_2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int d[1003], a[1003];
int main(void) {
ios_base::sync_with_stdio(false);
int n; cin >> n;
int ans = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i]; d[i] = 1;
for (int j = 1; j < i; ++j) {
if (a[j] < a[i] && d[j] + 1 > d[i]) d[i] = d[j] + 1;
}
if (ans < d[i]) ans = d[i];
}
cout << ans << "\n";
return 0;
}
4-2. JAVA
4-2-1. Top-Down
https://github.com/dev-aiden/problem-solving/blob/main/boj/11053.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int d[] = new int[1003];
static int a[] = new int[1003];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int ans = 0;
d[1] = 1;
for (int i = 1; i <= n; ++i) {
a[i] = Integer.parseInt(st.nextToken());
solve(i);
}
for (int i = 1; i <= n; ++i) if(ans < d[i]) ans = d[i];
System.out.println(ans);
}
public static int solve(int n) {
if (n == 1) return 1;
if (d[n] > 0) return d[n];
d[n] = 1;
for (int i = n - 1; i >= 1; --i) {
if (a[n] > a[i] && d[n] < solve(i) + 1) d[n] = d[i] + 1;
}
return d[n];
}
}
4-2-2. Bottom-Up
https://github.com/dev-aiden/problem-solving/blob/main/boj/11053_2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int d[] = new int[1003];
static int a[] = new int[1003];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int ans = 0;
for(int i = 1; i <= n; ++i) {
a[i] = Integer.parseInt(st.nextToken()); d[i] = 1;
for(int j = 1; j < i; ++j) {
if (a[j] < a[i] && d[j] + 1 > d[i]) d[i] = d[j] + 1;
}
if (ans < d[i]) ans = d[i];
}
System.out.println(ans);
}
}
Leave a comment