forked from jnozsc/lintcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongest_Increasing_Subsequence.cpp
43 lines (37 loc) · 1.04 KB
/
Longest_Increasing_Subsequence.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Given a sequence of integers, find the longest increasing subsequence (LIS).
You code should return the length of the LIS.
Example
For [5, 4, 1, 2, 3], the LIS is [1, 2, 3], return 3
For [4, 2, 4, 5, 3, 7], the LIS is [4, 4, 5, 7], return 4
Challenge
Time complexity O(n^2) or O(nlogn)
*/
#include <vector>
using namespace std;
class Solution {
public:
/**
* @param nums: The integer array
* @return: The length of LIS (longest increasing subsequence)
*/
int longestIncreasingSubsequence(vector<int> nums) {
// write your code here
if (nums.empty()) {
return 0;
}
vector<int> dp(nums.size(), 1);
for (int i = 1; i < nums.size(); i++) {
for (int j = 0; j < i; j++) {
if (nums[i] >= nums[j] && dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
}
}
}
int result = 1;
for (int i = 0; i < dp.size(); i++) {
result = max(result, dp[i]);
}
return result;
}
};