Skip to content

Commit 01e61d9

Browse files
committed
update 300
1 parent 96e2dfb commit 01e61d9

File tree

3 files changed

+112
-91
lines changed

3 files changed

+112
-91
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
6767
## Binary Search
6868
| Problem | Solution | Time | Space | Note | Ref |
6969
| ----- | ----- | ----- | ----- | ----- | ----- |
70+
| [Leetcode-300. Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/description/) | [c++](./leetcode/300.longest-increasing-subsequence.cpp), [python](./leetcode/300.longest-increasing-subsequence.py) | O\(N\*logN\) | O\(N\) | \- | - |
7071
| [Leetcode-3048. Earliest Second To Mark Indices I](https://leetcode.com/problems/earliest-second-to-mark-indices-i/description/) | [c++](./leetcode/3048.earliest-second-to-mark-indices-i.cpp), [python](./leetcode/3048.earliest-second-to-mark-indices-i.py) | O\(M\*logM\) | O\(M\) | \- | - |
7172
| [Leetcode-2468. Split Message Based On Limit](https://leetcode.com/problems/split-message-based-on-limit/description/) | [c++](./leetcode/2468.split-message-based-on-limit.cpp), [python](./leetcode/2468.split-message-based-on-limit.py) | O\(logN \+ K\) | O\(1\) | Answer \| Bruteforce | - |
7273
| [Lintcode-437. Copy Books](https://www.lintcode.com/problem/copy-books) | [c++](./lintcode/437.copy-books.cpp), [python](./lintcode/437.copy-books.py) | O\(N\*logP\) | O\(1\) | Answer \| DP | - |
@@ -311,7 +312,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
311312
| [Leetcode-288. Unique Word Abbreviation](https://leetcode.com/problems/unique-word-abbreviation/description/) | [c++](./leetcode/288.unique-word-abbreviation.cpp), [python](./leetcode/288.unique-word-abbreviation.py) | \- | \- | \- | - |
312313
| [Leetcode-289. Game Of Life](https://leetcode.com/problems/game-of-life/description/) | [python](./leetcode/289.game-of-life.py) | \- | \- | \- | - |
313314
| [Leetcode-295. Find Median From Data Stream](https://leetcode.com/problems/find-median-from-data-stream/description/) | [c++](./leetcode/295.find-median-from-data-stream.cpp), [python](./leetcode/295.find-median-from-data-stream.py) | \- | \- | \- | - |
314-
| [Leetcode-300. Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/description/) | [c++](./leetcode/300.longest-increasing-subsequence.cpp), [python](./leetcode/300.longest-increasing-subsequence.py) | \- | \- | \- | - |
315315
| [Leetcode-305. Number Of Islands II](https://leetcode.com/problems/number-of-islands-ii/description/) | [c++](./leetcode/305.number-of-islands-ii.cpp), [python](./leetcode/305.number-of-islands-ii.py) | \- | \- | \- | - |
316316
| [Leetcode-311. Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/description/) | [c++](./leetcode/311.sparse-matrix-multiplication.cpp), [python](./leetcode/311.sparse-matrix-multiplication.py) | \- | \- | \- | - |
317317
| [Leetcode-312. Burst Balloons](https://leetcode.com/problems/burst-balloons/description/) | [c++](./leetcode/312.burst-balloons.cpp), [python](./leetcode/312.burst-balloons.py) | \- | \- | \- | - |
@@ -438,5 +438,5 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
438438
Total sovled: **320**<br/>
439439
PS: semicolon(;) after each note means related questions are checked<br/>
440440
<br/>
441-
Auto updated at: **2024-06-25 11:34:45**<br/>
441+
Auto updated at: **2024-06-25 15:23:43**<br/>
442442

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,70 @@
1-
// O(N * logN)
2-
// http://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/
1+
// Category: Array, Binary Search, Dynamic Programming
2+
// Time: O(N*logN)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given an integer array nums, return the length of the longest strictly increasing subsequence.
8+
//  
9+
// Example 1:
10+
//
11+
// Input: nums = [10,9,2,5,3,7,101,18]
12+
// Output: 4
13+
// Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
14+
//
15+
// Example 2:
16+
//
17+
// Input: nums = [0,1,0,3,2,3]
18+
// Output: 4
19+
//
20+
// Example 3:
21+
//
22+
// Input: nums = [7,7,7,7,7,7,7]
23+
// Output: 1
24+
//
25+
//  
26+
// Constraints:
27+
//
28+
// 1 <= nums.length <= 2500
29+
// -104 <= nums[i] <= 104
30+
//
31+
//  
32+
// Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity?
33+
//
34+
335
class Solution {
436
public:
537
int lengthOfLIS(vector<int>& nums) {
6-
vector<int> table;
7-
for(auto i = 0; i < nums.size(); ++i)
8-
{
9-
auto it = lower_bound(table.begin(), table.end(), nums[i]);
10-
if (it == table.end())
11-
{
12-
table.push_back(nums[i]);
13-
}
14-
else
15-
{
16-
*it = nums[i];
38+
vector<int> res;
39+
for (int n : nums) {
40+
auto it = lower_bound(res.begin(), res.end(), n);
41+
if (it == res.end()) {
42+
res.push_back(n);
43+
} else {
44+
*it = n;
1745
}
1846
}
1947

20-
return table.size();
48+
return res.size();
2149
}
2250
};
2351

24-
// f(i) = max(1, f(j) + 1 if j < i and nums[j] < nums[i])
25-
// O(N ^ 2)
26-
class Solution2 {
52+
53+
class Solution {
2754
public:
2855
int lengthOfLIS(vector<int>& nums) {
29-
30-
if (nums.size() == 0)
31-
{
32-
return 0;
33-
}
34-
35-
int m = nums.size();
36-
vector<int> table(m, 0);
37-
table[0] = 1;
38-
39-
for(auto i = 1; i < m; ++i)
40-
{
41-
int value = 1;
42-
for(auto j = 0; j < i; ++j)
43-
{
44-
if (nums[j] < nums[i])
45-
{
46-
value = max(value, table[j] + 1);
56+
int n = nums.size();
57+
vector<int> dp(n, 1);
58+
59+
for (int i = 1; i < n; i++) {
60+
for (int j = 0; j < i; j++ ) {
61+
if (nums[i] > nums[j]) {
62+
dp[i] = max(dp[i], dp[j] + 1);
4763
}
4864
}
49-
table[i] = value;
5065
}
51-
52-
return *max_element(table.begin(), table.end());
66+
67+
return *max_element(dp.begin(), dp.end());
5368
}
5469
};
5570

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,74 @@
1-
# O(N * logN)
2-
# https://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/
3-
class Solution(object):
4-
def lengthOfLIS(self, nums):
5-
"""
6-
:type nums: List[int]
7-
:rtype: int
8-
"""
1+
# Category: Array, Binary Search, Dynamic Programming
2+
# Time: O(N*logN)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given an integer array nums, return the length of the longest strictly increasing subsequence.
8+
#  
9+
# Example 1:
10+
#
11+
# Input: nums = [10,9,2,5,3,7,101,18]
12+
# Output: 4
13+
# Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
14+
#
15+
# Example 2:
16+
#
17+
# Input: nums = [0,1,0,3,2,3]
18+
# Output: 4
19+
#
20+
# Example 3:
21+
#
22+
# Input: nums = [7,7,7,7,7,7,7]
23+
# Output: 1
24+
#
25+
#  
26+
# Constraints:
27+
#
28+
# 1 <= nums.length <= 2500
29+
# -104 <= nums[i] <= 104
30+
#
31+
#  
32+
# Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity?
33+
#
34+
35+
class Solution:
36+
# https://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/
37+
def lengthOfLIS(self, nums: List[int]) -> int:
938
res = []
1039
for n in nums:
11-
index = self.binary_search(res, n) # use bisect.bisect_left to reduce more runtime
12-
if index == -1:
13-
res.append(n)
14-
else:
40+
index = self.lower_bound(res, n)
41+
if index < len(res):
1542
res[index] = n
43+
else:
44+
res.append(n)
1645

1746
return len(res)
1847

19-
def binary_search(self, nums, target):
20-
#find element no less than target
21-
22-
if len(nums) == 0:
23-
return -1
2448

49+
def lower_bound(self, nums: List[int], target: int) -> int:
50+
# find the smallest index >= target
2551
start = 0
26-
end = len(nums) - 1
52+
end = len(nums)
53+
54+
while start < end:
55+
mid = start + (end - start) // 2
2756

28-
while(start + 1 < end):
29-
mid = start + (end - start) / 2
3057
if nums[mid] < target:
31-
start = mid
58+
start = mid + 1
3259
else:
3360
end = mid
3461

35-
if nums[start] >= target:
36-
return start
37-
38-
if nums[end] >= target:
39-
return end
40-
41-
return -1
42-
62+
return start
4363

44-
# f(i) = max(1, f(j) + 1 if j < i and nums[j] < nums[i])
45-
# O(N ^ 2)
46-
class Solution2(object):
47-
def lengthOfLIS(self, nums):
48-
"""
49-
:type nums: List[int]
50-
:rtype: int
51-
"""
52-
if nums is None or len(nums) == 0:
53-
return 0
54-
55-
m = len(nums)
56-
table = [0 for _ in range(m)]
57-
table[0] = 1
58-
59-
for i in range(1, m):
60-
value = 1
64+
class Solution:
65+
def lengthOfLIS(self, nums: List[int]) -> int:
66+
n = len(nums)
67+
dp = [1 for i in range(n)]
6168

69+
for i in range(1, n):
6270
for j in range(i):
63-
if nums[j] < nums[i]:
64-
value = max(value, table[j] + 1)
65-
66-
table[i] = value
71+
if nums[i] > nums[j]:
72+
dp[i] = max(dp[i], dp[j] + 1)
6773

68-
return max(table)
74+
return max(dp)

0 commit comments

Comments
 (0)