76. Longest Increasing Subsequence
Tag:
Dynamic Problem
Main Idea:
Classic DP Problem. Let represents the number of longest increasing subsequence at the position i. For initialization, each element is assign as 1. As for update equation, it would be: for 0 < j < i < n,
Tips/Notes:
- Don’t forget to initialize the dp[i].
Time/Space Cost:
Time Cost:
Space Cost:
Code:
class Solution {
public:
/**
* @param nums: An integer array
* @return: The length of LIS (longest increasing subsequence)
*/
int longestIncreasingSubsequence(vector<int> &nums) {
// write your code here
int len = nums.size();
if(len == 0) return 0;
int res = 0;
vector<int> dp(len+1, 0);
for(int i = 0; i < len; i++){
dp[i] = 1;
for(int j = 0; j < i; j++){
if(nums[i] > nums[j]){
dp[i] = max(dp[i], dp[j] + 1);
}
res = max(res, dp[i]);
}
}
return res;
}
};
Followup Problem: 398. Longest Continuous Increasing Subsequence II
Main Idea:
DP-Memorization Problem. In this problem, we need to use to record each position’s information. Let be the longest increasing subsequence length ending at row i, column j (i, j). For initialization, default value of will be -1, when it’s in SEARCH() function, set as 1.
Then, update equation, let (nx, ny) be one of four directions of (i,j), IF , .
The answer will be the maximum element in .
Tips/Notes:
- 2D vector definition: vector<vector>(row, vector(col, -1));
- Note the value of . The transition would be (-1 -> 1 -> maxValue). When , it means this element haven’t been searched; When , it means begin to search this element; When , it means this element has been searched.
Time/Space Cost:
Time Cost:
Space Cost:
Code:
class Solution {
public:
/**
* @param matrix: A 2D-array of integers
* @return: an integer
*/
vector<vector<int>> dp;
int row, col;
int longestContinuousIncreasingSubsequence2(vector<vector<int>> &matrix) {
// write your code here
row = matrix.size();
col = (row == 0)? 0 : matrix[0].size();
// default as -1, which means haven't searched
dp = vector<vector<int>>(row, vector<int>(col, -1));
int res = 0;
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
// search row i, column j
search(matrix, i, j);
res = max(res, dp[i][j]);
}
}
return res;
}
void search(vector<vector<int>> &matrix, int x, int y){
if(dp[x][y] != -1)
return;
// direction vector
vector<int> d = {0,1,0,-1,0};
// NOTE HERE: must initializa the dp[x][y]
dp[x][y] = 1;
for(int i = 0; i < 5; i++){
int nx = x + d[i];
int ny = y + d[i+1];
if(nx >= 0 && nx < row && ny >= 0 && ny < col){
if(matrix[nx][ny] < matrix[x][y])
{
// MUST search dp[nx][ny] first
search(matrix, nx, ny);
dp[x][y] = max(dp[x][y], dp[nx][ny] + 1);
}
}
}
}
};