Essential Algorithms and Data Structures: A Quick Reference
Classified in Computers
Written on in English with a size of 580.94 KB
Essential Algorithms and Data Structures
Longest Increasing Subsequence (LIS):
- Subproblem: dp[i] = length of LIS ending at index i
- Recursion: dp[i] = max(dp[j] + 1 for j < i and arr[j] < arr[i])
- Base case: dp[i] = 1 (every element is a subsequence of length 1)
- Time Complexity: O(n^2), O(n log n) with binary search optimization.
Longest Alternating Subsequence (LAS):
- Subproblem: dp[i][0] (increasing), dp[i][1] (decreasing)
- Recursion: dp[i][0] = max(dp[j][1] + 1 for j < i and arr[j] < arr[i]), dp[i][1] = max(dp[j][0] + 1 for j < i and arr[j] > arr[i])
- Base case: dp[0][0] = 1, dp[0][1] = 1
- Time Complexity: O(n^2)
0/1 Knapsack Problem:
- Subproblem: dp[i][w] = maximum value for the first i items and weight limit w
- Recursion: dp[i][w] = max(