This is one of those problems where the clean idea matters more than the amount of code. For 300. Longest Increasing Subsequence, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming, binary search, two pointers, sliding window.
The notes already sitting in the source point us in the right direction:
- DP
- time: O(N^2), space: O(N)
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are lengthOfLIS.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(NlogN), space: O(N)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//DP
02//Runtime: 80 ms, faster than 27.26% of C++ online submissions for Longest Increasing Subsequence.
03//Memory Usage: 7.9 MB, less than 100.00% of C++ online submissions for Longest Increasing Subsequence.
04//time: O(N^2), space: O(N)
05class Solution {
06public:
07 int lengthOfLIS(vector<int>& nums) {
08 int n = nums.size();
09 if(n == 0) return 0;
10
11 //note that the shortest possible increasing sequence's length is 1!
12 //dp[i]: length of longest increasing subsequence ending at i
13 vector<int> dp(n, 1);
14 int ans = 1;
15
16 for(int i = 0; i < n; i++){
17 for(int j = 0; j < i; j++){
18 /*
19 before nums[i], add the increasing subsequence ending at j
20 */
21 if(nums[i] > nums[j]){
22 dp[i] = max(dp[i], dp[j]+1);
23 }
24 }
25 // cout << dp[i] << " ";
26 ans = max(ans, dp[i]);
27 }
28 // cout << endl;
29
30 return ans;
31 }
32};
33
34//DP + binary search
35//Runtime: 4 ms, faster than 89.41% of C++ online submissions for Longest Increasing Subsequence.
36//Memory Usage: 7.9 MB, less than 100.00% of C++ online submissions for Longest Increasing Subsequence.
37//https://algorithmsandme.com/longest-increasing-subsequence-in-onlogn/
38//https://github.com/keineahnung2345/fucking-algorithm/blob/note/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E7%B3%BB%E5%88%97/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E8%AE%BE%E8%AE%A1%EF%BC%9A%E6%9C%80%E9%95%BF%E9%80%92%E5%A2%9E%E5%AD%90%E5%BA%8F%E5%88%97.md
39//time: O(NlogN), space: O(N)
40class Solution {
41public:
42 int lengthOfLIS(vector<int>& nums) {
43 int n = nums.size();
44 if(n == 0) return 0;
45
46 vector<int> top;
47
48 for(int poker : nums){
49 int left = 0, right = top.size()-1;
50 /*
51 find the smallest top poker that is greater than current poker,
52 so we are finding lower bound,
53 that means we should focus on "left"
54 */
55 while(left <= right){
56 int mid = left + (right-left)/2;
57 if(poker < top[mid]){
58 right = mid - 1;
59 }else if(poker == top[mid]){
60 right = mid - 1;
61 }else if(poker > top[mid]){
62 left = mid+1;
63 }
64 }
65
66 if(left == top.size()){
67 //poker is larger than all cards in top
68 top.push_back(poker);
69 }else{
70 //overlay current poker to the specific pile
71 top[left] = poker;
72 }
73
74 }
75
76 return top.size();
77 }
78};
79
80//Approach 1: Brute Force, Recursion
81//TLE
82//21 / 24 test cases passed.
83//time: O(2^N), because for each element, there are 2 cases: taken or nottaken
84class Solution {
85public:
86 int lengthOfLIS(vector<int>& nums, int prev = -1, int cur = 0) {
87 /*
88 nums[cur] is the element we consider to append,
89 it cannot be out of the range of nums
90 */
91 if(cur == nums.size()) return 0;
92 int taken = 0;
93 //we may append current element into sequence, take
94 /*
95 initial state: prev is -1, and cur = 0,
96 that means we are considering the 0th element,
97 and the 0th element is always ok to be appended to the empty sequence
98 */
99 if(prev < 0 || nums[cur] > nums[prev]){
100 taken = 1 + lengthOfLIS(nums, cur, cur+1);
101 }
102 //skip current element, not take
103 int nottaken = lengthOfLIS(nums, prev, cur+1);
104 // cout << prev << ", " << cur << " : " << taken << ", " << nottaken << endl;
105 return max(taken, nottaken);
106 }
107};
108
109//Approach 2: Recursion with Memoization
110//Runtime: 748 ms, faster than 5.13% of C++ online submissions for Longest Increasing Subsequence.
111//Memory Usage: 110.3 MB, less than 6.25% of C++ online submissions for Longest Increasing Subsequence.
112//time: O(N^2), space: O(N^2)
113class Solution {
114public:
115 vector<vector<int>> memo;
116
117 int lengthOfLIS(vector<int>& nums, int prev, int cur) {
118 /*
119 nums[cur] is the element we consider to append,
120 it cannot be out of the range of nums
121 */
122 if(cur == nums.size()) return 0;
123 if(memo[prev+1][cur] != -1) return memo[prev+1][cur];
124 int taken = 0;
125 //we may append current element into sequence
126 /*
127 initial state: prev is -1, and cur = 0,
128 that means we are considering the 0th element,
129 and the 0th element is always ok to be appended to the empty sequence
130 */
131 if(prev < 0 || nums[cur] > nums[prev]){
132 taken = 1 + lengthOfLIS(nums, cur, cur+1);
133 }
134 //skip current element
135 int nottaken = lengthOfLIS(nums, prev, cur+1);
136 // cout << prev << ", " << cur << " : " << taken << ", " << nottaken << endl;
137 memo[prev+1][cur] = max(taken, nottaken);
138 return memo[prev+1][cur];
139 }
140
141 int lengthOfLIS(vector<int>& nums){
142 /*
143 padding ahead,
144 because prev may be -1
145 */
146 int n = nums.size();
147 memo = vector<vector<int>>(n+1, vector(n, -1));
148 return lengthOfLIS(nums, -1, 0);
149 }
150};
Cost