[LeetCode] 883. Projection Area of 3D Shapes

原题链接:https://leetcode.com/problems/projection-area-of-3d-shapes/

1. 题目介绍

On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes.
Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).
Now we view the projection of these cubes onto the xy, yz, and zx planes.
A projection is like a shadow, that maps our 3 dimensional figure to a 2 dimensional plane.
Here, we are viewing the “shadow” when looking at the cubes from the top, the front, and the side.
Return the total area of all three projections.

在一个 N * N 的格子中,我们放入长宽高都为1 的立方体,它们和x、y、z坐标轴是轴对齐的。v = grid[i][j],代表着在坐标为(i,j)的位置,有v个立方体垂直累积堆放。
现在我们可以看到这些立方体的俯视图(xy)、右视图(yz)和后视图(zx)。这三种视图是一种投影,把三维变成了2维。

返回三种方向得出的投影图的面积和。
Example 1:

Input: [[2]]
Output: 5

Example 2:

Input: [[1,2],[3,4]]
Output: 17
Explanation: 
Here are the three projections ("shadows") of the shape made with each axis-aligned plane.

范例2的堆积情况,从左到右依次是三维示意图、俯视图、右视图、后视图。总面积为4+6+7 = 17
在这里插入图片描述
Example 3:

Input: [[1,0],[0,2]]
Output: 8

Example 4:

Input: [[1,1,1],[1,0,1],[1,1,1]]
Output: 14

Example 5:

Input: [[2,2,2],[2,1,2],[2,2,2]]
Output: 21

Note:
1 <= grid.length = grid[0].length <= 50
0 <= grid[i][j] <= 50

2. 解题思路

俯视图的面积:
数组grid中不为零的元素个数,也即俯视图的面积大小就是不等于零的grid[i][j]的个数。

右视图的面积:
取数组grid每行元素的最大值进行累加,累加的结果就是右视图的面积。
在这里插入图片描述

后视图的面积:
取数组grid每列元素的最大值进行累加,累加的结果就是后视图的面积。
在这里插入图片描述

实现代码

class Solution {
    public int projectionArea(int[][] grid) {
		int N = grid.length;
		if (N == 0) {
			return 0;
		}
        int xy = 0;
        int yz = 0;
        int zx = 0;
        for (int i = 0; i < N ; i++) {
        	int Max = 0;
        	for (int j = 0; j < N ; j++ ) {
        		xy += (grid[i][j]==0 ? 0 : 1 );
        		Max = Math.max(Max,grid[i][j]);
        	}
        	yz += Max;
        }

        for (int j = 0; j<N ; j++) {
        	int Maxcolumn = 0;
        	for (int i = 0; i<N ; i++ ) {
        		Maxcolumn = Math.max(Maxcolumn,grid[i][j]);
        	}
        	zx += Maxcolumn;
        }

        return xy+yz+zx;
    }
}

可以利用新的数组存储每行和每列最大的数。这样可以在一遍循环后就计算出结果。

实现代码

class Solution {
    public int projectionArea(int[][] grid) {
		int N = grid.length;
		if (N == 0) {
			return 0;
		}
        int xy = 0;
        int yz = 0;
        int zx = 0;
        int[] MaxRow = new int[N];
        int[] MaxColumn = new int[N];

        for (int i = 0; i < N ; i++) {
        	for (int j = 0; j < N ; j++ ) {
        		xy += (grid[i][j]==0 ? 0 : 1 );

        		if (MaxRow[i] < grid[i][j]) {
        			yz = yz - MaxRow[i] + grid[i][j];
        			MaxRow[i] = grid[i][j];
        		}
        		if (MaxColumn[j] < grid[i][j]) {
        			zx = zx - MaxColumn[j] + grid[i][j];
        			MaxColumn[j] = grid[i][j];
        		}
        	}
        }

        return xy+yz+zx;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值