We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 114f46c commit 0898647Copy full SHA for 0898647
longest-increasing-subsequence/GangBean.java
@@ -0,0 +1,22 @@
1
+class Solution {
2
+ /**
3
+ 1. understanding
4
+ - dp[n] : length of longest increasing subsequence in 0...n
5
+ 2. space
6
+ - time: O(N^2)
7
+ - space: O(N)
8
+ */
9
+ public int lengthOfLIS(int[] nums) {
10
+ int[] dp = new int[nums.length];
11
+ Arrays.fill(dp, 1);
12
+ for (int i = 0; i < nums.length; i++) { // O(N)
13
+ for (int j = 0; j <= i; j++) { // O(N)
14
+ if (nums[j] < nums[i]) {
15
+ dp[i] = Math.max(dp[j]+1, dp[i]);
16
+ }
17
18
19
+ return Arrays.stream(dp).max().orElse(1);
20
21
+}
22
+
0 commit comments