最长下降子序列

题目描述:

代码实现:

 1 import java.util.Scanner;
 2  
 3 public class Main {
 4  
 5     public static void main(String[] args) {
 6         Scanner s = new Scanner(System.in);
 7         String string=s.nextLine();
 8         String[] strs=string.split(" ");
 9         int len=strs.length;
10         Integer[] a = new Integer[len];
11         int[] dp = new int[len];
12         for(int i=0;i<len;i++) {
13             a[i]=Integer.parseInt(strs[i]);
14         }
15         //dp[i]表示从第0颗导弹开始,到第i颗导弹之间,最长下降子序列长度是多少
16         int n=len, j;
17         int max = 0;
18         for (int i = 0; i < n; i++) {
19             dp[i]=1;
20             for (j = 0; j < i; j++) {
21                 if (a[i] <= a[j])
22                     dp[i] = Math.max(dp[i], dp[j] + 1);
23                 max = Math.max(max, dp[i]);
24             }
25         }
26         System.out.println(max);
27         s.close();
28     }
29 }

猜你喜欢

转载自www.cnblogs.com/LJHAHA/p/10205307.html