class MostEnvelope { public static Envelope[] getSortedEnvelopes (int[][] matrix) { Envelope[] res = new Envelope[matrix.length]; for (int i = 0; i < matrix.length; i++) { res[i] = new Envelope(matrix[i][0], matrix[i][1]); } Arrays.parallelSort(res,new EnvelopeComparator()); return res; } //这是主方法 public static int maxEnvelopes(int[][] matrix) { Envelope[] envelopes = getSortedEnvelopes(matrix); int[] ends = new int[matrix.length]; ends[0] = envelopes[0].wid; int right = 0; int l = 0; int r = 0; int m = 0; for (int i = 1;i < envelopes.length;i++) { l = 0; r = right; while (l <= r) { m = (l + r) / 2; if (envelopes[i].wid >ends[m]) { l = m + 1; } else { r = m - 1; } } right = Math.max(right, l); ends[l] = envelopes[i].wid; } return right + 1; } //测试代码 public static void main(String[] args) { // TODO Auto-generated method stub //定义数组 int[][] matrix = { {2,3},{4,5},{3,4},{2,2},{2,4},{3,6}}; System.out.println(maxEnvelopes(matrix)); }