题解 | #Redraiment的走法#
Redraiment的走法
https://www.nowcoder.com/practice/24e6243b9f0446b081b1d6d32f2aa3aa
使用动态规划,简单到爆炸
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); } System.out.println(getLong(arr)); } private static int getLong(int arr[]) { int n = arr.length; int[] dp = new int[n]; // 初始化 Arrays.fill(dp, 1); for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (arr[i] > arr[j]) { // 当前是否比之前大 dp[i] = Math.max(dp[i], dp[j] + 1); } } } int res = 0; for (int i = 0; i < n; i++) { res = Math.max(res, dp[i]); } return res; } }