leetcode (Projection Area of 3D Shapes)
阿新 • • 發佈:2019-01-06
Title: Projection Area of 3D Shapes 883
Difficulty:Easy
原題leetcode地址: https://leetcode.com/problems/projection-area-of-3d-shapes/
1. 見程式碼註釋
時間複雜度:O(n^2),巢狀for迴圈,最長長度為n*n。
空間複雜度:O(1),沒有申請額外空間。
/** * 1、計算底部 * 2、計數左邊 * 3、計數正面 * @param grid * @return */ public static int projectionArea2(int[][] grid) { int count = 0; for (int i = 0; i < grid.length; i++) { int max1 = Integer.MIN_VALUE; int max2 = Integer.MIN_VALUE; for (int j = 0; j < grid[0].length; j++) { if (grid[i][j] > 0) { count++; } max1 = Math.max(max1, grid[i][j]); max2 = Math.max(max2, grid[j][i]); } count += max1 + max2; } return count; }