File tree Expand file tree Collapse file tree 3 files changed +56
-11
lines changed Expand file tree Collapse file tree 3 files changed +56
-11
lines changed Original file line number Diff line number Diff line change 80
80
* [ q5_最长回文子串] ( /src/动态规划/q5_最长回文子串 )
81
81
* [ q53_最大子序和] ( /src/动态规划/q53_最大子序和 )
82
82
* [ q118_杨辉三角] ( /src/动态规划/q118_杨辉三角 )
83
+ * [ q300_最长上升子序列] ( /src/动态规划/q300_最长上升子序列 )
83
84
* [ q746_使用最小花费爬楼梯] ( /src/动态规划/q746_使用最小花费爬楼梯 )
84
85
* [ q1277_统计全为1的正方形子矩阵] ( /src/动态规划/q1277_统计全为1的正方形子矩阵 )
85
86
Original file line number Diff line number Diff line change
1
+ package 动态规划 .q300_最长上升子序列 ;
2
+
3
+ /**
4
+ * 动态规划 dp[i]表示以i索引下标结束的最长上升子序列 o(n*log(n))
5
+ */
6
+ public class Solution {
7
+
8
+ public int lengthOfLIS (int [] nums ) {
9
+ if (nums == null || nums .length == 0 ) {
10
+ return 0 ;
11
+ }
12
+
13
+ if (nums .length == 1 ) {
14
+ return 1 ;
15
+ }
16
+
17
+ int n = nums .length ;
18
+ int [] dp = new int [n ];
19
+ int rs = 0 ;
20
+
21
+ for (int i = 0 ; i < n ; i ++) {
22
+ dp [i ] = 1 ;
23
+ int max = 0 ;
24
+ for (int j = i - 1 ; j >= 0 ; j --) {
25
+ if (nums [j ] < nums [i ] && dp [j ] > max ) {
26
+ max = dp [j ];
27
+ }
28
+ }
29
+ dp [i ] += max ;
30
+ if (dp [i ] > rs ) {
31
+ rs = dp [i ];
32
+ }
33
+ }
34
+ return rs ;
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments