[Hacker Rank] The Longest Increasing Subsequence

The Longest Increasing Subsequence

  • Time : O(nlogn)
  • Space : O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
int longestIncreasingSubsequence(vector<int> arr) {
vector<int> lis;
for(auto& a : arr) {
if(lis.empty() or lis.back() < a) lis.push_back(a);
else {
auto lb = lower_bound(begin(lis), end(lis), a);
*lb = a;
}
}

return lis.size();
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/11/PS/HackerRank/longest-increasing-subsequent/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.