题解 | #Redraiment的走法#最长递增子序列默写

Redraiment的走法

https://www.nowcoder.com/practice/24e6243b9f0446b081b1d6d32f2aa3aa

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        in.nextLine();
        String ipt = in.nextLine();
        String[] highs = ipt.split(" ");
        int[] arr = new int[highs.length];
        for(int i = 0;i < highs.length;i++){
            arr[i] = Integer.parseInt(highs[i]);
        }
        /**
            求最长递增子序列?
            dp[i] 直到i为止的最长递增子序列
         */
        int[] dp = new int[arr.length];
        dp[0] = 1;
        for(int i = 1; i < dp.length ; i++){
            dp[i] = 1;
            for(int j = 0; j < i ;j++){
                if(arr[i] > arr[j]){
                    dp[i] = Math.max(dp[i],dp[j]+1);
                }
            }
        }
        int max = Integer.MIN_VALUE;
        for(int i = 0;i< dp.length;i++)
            max = Math.max(dp[i],max);
        System.out.println(max);
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务