Dynamic Programming
Dynamic Programming
1. LIS
(1) Algorithm
// Time: O(n^2), Space: O(n)
public int lengthOfLIS(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
// LIS[i] indicate the longest increasing subsequence found at index i
int[] LIS = new int[nums.length];
Arrays.fill(LIS, 1);
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j] && (LIS[j] + 1) > LIS[i]) {
LIS[i] = LIS[j] + 1;
}
}
}
int max = 0;
for (int i = 0; i < LIS.length; i++) {
max = Math.max(LIS[i], max);
}
return max
}(2) Optimization: DP + Binary Search
(3) Questions
Longest Increasing Subsequence
2. Longest Common Subsequence/ Longest Common Substring
(1) Algorithm
(2) Questions
Edit Distance, [Palindrome Partitioning II](https://leetcode.com/problems/palindrome-partitioning-ii/?tab=Description\)
3. Backpack Problem
Last updated
Was this helpful?