swexpertacademy 문제 바로가기 (로그인 후 이용가능)
먼저 LIS 문제는 DP로 푸는 것으로 공부했기 때문에
저장된 배열을 가지고 dp 배열을 만들어 i번째 dp값과 저장된 max값 중 최댓값을 도출한다.
import java.util.*;
public class Solution {
static int n;
static int max;
static int[] arr;
static int[] dp;
static StringBuffer sb = new StringBuffer();
public static void main(String[] args) throws Exception {
//LIS
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int tc = 1; tc <= T; tc++) {
max = 1;
n = sc.nextInt(); //수열의 길이
arr = new int[n];
dp = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
dp[0] = 1;
for (int i = 1; i < n; i++) {
dp[i] = 1; //기본값 1 초기화
for( int j=0;j < i;j++) { //현재위치-1 까지 작은 값
// 원소값이 전원소보다 크고, 전 dp값이 1보다 크거나 같으면
if (arr[j] < arr[i] && dp[i] <= dp[j])
dp[i] = dp[j] + 1;
}
max = Math.max(max, dp[i]);
}
sb.append("#" + tc).append(" ").append(max).append("\n");
}
System.out.println(sb);
}
}