题解 | #HJ103 Redraiment的走法#

Redraiment的走法

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

import java.util.Arrays;
import java.util.Scanner;

/**
 * HJ103 Redraiment的走法 - 中等
 */
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int[] arr = new int[sc.nextInt()];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = sc.nextInt();
            }
            int maxLength = lengthOfLIS(arr);
            System.out.println(maxLength);
        }
        sc.close();
    }

    private static int lengthOfLIS(int[] nums) {
        int[] dp = new int[nums.length];
        Arrays.fill(dp, 1);
        for (int i = 0; i < dp.length; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
        }
        int res = 0;
        for (int value : dp) {
            res = Math.max(res, value);
        }
        return res;
    }

}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
4
7
分享

创作者周榜

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