滴滴出行2017春招研發工程師筆試題-套娃

這裏寫圖片描述
這裏寫圖片描述

思路:二維數組排序+動態規劃

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

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] taowa = new int[n][2];
        for (int i = 0; i < n; i++) {
            taowa[i][0] = sc.nextInt();
            taowa[i][1] = sc.nextInt();
        }
        Arrays.sort(taowa, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] == o2[0] ? o1[1] - o2[1] : o1[0] - o2[0];
            }
        });
        int[] dp = new int[n];
        Arrays.fill(dp,1);
        int result = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (taowa[i][0] > taowa[j][0] && taowa[i][1] > taowa[j][1]) {
                    dp[i] = Math.max(dp[j] + 1, dp[i]);
                }
                result = Math.max(result,dp[i]);
            }
        }
        System.out.println(result);
    }
}
發佈了27 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章