题解 | #Redraiment的走法#
Redraiment的走法
https://www.nowcoder.com/practice/24e6243b9f0446b081b1d6d32f2aa3aa
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import java.util.stream.Collectors;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String nextLine = in.nextLine();
if (Objects.isNull(nextLine) || nextLine.equals("")) {
break;
}
String s = in.nextLine();
List<Integer> collect = Arrays.stream(s.split(" "))
.map(Integer::parseInt)
.collect(Collectors.toList());
int max = 0;
int[] dp = new int[collect.size() + 1];
// 先默认全部为1
// 动态规划遍历
// 第一个维度为起点,第二个维度为终点
for (int i = 0; i < collect.size(); i++) {
for (int j = i; j < collect.size(); j++) {
// 如果没有存值就先赋值1
dp[j] = Math.max(1, dp[j]);
// 如果当前的位置比i的位置大,那么就+1
if (collect.get(j) > collect.get(i)) {
dp[j] = Math.max(dp[j], dp[i] + 1);
max = Math.max(dp[j], max);
}
}
}
System.out.println(max);
}
}
}

查看12道真题和解析