## Longest Increasing Subsequence

Longest Increasing Subsequence.

Longest Increasing Subsequence finds the longest (not necessarily continuous) sub-sequence given a sequence.

Example:

Input: [1, 4, 2, 5] Output: 3

(Explanation: The length of the longest increasing subsequence can be found through finding either [1, 2, 5] or [1, 4, 5])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

class Solution {
public int lengthOfLIS(int[] nums) {
if (nums.length == 0) {
return 0;
}
int[] dp = new int[nums.length];
dp[0] = 1;
for (int i = 1; i < nums.length; i++) {
dp[i] = 1;
// nums[i] = current integer
for (int j = i - 1; j >= 0; j--) {
if (nums[j] < nums[i]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
int max = 0;
for (int i = 0; i < dp.length; i++) {
if (dp[i] > max) {
max = dp[i];
}
}
return max;
}
}