Skip to content

Commit 6fb4279

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0300
1 parent 46c3e40 commit 6fb4279

File tree

6 files changed

+35
-42
lines changed

6 files changed

+35
-42
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133

134134
- [打家劫舍](/solution/0100-0199/0198.House%20Robber/README.md)
135135
- [打家劫舍 II](/solution/0200-0299/0213.House%20Robber%20II/README.md)
136+
- [最长上升子序列](/solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md)
136137

137138
### 混合问题
138139

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
122122

123123
- [House Robber](/solution/0100-0199/0198.House%20Robber/README_EN.md)
124124
- [House Robber II](/solution/0200-0299/0213.House%20Robber%20II/README_EN.md)
125+
- [Longest Increasing Subsequence](/solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README_EN.md)
125126

126127
### Misc
127128

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

+8-16
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,13 @@ class Solution:
4646
n = len(nums)
4747
if n < 2:
4848
return n
49-
dp = [0 for _ in range(n)]
50-
dp[0] = 1
49+
dp = [1 for _ in nums]
5150
res = 1
52-
for i in range(n):
53-
max_val = 0
51+
for i in range(1, n):
5452
for j in range(0, i):
5553
if nums[j] < nums[i]:
56-
max_val = max(max_val, dp[j])
57-
dp[i] = max_val + 1
58-
res = max(res, dp[i])
54+
dp[i] = max(dp[i], dp[j] + 1)
55+
res = max(res, dp[i])
5956
return res
6057
```
6158

@@ -66,25 +63,20 @@ class Solution:
6663
```java
6764
class Solution {
6865
public int lengthOfLIS(int[] nums) {
69-
int n = nums.length;
70-
if (n < 2) {
71-
return n;
72-
}
66+
int n;
67+
if ((n = nums.length) < 2) return n;
7368
int[] dp = new int[n];
74-
dp[0] = 1;
69+
Arrays.fill(dp, 1);
7570
int res = 1;
7671
for (int i = 1; i < n; ++i) {
77-
int maxVal = 0;
7872
for (int j = 0; j < i; ++j) {
7973
if (nums[j] < nums[i]) {
80-
maxVal = Math.max(maxVal, dp[j]);
74+
dp[i] = Math.max(dp[i], dp[j] + 1);
8175
}
8276
}
83-
dp[i] = maxVal + 1;
8477
res = Math.max(res, dp[i]);
8578
}
8679
return res;
87-
8880
}
8981
}
9082
```

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

+17-10
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,40 @@
3232
### **Python3**
3333

3434
```python
35-
35+
class Solution:
36+
def lengthOfLIS(self, nums: List[int]) -> int:
37+
n = len(nums)
38+
if n < 2:
39+
return n
40+
dp = [1 for _ in nums]
41+
res = 1
42+
for i in range(1, n):
43+
for j in range(0, i):
44+
if nums[j] < nums[i]:
45+
dp[i] = max(dp[i], dp[j] + 1)
46+
res = max(res, dp[i])
47+
return res
3648
```
3749

3850
### **Java**
3951

4052
```java
4153
class Solution {
4254
public int lengthOfLIS(int[] nums) {
43-
int n = nums.length;
44-
if (n < 2) {
45-
return n;
46-
}
55+
int n;
56+
if ((n = nums.length) < 2) return n;
4757
int[] dp = new int[n];
48-
dp[0] = 1;
58+
Arrays.fill(dp, 1);
4959
int res = 1;
5060
for (int i = 1; i < n; ++i) {
51-
int maxVal = 0;
5261
for (int j = 0; j < i; ++j) {
5362
if (nums[j] < nums[i]) {
54-
maxVal = Math.max(maxVal, dp[j]);
63+
dp[i] = Math.max(dp[i], dp[j] + 1);
5564
}
5665
}
57-
dp[i] = maxVal + 1;
5866
res = Math.max(res, dp[i]);
5967
}
6068
return res;
61-
6269
}
6370
}
6471
```
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
class Solution {
22
public int lengthOfLIS(int[] nums) {
3-
int n = nums.length;
4-
if (n < 2) {
5-
return n;
6-
}
3+
int n;
4+
if ((n = nums.length) < 2) return n;
75
int[] dp = new int[n];
8-
dp[0] = 1;
6+
Arrays.fill(dp, 1);
97
int res = 1;
108
for (int i = 1; i < n; ++i) {
11-
int maxVal = 0;
129
for (int j = 0; j < i; ++j) {
1310
if (nums[j] < nums[i]) {
14-
maxVal = Math.max(maxVal, dp[j]);
11+
dp[i] = Math.max(dp[i], dp[j] + 1);
1512
}
1613
}
17-
dp[i] = maxVal + 1;
1814
res = Math.max(res, dp[i]);
1915
}
2016
return res;
21-
2217
}
2318
}

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ def lengthOfLIS(self, nums: List[int]) -> int:
33
n = len(nums)
44
if n < 2:
55
return n
6-
dp = [0 for _ in range(n)]
7-
dp[0] = 1
6+
dp = [1 for _ in nums]
87
res = 1
9-
for i in range(n):
10-
max_val = 0
8+
for i in range(1, n):
119
for j in range(0, i):
1210
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])
11+
dp[i] = max(dp[i], dp[j] + 1)
12+
res = max(res, dp[i])
1613
return res

0 commit comments

Comments
 (0)