leetcode (Projection Area of 3D Shapes)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hsx1612727380/article/details/85646543

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;

    }

猜你喜欢

转载自blog.csdn.net/hsx1612727380/article/details/85646543