883. Projection area of 3D Shapes

Source: Internet
Author: User

Problem

NxN lattice, stacked with 1x1x1 cubes, grid[i][j] represents the number of cubes stacked on a coordinate grid, and the three-view area is calculated.

Input: [[1,2],[3,4]]
Output:17
Explanation: See

Ideas

For the top view, as long as a lattice has a value, the area value is added 1.
For a front view (facing the x-axis), for an X, the highest grid value in the y-axis direction, which indicates that the x follows the y-axis to see the area values seen in the past.
For a side view (facing the y-axis), for a Y, the highest grid value in the x-axis direction, indicating that Y is looking at the area values seen in the past along the y-axis.
Add up these area values.

Code
class Solution(object):    def projectionArea(self, grid):        """        :type grid: List[List[int]]        :rtype: int        """        s = 0        n = len(grid)        for i in range(n):            best_row = 0            best_col = 0            for j in range(n):                if(grid[i][j] > 0):                    s += 1                best_row = max(best_row, grid[i][j])                best_col = max(best_col, grid[j][i])            s += best_row + best_col        return s
Similar topics

892. Surface area of 3D Shapes

883. Projection area of 3D Shapes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.