Skip to content

Commit 5d340bf

Browse files
Add files via upload
1 parent f7fca34 commit 5d340bf

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// 典型的动态规划
2+
// 时间复杂度为n2的版本
3+
4+
// Runtime: 48 ms, faster than 56.04% of C++ online submissions for Longest Increasing Subsequence.
5+
// Memory Usage: 8.7 MB, less than 40.00% of C++ online submissions for Longest Increasing Subsequence.
6+
7+
class Solution
8+
{
9+
public:
10+
int lengthOfLIS(vector<int>& nums)
11+
{
12+
if (nums.size() <= 1)
13+
return nums.size();
14+
15+
int *res = new int[nums.size()];
16+
for (int i = 0; i < nums.size(); i++)
17+
res[i] = 1;
18+
19+
// res[i]代表以a[i]结尾的LIS的长度
20+
for (int i = 1; i < nums.size(); i++)
21+
{
22+
for (int j = i - 1; j >= 0; j--)
23+
{
24+
if (nums[i] > nums[j])
25+
res[i] = max(res[i], res[j] + 1);
26+
}
27+
}
28+
29+
// 遍历求取一下最大值
30+
int maxLength = INT_MIN;
31+
for (int i = 0; i < nums.size(); i++)
32+
maxLength = max(maxLength, res[i]);
33+
delete[] res;
34+
return maxLength;
35+
}
36+
};
37+
38+
// 时间复杂度为nlogn的版本
39+
// https://leetcode.com/problems/longest-increasing-subsequence/discuss/74824/JavaPython-Binary-search-O(nlogn)-time-with-explanation
40+
41+
// Runtime: 4 ms, faster than 100.00% of C++ online submissions for Longest Increasing Subsequence.
42+
// Memory Usage: 8.7 MB, less than 48.95% of C++ online submissions for Longest Increasing Subsequence.
43+
44+
class Solution
45+
{
46+
public:
47+
int lengthOfLIS(vector<int>& nums)
48+
{
49+
if (nums.size() <= 1)
50+
return nums.size();
51+
52+
vector<int> tails;
53+
tails.push_back(nums[0]);
54+
for (int i = 1; i < nums.size(); i++)
55+
{
56+
if (nums[i] > tails.back())
57+
tails.push_back(nums[i]);
58+
else
59+
{
60+
// 使用二分法确定需要修改的索引
61+
int leftPtr = 0, rightPtr = tails.size() - 1;
62+
while (leftPtr < rightPtr)
63+
{
64+
int midPtr = (leftPtr + rightPtr) / 2;
65+
if (tails[midPtr] < nums[i])
66+
leftPtr = midPtr + 1;
67+
else
68+
rightPtr = midPtr;
69+
}
70+
tails[leftPtr] = nums[i];
71+
}
72+
}
73+
return tails.size();
74+
}
75+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# nlogn 的py代码
2+
3+
# Runtime: 32 ms, faster than 100.00% of Python3 online submissions for Longest Increasing Subsequence.
4+
# Memory Usage: 12.5 MB, less than 93.78% of Python3 online submissions for Longest Increasing Subsequence.
5+
6+
class Solution:
7+
def lengthOfLIS(self, nums: 'List[int]') -> 'int':
8+
if len(nums) <= 1:
9+
return len(nums)
10+
11+
tails = []
12+
tails.append(nums[0])
13+
14+
for i in range(1, len(nums)):
15+
if nums[i] > tails[-1]:
16+
tails.append(nums[i])
17+
else:
18+
leftPtr, rightPtr = 0, len(tails) - 1
19+
while leftPtr < rightPtr:
20+
midPtr = (leftPtr + rightPtr) // 2
21+
if tails[midPtr] < nums[i]:
22+
leftPtr = midPtr + 1
23+
else:
24+
rightPtr = midPtr
25+
tails[leftPtr] = nums[i]
26+
27+
return len(tails)

0 commit comments

Comments
 (0)