Skip to content

Commit 9fe90a1

Browse files
committed
feat: add solutions to lc problem: No.0300. Longest Increasing
Subsequence
1 parent 53a5b6d commit 9fe90a1

File tree

7 files changed

+140
-23
lines changed

7 files changed

+140
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@
194194
- [最小路径和](./solution/0000-0099/0064.Minimum%20Path%20Sum/README.md)
195195
- [最长回文子串](./solution/0000-0099/0005.Longest%20Palindromic%20Substring/README.md)
196196
- [最长回文子序列](.solution/0500-0599/0516.Longest%20Palindromic%20Subsequence/README.md)
197+
- [最长递增子序列](./solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md)
197198
- [礼物的最大价值](./lcof/面试题47.%20礼物的最大价值/README.md)
198-
- [最长上升子序列](./solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md)
199199
- [俄罗斯套娃信封问题](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README.md)
200200
- [最长公共子序列](./solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md)
201201

solution/0300-0399/0300.Longest Increasing Subsequence/README.md

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,13 @@
5151
<li>你能将算法的时间复杂度降低到 <code>O(n log(n))</code> 吗?</li>
5252
</ul>
5353

54-
5554
## 解法
5655

5756
<!-- 这里可写通用的实现逻辑 -->
5857

5958
动态规划求解。
6059

61-
定义 `dp[i]` 为以 `nums[i]` 结尾的最长子序列的长度。即题目求的是 `dp[i]``i ∈[0, n-1]`)的最大值。
60+
定义 `dp[i]` 为以 `nums[i]` 结尾的最长子序列的长度`dp[i]` 初始化为 1(`i∈[0, n)`)。即题目求的是 `dp[i]``i ∈[0, n-1]`)的最大值。
6261

6362
状态转移方程为:
6463

@@ -74,8 +73,6 @@
7473
class Solution:
7574
def lengthOfLIS(self, nums: List[int]) -> int:
7675
n = len(nums)
77-
if n < 2:
78-
return n
7976
dp = [1] * n
8077
res = 1
8178
for i in range(1, n):
@@ -110,6 +107,56 @@ class Solution {
110107
}
111108
```
112109

110+
### **C++**
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
int lengthOfLIS(vector<int>& nums) {
116+
int n = nums.size();
117+
vector<int> dp(n, 1);
118+
int res = 1;
119+
for (int i = 1; i < n; ++i) {
120+
for (int j = 0; j < i; ++j) {
121+
if (nums[j] < nums[i]) {
122+
dp[i] = max(dp[i], dp[j] + 1);
123+
}
124+
}
125+
res = max(res, dp[i]);
126+
}
127+
return res;
128+
}
129+
};
130+
```
131+
132+
### **Go**
133+
134+
```go
135+
func lengthOfLIS(nums []int) int {
136+
n := len(nums)
137+
dp := make([]int, n)
138+
dp[0] = 1
139+
res := 1
140+
for i := 1; i < n; i++ {
141+
dp[i] = 1
142+
for j := 0; j < i; j++ {
143+
if nums[j] < nums[i] {
144+
dp[i] = max(dp[i], dp[j]+1)
145+
}
146+
}
147+
res = max(res, dp[i])
148+
}
149+
return res
150+
}
151+
152+
func max(a, b int) int {
153+
if a > b {
154+
return a
155+
}
156+
return b
157+
}
158+
```
159+
113160
### **...**
114161

115162
```

solution/0300-0399/0300.Longest Increasing Subsequence/README_EN.md

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@
4747
<li>Could you improve it to <code>O(n log(n))</code> time complexity?</li>
4848
</ul>
4949

50-
5150
## Solutions
5251

52+
Dynamic programming.
53+
5354
<!-- tabs:start -->
5455

5556
### **Python3**
@@ -58,8 +59,6 @@
5859
class Solution:
5960
def lengthOfLIS(self, nums: List[int]) -> int:
6061
n = len(nums)
61-
if n < 2:
62-
return n
6362
dp = [1] * n
6463
res = 1
6564
for i in range(1, n):
@@ -92,6 +91,56 @@ class Solution {
9291
}
9392
```
9493

94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int lengthOfLIS(vector<int>& nums) {
100+
int n = nums.size();
101+
vector<int> dp(n, 1);
102+
int res = 1;
103+
for (int i = 1; i < n; ++i) {
104+
for (int j = 0; j < i; ++j) {
105+
if (nums[j] < nums[i]) {
106+
dp[i] = max(dp[i], dp[j] + 1);
107+
}
108+
}
109+
res = max(res, dp[i]);
110+
}
111+
return res;
112+
}
113+
};
114+
```
115+
116+
### **Go**
117+
118+
```go
119+
func lengthOfLIS(nums []int) int {
120+
n := len(nums)
121+
dp := make([]int, n)
122+
dp[0] = 1
123+
res := 1
124+
for i := 1; i < n; i++ {
125+
dp[i] = 1
126+
for j := 0; j < i; j++ {
127+
if nums[j] < nums[i] {
128+
dp[i] = max(dp[i], dp[j]+1)
129+
}
130+
}
131+
res = max(res, dp[i])
132+
}
133+
return res
134+
}
135+
136+
func max(a, b int) int {
137+
if a > b {
138+
return a
139+
}
140+
return b
141+
}
142+
```
143+
95144
### **...**
96145

97146
```
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
class Solution {
22
public:
33
int lengthOfLIS(vector<int>& nums) {
4-
if (nums.size() == 0)
5-
return 0 ;
6-
int M[nums.size()] = {0, } ;
7-
int MM = -1 ;
8-
for (int i = nums.size()-1; i >= 0; --i)
9-
{
10-
for (int j = i+1; j < nums.size(); ++j)
11-
if (nums[i] < nums[j])
12-
M[i] = max(M[i], M[j]) ;
13-
MM = max(++M[i], MM) ;
4+
int n = nums.size();
5+
vector<int> dp(n, 1);
6+
int res = 1;
7+
for (int i = 1; i < n; ++i) {
8+
for (int j = 0; j < i; ++j) {
9+
if (nums[j] < nums[i]) {
10+
dp[i] = max(dp[i], dp[j] + 1);
11+
}
12+
}
13+
res = max(res, dp[i]);
1414
}
15-
return MM ;
15+
return res;
1616
}
17-
};
17+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func lengthOfLIS(nums []int) int {
2+
n := len(nums)
3+
dp := make([]int, n)
4+
dp[0] = 1
5+
res := 1
6+
for i := 1; i < n; i++ {
7+
dp[i] = 1
8+
for j := 0; j < i; j++ {
9+
if nums[j] < nums[i] {
10+
dp[i] = max(dp[i], dp[j]+1)
11+
}
12+
}
13+
res = max(res, dp[i])
14+
}
15+
return res
16+
}
17+
18+
func max(a, b int) int {
19+
if a > b {
20+
return a
21+
}
22+
return b
23+
}

solution/0300-0399/0300.Longest Increasing Subsequence/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public int lengthOfLIS(int[] nums) {
77
for (int i = 1; i < n; ++i) {
88
for (int j = 0; j < i; ++j) {
99
if (nums[j] < nums[i]) {
10-
dp[i] = Math.max(dp[i], dp[j] + 1);
10+
dp[i] = Math.max(dp[i], dp[j] + 1);
1111
}
1212
}
1313
res = Math.max(res, dp[i]);

solution/0300-0399/0300.Longest Increasing Subsequence/Solution.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
class Solution:
22
def lengthOfLIS(self, nums: List[int]) -> int:
33
n = len(nums)
4-
if n < 2:
5-
return n
64
dp = [1] * n
75
res = 1
86
for i in range(1, n):

0 commit comments

Comments
 (0)