File tree Expand file tree Collapse file tree 7 files changed +140
-23
lines changed
solution/0300-0399/0300.Longest Increasing Subsequence Expand file tree Collapse file tree 7 files changed +140
-23
lines changed Original file line number Diff line number Diff line change 194
194
- [ 最小路径和] ( ./solution/0000-0099/0064.Minimum%20Path%20Sum/README.md )
195
195
- [ 最长回文子串] ( ./solution/0000-0099/0005.Longest%20Palindromic%20Substring/README.md )
196
196
- [ 最长回文子序列] ( .solution/0500-0599/0516.Longest%20Palindromic%20Subsequence/README.md )
197
+ - [ 最长递增子序列] ( ./solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md )
197
198
- [ 礼物的最大价值] ( ./lcof/面试题47.%20礼物的最大价值/README.md )
198
- - [ 最长上升子序列] ( ./solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md )
199
199
- [ 俄罗斯套娃信封问题] ( ./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README.md )
200
200
- [ 最长公共子序列] ( ./solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md )
201
201
Original file line number Diff line number Diff line change 51
51
<li>你能将算法的时间复杂度降低到 <code>O(n log(n))</code> 吗?</li>
52
52
</ul >
53
53
54
-
55
54
## 解法
56
55
57
56
<!-- 这里可写通用的实现逻辑 -->
58
57
59
58
动态规划求解。
60
59
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] ` )的最大值。
62
61
63
62
状态转移方程为:
64
63
74
73
class Solution :
75
74
def lengthOfLIS (self , nums : List[int ]) -> int :
76
75
n = len (nums)
77
- if n < 2 :
78
- return n
79
76
dp = [1 ] * n
80
77
res = 1
81
78
for i in range (1 , n):
@@ -110,6 +107,56 @@ class Solution {
110
107
}
111
108
```
112
109
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
+
113
160
### ** ...**
114
161
115
162
```
Original file line number Diff line number Diff line change 47
47
<li>Could you improve it to <code>O(n log(n))</code> time complexity?</li>
48
48
</ul >
49
49
50
-
51
50
## Solutions
52
51
52
+ Dynamic programming.
53
+
53
54
<!-- tabs:start -->
54
55
55
56
### ** Python3**
58
59
class Solution :
59
60
def lengthOfLIS (self , nums : List[int ]) -> int :
60
61
n = len (nums)
61
- if n < 2 :
62
- return n
63
62
dp = [1 ] * n
64
63
res = 1
65
64
for i in range (1 , n):
@@ -92,6 +91,56 @@ class Solution {
92
91
}
93
92
```
94
93
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
+
95
144
### ** ...**
96
145
97
146
```
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
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]) ;
14
14
}
15
- return MM ;
15
+ return res ;
16
16
}
17
- };
17
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ public int lengthOfLIS(int[] nums) {
7
7
for (int i = 1 ; i < n ; ++i ) {
8
8
for (int j = 0 ; j < i ; ++j ) {
9
9
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 );
11
11
}
12
12
}
13
13
res = Math .max (res , dp [i ]);
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def lengthOfLIS (self , nums : List [int ]) -> int :
3
3
n = len (nums )
4
- if n < 2 :
5
- return n
6
4
dp = [1 ] * n
7
5
res = 1
8
6
for i in range (1 , n ):
You can’t perform that action at this time.
0 commit comments