1. public class Solution_wei {
    2. public int buildingBlocks(int[][] blocks) {
    3. int len = blocks.length;
    4. if (len == 0) {
    5. return 0;
    6. }
    7. // 最后的返回值
    8. int result = 0;
    9. // 保证长 > 宽
    10. for (int i = 0; i < len; i++) {
    11. if (blocks[i][0] < blocks[i][1]) {
    12. int[] block = blocks[i];
    13. int temp = block[0];
    14. block[0] = block[1];
    15. block[1] = temp;
    16. }
    17. }
    18. // 按第一维的增序排列
    19. // 按第二维降序
    20. Arrays.sort(blocks, (a1, a2) -> {
    21. return a1[0] != a2[0] ? a1[0] - a2[0] : a2[1] - a1[1];
    22. } );
    23. int[] dp = new int[len];
    24. Arrays.fill(dp, 1);
    25. for (int i = 0; i < len; i++) {
    26. for (int j = 0; j < i; j++) {
    27. if (blocks[i][1] >= blocks[j][1]) {
    28. dp[i] = Math.max(dp[i], dp[j] + 1);
    29. }
    30. }
    31. }
    32. // 找到最大值
    33. for (int i = 0; i < len; i++) {
    34. result = Math.max(result, dp[i]);
    35. }
    36. return result;
    37. }
    38. public static void main(String[] args) {
    39. int[][] blocks = {
    40. {5, 4},
    41. {6, 3},
    42. {6, 7},
    43. {6, 6}
    44. };
    45. int[][] blocks2 = {
    46. {5, 4},
    47. {6, 3},
    48. {6, 7},
    49. {6, 6},
    50. {4, 6}
    51. };
    52. Solution_wei obj = new Solution_wei();
    53. int i = obj.buildingBlocks(blocks);
    54. System.out.println(i);
    55. int j = obj.buildingBlocks(blocks2);
    56. System.out.println(j);
    57. }
    58. }