[M] 300. Longest Increasing Subsequence

YJ·3일 전

leetcode

목록 보기
100/100

https://leetcode.com/problems/longest-increasing-subsequence/

문제

배열에서 존재하는 가장 긴 increasing subsequence 의 길이

답안

public int lengthOfLIS(int[] nums) {
	int n = nums.length;
	int[] dp = new int[n];
	Arrays.fill(dp, 1);
	int answer = 1;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < i; j++) {
			if (nums[j] < nums[i]) {
				dp[i] = Math.max(dp[i], dp[j] + 1);
			}
		}
		answer = Math.max(answer, dp[i]);
	}
	return answer;
}

메모

dp[i] = nums[i] 를 마지막 원소로 사용하는 가장 긴 증가 부분 수열의 길이

dp[i] = max(dp[i], dp[j] + 1)
profile
Hello

0개의 댓글