Skip to content

Commit 9ff2ad3

Browse files
committed
feat: update solutions to lc problems: No.0300,1143
1 parent d293aa6 commit 9ff2ad3

File tree

9 files changed

+30
-58
lines changed

9 files changed

+30
-58
lines changed

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

+7-11
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,11 @@ class Solution:
7474
def lengthOfLIS(self, nums: List[int]) -> int:
7575
n = len(nums)
7676
dp = [1] * n
77-
res = 1
7877
for i in range(1, n):
7978
for j in range(i):
8079
if nums[j] < nums[i]:
8180
dp[i] = max(dp[i], dp[j] + 1)
82-
res = max(res, dp[i])
83-
return res
81+
return max(dp)
8482
```
8583

8684
### **Java**
@@ -132,16 +130,14 @@ public:
132130
int lengthOfLIS(vector<int>& nums) {
133131
int n = nums.size();
134132
vector<int> dp(n, 1);
135-
int res = 1;
136-
for (int i = 1; i < n; ++i) {
137-
for (int j = 0; j < i; ++j) {
138-
if (nums[j] < nums[i]) {
139-
dp[i] = max(dp[i], dp[j] + 1);
140-
}
133+
for (int i = 1; i < n; ++i)
134+
{
135+
for (int j = 0; j < i; ++j)
136+
{
137+
if (nums[j] < nums[i]) dp[i] = max(dp[i], dp[j] + 1);
141138
}
142-
res = max(res, dp[i]);
143139
}
144-
return res;
140+
return *max_element(dp.begin(), dp.end());
145141
}
146142
};
147143
```

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

+7-11
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,11 @@ class Solution:
6060
def lengthOfLIS(self, nums: List[int]) -> int:
6161
n = len(nums)
6262
dp = [1] * n
63-
res = 1
6463
for i in range(1, n):
6564
for j in range(i):
6665
if nums[j] < nums[i]:
6766
dp[i] = max(dp[i], dp[j] + 1)
68-
res = max(res, dp[i])
69-
return res
67+
return max(dp)
7068
```
7169

7270
### **Java**
@@ -116,16 +114,14 @@ public:
116114
int lengthOfLIS(vector<int>& nums) {
117115
int n = nums.size();
118116
vector<int> dp(n, 1);
119-
int res = 1;
120-
for (int i = 1; i < n; ++i) {
121-
for (int j = 0; j < i; ++j) {
122-
if (nums[j] < nums[i]) {
123-
dp[i] = max(dp[i], dp[j] + 1);
124-
}
117+
for (int i = 1; i < n; ++i)
118+
{
119+
for (int j = 0; j < i; ++j)
120+
{
121+
if (nums[j] < nums[i]) dp[i] = max(dp[i], dp[j] + 1);
125122
}
126-
res = max(res, dp[i]);
127123
}
128-
return res;
124+
return *max_element(dp.begin(), dp.end());
129125
}
130126
};
131127
```

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

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ class Solution {
33
int lengthOfLIS(vector<int>& nums) {
44
int n = nums.size();
55
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-
}
6+
for (int i = 1; i < n; ++i)
7+
{
8+
for (int j = 0; j < i; ++j)
9+
{
10+
if (nums[j] < nums[i]) dp[i] = max(dp[i], dp[j] + 1);
1211
}
13-
res = max(res, dp[i]);
1412
}
15-
return res;
13+
return *max_element(dp.begin(), dp.end());
1614
}
1715
};

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ class Solution:
22
def lengthOfLIS(self, nums: List[int]) -> int:
33
n = len(nums)
44
dp = [1] * n
5-
res = 1
65
for i in range(1, n):
76
for j in range(i):
87
if nums[j] < nums[i]:
98
dp[i] = max(dp[i], dp[j] + 1)
10-
res = max(res, dp[i])
11-
return res
9+
return max(dp)

solution/1100-1199/1143.Longest Common Subsequence/README.md

+3-9
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
递推公式如下:
6464

65-
![](https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1100-1199/1143.Longest%20Common%20Subsequence/images/gif.gif)
65+
![](https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1100-1199/1143.Longest%20Common%20Subsequence/images/CodeCogsEqn.gif)
6666

6767
<!-- tabs:start -->
6868

@@ -119,14 +119,8 @@ public:
119119
{
120120
for (int j = 1; j <= n; ++j)
121121
{
122-
if (text1[i - 1] == text2[j - 1])
123-
{
124-
dp[i][j] = dp[i - 1][j - 1] + 1;
125-
}
126-
else
127-
{
128-
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
129-
}
122+
if (text1[i - 1] == text2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
123+
else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
130124
}
131125
}
132126
return dp[m][n];

solution/1100-1199/1143.Longest Common Subsequence/README_EN.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252

5353
Dynamic programming.
5454

55+
![](https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1100-1199/1143.Longest%20Common%20Subsequence/images/CodeCogsEqn.gif)
56+
5557
<!-- tabs:start -->
5658

5759
### **Python3**
@@ -103,14 +105,8 @@ public:
103105
{
104106
for (int j = 1; j <= n; ++j)
105107
{
106-
if (text1[i - 1] == text2[j - 1])
107-
{
108-
dp[i][j] = dp[i - 1][j - 1] + 1;
109-
}
110-
else
111-
{
112-
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
113-
}
108+
if (text1[i - 1] == text2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
109+
else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
114110
}
115111
}
116112
return dp[m][n];

solution/1100-1199/1143.Longest Common Subsequence/Solution.cpp

+2-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@ class Solution {
77
{
88
for (int j = 1; j <= n; ++j)
99
{
10-
if (text1[i - 1] == text2[j - 1])
11-
{
12-
dp[i][j] = dp[i - 1][j - 1] + 1;
13-
}
14-
else
15-
{
16-
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
17-
}
10+
if (text1[i - 1] == text2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
11+
else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
1812
}
1913
}
2014
return dp[m][n];
Loading
Binary file not shown.

0 commit comments

Comments
 (0)