题解 | 最长上升子序列(一)

最长上升子序列(一)

https://www.nowcoder.com/practice/5f65ccbb025240bd8458eb6479c2612e

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] nums = new int[n];
        int index = 0;
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            nums[index] = a;
            index++;
        }
        int[] dp = new int[n];
        Arrays.fill(dp,1);
        int res = dp[0];
        for(int i = 1; i < n; i++){
            for(int j = 0; j < i; j++){
                // i指向当前序列的最后位置,来统计这个位置之前所有这个位置小的个数
                if(nums[i] > nums[j]){
                    dp[i] = Math.max(dp[i],dp[j] + 1);
                }
                res = Math.max(res, dp[i]);
            }
        }
        System.out.println(res);
    }
}

全部评论

相关推荐

影04714:把图书管理系统那个项目经验内容适当的减少掉,然后改成据为己有不要说团队项目,因为图书管理系统这类常见的谁来了都能独立写出来,提问能圆过来即可
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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