The dp: function is
dp[i] = Math.max(dp[i], dp[l] + 1);
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
/** | |
* @param nums: An integer array | |
* @return: The length of LIS (longest increasing subsequence) | |
*/ | |
public int longestIncreasingSubsequence(int[] nums) { | |
// write your code here | |
if (nums == null || nums.length == 0) { | |
return 0; | |
} | |
int max = 0; | |
int n = nums.length; | |
int[] dp = new int[n]; | |
Arrays.fill(dp, 1); | |
for (int i = 0; i < n; i++) { | |
for (int l = 0; l < i; l++) { | |
if (nums[i] > nums[l]) { | |
dp[i] = Math.max(dp[i], dp[l] + 1); | |
} | |
} | |
max = Math.max(max, dp[i]); | |
} | |
System.out.println("Arrays.toString(dp) = " + Arrays.toString(dp)); | |
return max; | |
} | |
} |
No comments:
Post a Comment