Skip to content

Commit af33758

Browse files
authored
Create Longest Increasing Subsequence.cpp
1 parent c6cdbff commit af33758

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//problem statement https://leetcode.com/problems/longest-increasing-subsequence/
2+
3+
class Solution {
4+
public:
5+
int lengthOfLIS(vector<int>& nums) {
6+
vector<int> sub;
7+
for (int x : nums) {
8+
if (sub.empty() || sub[sub.size() - 1] < x) {
9+
sub.push_back(x);
10+
} else {
11+
auto it = lower_bound(sub.begin(), sub.end(), x); // Find the index of the smallest number >= x
12+
*it = x; // Replace that number with x
13+
}
14+
}
15+
return sub.size();
16+
}
17+
};

0 commit comments

Comments
 (0)