Skip to content

Commit 671d379

Browse files
committed
add q300
1 parent 31aca68 commit 671d379

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

.idea/workspace.xml

Lines changed: 19 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
* [q5_最长回文子串](/src/动态规划/q5_最长回文子串)
8181
* [q53_最大子序和](/src/动态规划/q53_最大子序和)
8282
* [q118_杨辉三角](/src/动态规划/q118_杨辉三角)
83+
* [q300_最长上升子序列](/src/动态规划/q300_最长上升子序列)
8384
* [q746_使用最小花费爬楼梯](/src/动态规划/q746_使用最小花费爬楼梯)
8485
* [q1277_统计全为1的正方形子矩阵](/src/动态规划/q1277_统计全为1的正方形子矩阵)
8586

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}

0 commit comments

Comments
 (0)