문제 내용
https://www.acmicpc.net/problem/11053
11053번: 가장 긴 증가하는 부분 수열
수열 A가 주어졌을 때, 가장 긴 증가하는 부분 수열을 구하는 프로그램을 작성하시오. 예를 들어, 수열 A = {10, 20, 10, 30, 20, 50} 인 경우에 가장 긴 증가하는 부분 수열은 A = {10, 20, 10, 30, 20, 50} 이
www.acmicpc.net
풀이 시간
10분
풀이 과정
DP를 이용해 해결
정답 코드
1. 파이썬
import sys
sys_input=sys.stdin.readline
n=int(sys_input())
arr=list(map(int,sys_input().rstrip().split()))
dp=[1]
for i in range(1,n):
find=0
for j in range(i):
if arr[i]>arr[j]:
find=max(find,dp[j])
dp.append(find+1)
print(max(dp))
2. 자바
import java.io.*;
import java.util.*;
public class Main {
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[] arr = new int[n];
int[] dp = new int[n];
for (int i = 0; i < n; i++) {
int x = Integer.parseInt(st.nextToken());
int find = 0;
for (int j = 0; j < i; j++) {
if (arr[j] < x) {
find = Math.max(find, dp[j]);
}
}
arr[i] = x;
dp[i] = find + 1;
}
System.out.println(Arrays.stream(dp).max().getAsInt());
}
}
'코딩테스트 > 문제' 카테고리의 다른 글
[백준] 15650번: N과 M (2) (0) | 2023.12.19 |
---|---|
[백준] 1932번: 정수 삼각형 (0) | 2023.12.19 |
[백준] 1149번: RGB거리 (0) | 2023.12.18 |
여기서 부터 다시 풀기 (0) | 2023.12.18 |
[백준] 10026번 적록색약 (0) | 2023.07.03 |