Hyunebee
연속되는 최대 부분수열 본문
public class Practice2 {
public static int solution(int[] arr) {
int[] dp = new int[arr.length + 1];
int result = 0;
for(int i = 1; i <= dp.length-1; i++){
dp[i] = 1; // 기본 비교횟수는 항상 1
for(int j = 1; j<i; j++){
if (arr[j-1] < arr[i-1]){// j부터 i까지 계속 증가하고 있는지 조사를 함
dp[i] = Math.max(dp[i], dp[j]+1);// 비교하면서 채워줌
}
}
result = Math.max(result, dp[i]);
}
return result;
}
public static void main(String[] args) {
// Test code
int[] arr = new int[]{10, 20, 30, 10, 50, 10};
System.out.println(solution(arr));
}
}
'Java > 코테' 카테고리의 다른 글
두 노드를 지나는 최소 비용 (0) | 2022.06.02 |
---|---|
배낭에 짐싸기 (0) | 2022.05.31 |
노드 정렬하기 (0) | 2022.05.26 |
연속된 수중 가장 큰값을 리턴하는 부분수열 (0) | 2022.05.26 |
최대 강연료는 얼마? (0) | 2022.05.19 |