File tree 9 files changed +30
-58
lines changed
0300-0399/0300.Longest Increasing Subsequence
1100-1199/1143.Longest Common Subsequence
9 files changed +30
-58
lines changed Original file line number Diff line number Diff line change @@ -74,13 +74,11 @@ class Solution:
74
74
def lengthOfLIS (self , nums : List[int ]) -> int :
75
75
n = len (nums)
76
76
dp = [1 ] * n
77
- res = 1
78
77
for i in range (1 , n):
79
78
for j in range (i):
80
79
if nums[j] < nums[i]:
81
80
dp[i] = max (dp[i], dp[j] + 1 )
82
- res = max (res, dp[i])
83
- return res
81
+ return max (dp)
84
82
```
85
83
86
84
### ** Java**
@@ -132,16 +130,14 @@ public:
132
130
int lengthOfLIS(vector<int >& nums) {
133
131
int n = nums.size();
134
132
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);
141
138
}
142
- res = max(res, dp[ i] );
143
139
}
144
- return res ;
140
+ return * max_element(dp.begin(), dp.end()) ;
145
141
}
146
142
};
147
143
```
Original file line number Diff line number Diff line change @@ -60,13 +60,11 @@ class Solution:
60
60
def lengthOfLIS (self , nums : List[int ]) -> int :
61
61
n = len (nums)
62
62
dp = [1 ] * n
63
- res = 1
64
63
for i in range (1 , n):
65
64
for j in range (i):
66
65
if nums[j] < nums[i]:
67
66
dp[i] = max (dp[i], dp[j] + 1 )
68
- res = max (res, dp[i])
69
- return res
67
+ return max (dp)
70
68
```
71
69
72
70
### ** Java**
@@ -116,16 +114,14 @@ public:
116
114
int lengthOfLIS(vector<int >& nums) {
117
115
int n = nums.size();
118
116
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);
125
122
}
126
- res = max(res, dp[ i] );
127
123
}
128
- return res ;
124
+ return * max_element(dp.begin(), dp.end()) ;
129
125
}
130
126
};
131
127
```
Original file line number Diff line number Diff line change @@ -3,15 +3,13 @@ class Solution {
3
3
int lengthOfLIS (vector<int >& nums) {
4
4
int n = nums.size ();
5
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
- }
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 );
12
11
}
13
- res = max (res, dp[i]);
14
12
}
15
- return res ;
13
+ return * max_element (dp. begin (), dp. end ()) ;
16
14
}
17
15
};
Original file line number Diff line number Diff line change @@ -2,10 +2,8 @@ class Solution:
2
2
def lengthOfLIS (self , nums : List [int ]) -> int :
3
3
n = len (nums )
4
4
dp = [1 ] * n
5
- res = 1
6
5
for i in range (1 , n ):
7
6
for j in range (i ):
8
7
if nums [j ] < nums [i ]:
9
8
dp [i ] = max (dp [i ], dp [j ] + 1 )
10
- res = max (res , dp [i ])
11
- return res
9
+ return max (dp )
Original file line number Diff line number Diff line change 62
62
63
63
递推公式如下:
64
64
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 )
66
66
67
67
<!-- tabs:start -->
68
68
@@ -119,14 +119,8 @@ public:
119
119
{
120
120
for (int j = 1; j <= n; ++j)
121
121
{
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 ] );
130
124
}
131
125
}
132
126
return dp[ m] [ n ] ;
Original file line number Diff line number Diff line change 52
52
53
53
Dynamic programming.
54
54
55
+ ![ ] ( https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1100-1199/1143.Longest%20Common%20Subsequence/images/CodeCogsEqn.gif )
56
+
55
57
<!-- tabs:start -->
56
58
57
59
### ** Python3**
@@ -103,14 +105,8 @@ public:
103
105
{
104
106
for (int j = 1; j <= n; ++j)
105
107
{
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 ] );
114
110
}
115
111
}
116
112
return dp[ m] [ n ] ;
Original file line number Diff line number Diff line change @@ -7,14 +7,8 @@ class Solution {
7
7
{
8
8
for (int j = 1 ; j <= n; ++j)
9
9
{
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 ]);
18
12
}
19
13
}
20
14
return dp[m][n];
You can’t perform that action at this time.
0 commit comments