Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 85827e5

Browse files
Merge pull request begeekmyfriend#22 from pratikgl/master
Implemented the O(logN) algorithm for LIS
2 parents 081c64b + 536455b commit 85827e5

File tree

1 file changed

+7
-15
lines changed
  • 0300_longest_increasing_subsequence

1 file changed

+7
-15
lines changed

‎0300_longest_increasing_subsequence/lis.cc‎

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,14 @@ using namespace std;
55
class Solution {
66
public:
77
int lengthOfLIS(vector<int>& nums) {
8-
vector<int> dp(nums.size(), 1);
9-
for (int i = 0; i < nums.size(); i++) {
10-
for (int j = 0; j < i; j++) {
11-
// nums[i] should be contained as the last element in subsequence.
12-
if (nums[j] < nums[i] && dp[j] + 1 > dp[i]) {
13-
dp[i] = dp[j] + 1;
14-
}
8+
vector<int> ans;
9+
for (int x : nums) {
10+
auto it = lower_bound(ans.begin(), ans.end(), x);
11+
if (it == ans.end()) {
12+
ans.push_back(x);
1513
}
14+
else *it = x;
1615
}
17-
18-
int res = 0;
19-
for (int i : dp) {
20-
if (i > res) {
21-
res = i;
22-
}
23-
}
24-
return res;
16+
return ans.size();
2517
}
2618
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /