题解 | #Redraiment的走法#

Redraiment的走法

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

import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int i1 = Integer.parseInt(scanner.nextLine());
        int[] all = new int[i1];
        String[] s = scanner.nextLine().split(" ");
        for (int i = 0; i < s.length; i++) {
            all[i] = Integer.parseInt(s[i]);
        }
        HashSet<Integer> result = new HashSet<>();
        for (int i = 0; i < all.length; i++) {
            result.add(dp(i, all));
        }
        System.out.println(result.size() > 0 ? Collections.max(result).intValue() : 1);
    }

    public static int dp(int index, int[] all) {
        HashSet<Integer> result = new HashSet<>();
        for (int i = index; i < all.length; i++) {
            if (all[index] < all[i]) {
                result.add(dp(i, all) + 1);
            }
        }
        return result.size() > 0 ? Collections.max(result) : 1;
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务