Skip to content

Commit d628207

Browse files
committed
feat: add python and java solutions to leetcode problem: No.300. Longest Increasing Subsequence
添加LeetCode题解:300. 最长上升子序列
1 parent d0a2816 commit d628207

File tree

4 files changed

+152
-56
lines changed

4 files changed

+152
-56
lines changed

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

+45-2
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,63 @@
2323

2424
## 解法
2525
<!-- 这里可写通用的实现逻辑 -->
26+
动态规划求解。
2627

28+
定义 `dp[i]` 为以 `nums[i]` 结尾的最长子序列的长度。即题目求的是 `dp[i]``i ∈[0, n-1]`)的最大值。
29+
30+
状态转移方程为:
31+
32+
`dp[i] = max(dp[j]) + 1`,其中 `0≤j<i``nums[j]<nums[i]`
2733

2834
### Python3
2935
<!-- 这里可写当前语言的特殊实现逻辑 -->
3036

3137
```python
32-
38+
class Solution:
39+
def lengthOfLIS(self, nums: List[int]) -> int:
40+
n = len(nums)
41+
if n < 2:
42+
return n
43+
dp = [0 for _ in range(n)]
44+
dp[0] = 1
45+
res = 1
46+
for i in range(n):
47+
max_val = 0
48+
for j in range(0, i):
49+
if nums[j] < nums[i]:
50+
max_val = max(max_val, dp[j])
51+
dp[i] = max_val + 1
52+
res = max(res, dp[i])
53+
return res
3354
```
3455

3556
### Java
3657
<!-- 这里可写当前语言的特殊实现逻辑 -->
3758

3859
```java
39-
60+
class Solution {
61+
public int lengthOfLIS(int[] nums) {
62+
int n = nums.length;
63+
if (n < 2) {
64+
return n;
65+
}
66+
int[] dp = new int[n];
67+
dp[0] = 1;
68+
int res = 1;
69+
for (int i = 1; i < n; ++i) {
70+
int maxVal = 0;
71+
for (int j = 0; j < i; ++j) {
72+
if (nums[j] < nums[i]) {
73+
maxVal = Math.max(maxVal, dp[j]);
74+
}
75+
}
76+
dp[i] = maxVal + 1;
77+
res = Math.max(res, dp[i]);
78+
}
79+
return res;
80+
81+
}
82+
}
4083
```
4184

4285
### ...
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,81 @@
1-
# [300. Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence)
2-
3-
## Description
4-
<p>Given an unsorted array of integers, find the length of longest increasing subsequence.</p>
5-
6-
<p><b>Example:</b></p>
7-
8-
<pre>
9-
<b>Input:</b> <code>[10,9,2,5,3,7,101,18]
10-
</code><b>Output: </b>4
11-
<strong>Explanation: </strong>The longest increasing subsequence is <code>[2,3,7,101]</code>, therefore the length is <code>4</code>. </pre>
12-
13-
<p><strong>Note: </strong></p>
14-
15-
<ul>
16-
<li>There may be more than one LIS combination, it is only necessary for you to return the length.</li>
17-
<li>Your algorithm should run in O(<i>n<sup>2</sup></i>) complexity.</li>
18-
</ul>
19-
20-
<p><b>Follow up:</b> Could you improve it to O(<i>n</i> log <i>n</i>) time complexity?</p>
21-
22-
23-
24-
## Solutions
25-
26-
27-
### Python3
28-
29-
```python
30-
31-
```
32-
33-
### Java
34-
35-
```java
36-
37-
```
38-
39-
### ...
40-
```
41-
42-
```
1+
# [300. Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence)
2+
3+
## Description
4+
<p>Given an unsorted array of integers, find the length of longest increasing subsequence.</p>
5+
6+
7+
8+
<p><b>Example:</b></p>
9+
10+
11+
12+
<pre>
13+
14+
<b>Input:</b> <code>[10,9,2,5,3,7,101,18]
15+
16+
</code><b>Output: </b>4
17+
18+
<strong>Explanation: </strong>The longest increasing subsequence is <code>[2,3,7,101]</code>, therefore the length is <code>4</code>. </pre>
19+
20+
21+
22+
<p><strong>Note: </strong></p>
23+
24+
25+
26+
<ul>
27+
28+
<li>There may be more than one LIS combination, it is only necessary for you to return the length.</li>
29+
30+
<li>Your algorithm should run in O(<i>n<sup>2</sup></i>) complexity.</li>
31+
32+
</ul>
33+
34+
35+
36+
<p><b>Follow up:</b> Could you improve it to O(<i>n</i> log <i>n</i>) time complexity?</p>
37+
38+
39+
40+
41+
## Solutions
42+
43+
44+
### Python3
45+
46+
```python
47+
48+
```
49+
50+
### Java
51+
52+
```java
53+
class Solution {
54+
public int lengthOfLIS(int[] nums) {
55+
int n = nums.length;
56+
if (n < 2) {
57+
return n;
58+
}
59+
int[] dp = new int[n];
60+
dp[0] = 1;
61+
int res = 1;
62+
for (int i = 1; i < n; ++i) {
63+
int maxVal = 0;
64+
for (int j = 0; j < i; ++j) {
65+
if (nums[j] < nums[i]) {
66+
maxVal = Math.max(maxVal, dp[j]);
67+
}
68+
}
69+
dp[i] = maxVal + 1;
70+
res = Math.max(res, dp[i]);
71+
}
72+
return res;
73+
74+
}
75+
}
76+
```
77+
78+
### ...
79+
```
80+
81+
```
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
class Solution {
22
public int lengthOfLIS(int[] nums) {
3-
if (nums == null) {
4-
return 0;
5-
}
63
int n = nums.length;
74
if (n < 2) {
85
return n;
96
}
10-
int[] res = new int[n];
11-
res[0] = 1;
12-
res[1] = nums[1] > nums[0] ? 2 : 1;
13-
int max = res[1];
14-
for (int i = 2; i < n; ++i) {
15-
res[i] = 1;
7+
int[] dp = new int[n];
8+
dp[0] = 1;
9+
int res = 1;
10+
for (int i = 1; i < n; ++i) {
11+
int maxVal = 0;
1612
for (int j = 0; j < i; ++j) {
1713
if (nums[j] < nums[i]) {
18-
res[i] = Math.max(res[i], res[j] + 1);
14+
maxVal = Math.max(maxVal, dp[j]);
1915
}
2016
}
21-
max = Math.max(max, res[i]);
17+
dp[i] = maxVal + 1;
18+
res = Math.max(res, dp[i]);
2219
}
23-
return max;
20+
return res;
21+
2422
}
2523
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def lengthOfLIS(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
if n < 2:
5+
return n
6+
dp = [0 for _ in range(n)]
7+
dp[0] = 1
8+
res = 1
9+
for i in range(n):
10+
max_val = 0
11+
for j in range(0, i):
12+
if nums[j] < nums[i]:
13+
max_val = max(max_val, dp[j])
14+
dp[i] = max_val + 1
15+
res = max(res, dp[i])
16+
return res

0 commit comments

Comments
 (0)