From 9637dc35822f49de50929e4f14145a4e54f08937 Mon Sep 17 00:00:00 2001 From: sourcema Date: Mon, 1 Apr 2019 16:01:43 +0800 Subject: [PATCH] Create sourcema.md --- 2019.02.11-leetcode646/sourcema.md | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2019.02.11-leetcode646/sourcema.md diff --git a/2019.02.11-leetcode646/sourcema.md b/2019.02.11-leetcode646/sourcema.md new file mode 100644 index 000000000..e6f5043db --- /dev/null +++ b/2019.02.11-leetcode646/sourcema.md @@ -0,0 +1,34 @@ +# LeetCode 646 + class Solution { + public int findLongestChain(int[][] pairs) { + if (pairs == null || pairs.length == 0 || pairs[0].length == 0) { + return 0; + } + Arrays.sort(pairs, new Comparator() { + @Override + public int compare(int[] o1, int[] o2) { + if (o1[0] != o2[0]) { + return o1[0] - o2[0]; + } else { + return o1[1] - o2[1]; + } + } + }); + int[] dp = new int[pairs.length]; + for (int i = 0; i < pairs.length; i++) { + dp[i]=1; + for (int j = 0; j < i; j++) { + if (pairs[i][0] > pairs[j][1]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + } + int res=-1; + for (int i = 0; i < dp.length; i++) { + if (dp[i] > res) { + res = dp[i]; + } + } + return res; + } +}