896. 最长上升子序列 II
作者:
dsf2
,
2021-03-05 06:40:33
,
所有人可见
,
阅读 427
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] values = new int[n];
for (int i = 0; i < n; i++)
values[i] = sc.nextInt();
int res = 0;
int[] dp = new int[n];
// O(n ^ 2)
// for (int r = 0; r < n; r++) {
// dp[r] = 1;
// int max = 0;
// for (int l = r; l >= 0; l--) {
// if (values[l] < values[r])
// max = Math.max(max, dp[l]);
// }
// dp[r] += max;
// res = Math.max(res, dp[r]);
// }
int[] monoArr = new int[n + 1]; // index - length of increasing sebquence,
// value the minimum end value has length equals to index
Arrays.fill(monoArr, Integer.MAX_VALUE);
for (int i = 0; i < n; i++) {
dp[i] = 1;
int l = 0, r = n;
while (l < r) {
int m = l + (r - l) / 2 + 1;
if (monoArr[m] < values[i])
l = m;
else
r = m - 1;
}
dp[i] = l + 1;
if (l < n)
monoArr[l + 1] = Math.min(monoArr[l + 1], values[i]);
res = Math.max(res, dp[i]);
}
System.out.println(res);
}
}