-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLongestIncreasingSubsequence.java
More file actions
100 lines (94 loc) · 2.89 KB
/
Copy pathLongestIncreasingSubsequence.java
File metadata and controls
100 lines (94 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* <p>给你一个整数数组 <code>nums</code> ,找到其中最长严格递增子序列的长度。</p>
*
* <p>子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,<code>[3,6,2,7]</code> 是数组 <code>[0,3,1,6,2,2,7]</code> 的子序列。</p>
*
*
* <p><strong>示例 1:</strong></p>
*
* <pre>
* <strong>输入:</strong>nums = [10,9,2,5,3,7,101,18]
* <strong>输出:</strong>4
* <strong>解释:</strong>最长递增子序列是 [2,3,7,101],因此长度为 4 。
* </pre>
*
* <p><strong>示例 2:</strong></p>
*
* <pre>
* <strong>输入:</strong>nums = [0,1,0,3,2,3]
* <strong>输出:</strong>4
* </pre>
*
* <p><strong>示例 3:</strong></p>
*
* <pre>
* <strong>输入:</strong>nums = [7,7,7,7,7,7,7]
* <strong>输出:</strong>1
* </pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li><code>1 <= nums.length <= 2500</code></li>
* <li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
* </ul>
*
* <p> </p>
*
* <p><b>进阶:</b></p>
*
* <ul>
* <li>你可以设计时间复杂度为 <code>O(n<sup>2</sup>)</code> 的解决方案吗?</li>
* <li>你能将算法的时间复杂度降低到 <code>O(n log(n))</code> 吗?</li>
* </ul>
* <div><div>Related Topics</div><div><li>数组</li><li>二分查找</li><li>动态规划</li></div></div><br><div><li>👍 2174</li><li>👎 0</li></div>
*/
package leetcode9;
import java.util.Arrays;
public class LongestIncreasingSubsequence {
public static void main(String[] args) {
Solution solution = new LongestIncreasingSubsequence().new Solution();
}
/**
* 时间复杂度:O(n^2)
* for i in (1 ~ n):
* if (dp(i) > dp(j)) : dp(i) = Max( dp(i), dp(j)+1 )
* <p>
* 注:dp(i) 是以 i 为结尾时的最优值,不是全局最优值
*/
class Solution {
public int lengthOfLIS(int[] nums) {
int[] dp = new int[nums.length];
int max = 0;
for (int i = 0; i < nums.length; i++) {
int val = 0;
for (int j = i; j >= 0; j--) {
if (nums[j] < nums[i]) {
val = Math.max(val, dp[j]);
}
}
dp[i] = val + 1;
max = Math.max(max, dp[i]);
}
return max;
}
}
class Solution2 {
public int lengthOfLIS(int[] nums) {
int[] dp = new int[nums.length];
Arrays.fill(dp, 1);
int res = 1;
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
res = Math.max(res, dp[i]);
}
return res;
}
}
}