問題翻譯:
給一個二維陣列,該陣列每個元素代表一塊地,每個元素內的值代表建築物高度.
Keep City Skyline,代表一種建造規則,任何建築物不得建造比原本的列跟欄(column and row)最高建築的還要高.
重點: 1 < grid.length = grid[0].length <= 50. All heights grid[i][j] are in the range [0, 100]. All buildings in grid[i][j] occupy the entire grid cell: that is, they are a 1 x 1 x grid[i][j] rectangular prism.
第一版本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 class Solution { public int maxIncreaseKeepingSkyline (int [][] grid) { int increated = 0 ,row = grid.length,column=grid[0 ].length; List<Integer> columnMax = new LinkedList<Integer>(); List<Integer> rowMax = new LinkedList<Integer>(); for (int i =0 ;i<row;i++){ for (int j=0 ;j<column;j++){ if (columnMax.size()<=j){ columnMax.add(grid[i][j]); }else if (columnMax.get(j)<grid[i][j]){ columnMax.set(j,grid[i][j]); } if (rowMax.size()<=i){ rowMax.add(grid[i][j]); }else if (rowMax.get(i)<grid[i][j]){ rowMax.set(i,grid[i][j]); } } } for (int i =0 ;i<row;i++){ for (int j=0 ;j<column;j++){ if (columnMax.get(j).compareTo(rowMax.get(i))>0 ){ increated+=(rowMax.get(i)-grid[i][j]); }else { increated+=(columnMax.get(j)-grid[i][j]); } } } return increated; } }
心得:
本題是第一次做medium的難度,花在理解題目的時間,比解題的時間還要多.一直在思考Skyline,再說些什麼.