We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e461ea1 commit dd2cee8Copy full SHA for dd2cee8
1 file changed
longest-increasing-subsequence/hwi-middle.cpp
@@ -1,3 +1,4 @@
1
+// O(n^2) 풀이
2
class Solution {
3
public:
4
int lengthOfLIS(vector<int>& nums) {
@@ -21,3 +22,27 @@ class Solution {
21
22
return *max_element(d.begin(), d.end());
23
}
24
};
25
+
26
+// O(n log n) 풀이
27
+class Solution {
28
+public:
29
+ int lengthOfLIS(vector<int>& nums) {
30
+ int n = nums.size();
31
+ vector<int> d; // d[i] = 길이가 i + 1인 LIS의 가장 작은 마지막 값
32
33
+ for (int i = 0; i < n; ++i)
34
+ {
35
+ auto it = lower_bound(d.begin(), d.end(), nums[i]);
36
+ if (it == d.end()) // nums[i]가 d의 모든 원소보다 큰 경우
37
38
+ d.push_back(nums[i]);
39
+ }
40
+ else // nums[i]의 자리를 찾은 경우
41
42
+ *it = nums[i]; // 그냥 대체하는게 이득임 -> 길이는 유지됐고, 더 이어붙일 수 있는 수의 범위는 늘어나므로
43
44
45
46
+ return d.size();
47
48
+};
0 commit comments