300 Longest Increasing Subsequence
1. Question
2. Implementation
class Solution {
public int lengthOfLIS(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int n = nums.length;
int[] LIS = new int[n];
Arrays.fill(LIS, 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i] && (LIS[j] + 1 > LIS[i])) {
LIS[i] = LIS[j] + 1;
}
}
}
int res = 0;
for (int e : LIS) {
res = Math.max(e, res);
}
return res;
}
}3. Time & Space Complexity
Last updated