892.leetcode題目講解(Python):三維形體的表面積(Surface Area of 3D Shapes)

題目

解題思路

解這道題的思路爲,首先獲取一個座標點長方體的表面積,計算公式如下:

surface = grid[i][j] * 4 + 2

然後減去其 “上、下、左、右” 與相鄰長方體的重疊表面積,減去的面積應該是相鄰兩個長方體中高度(grid[i][j])較小的那個。

參考代碼

代碼一:爲了方便理解,我們先寫個不太精煉的, 如下:

'''
@auther: Jedi.L
@Date: Wed, Mar 27, 2019 12:44
@Email: [email protected]
@Blog: www.tundrazone.com
'''

class Solution:
    def surfaceArea(self, grid):
        self.grid = grid
        tsa = 0  # total surface area of resulting shapes
        x = len(self.grid) - 1  # max i for grid[i][j]
        y = len((self.grid[0])) - 1  # max j for grid[i][j]
        i = 0
        while i <= x:
            j = 0
            while j <= y:
                if self.grid[i][j] != 0:
                    tsa += self.grid[i][j] * 4 + 2  # surface are at point
                    # we count the left surface area of this point as follow:
                    # up close
                    if i-1 >= 0 and self.grid[i-1][j] >= self.grid[i][j]:
                        tsa = tsa - self.grid[i][j]
                    elif i-1 >= 0 and self.grid[i-1][j] < self.grid[i][j]:
                        tsa = tsa - self.grid[i-1][j]
                    # down close
                    if i+1 <= x and self.grid[i+1][j] >= self.grid[i][j]:
                        tsa = tsa - self.grid[i][j]
                    elif i+1 <= x and self.grid[i+1][j] < self.grid[i][j]:
                        tsa = tsa - self.grid[i+1][j]
                    # left close
                    if j-1 >= 0 and self.grid[i][j-1] >= self.grid[i][j]:
                        tsa = tsa - self.grid[i][j]
                    elif j-1 >= 0 and self.grid[i][j-1] < self.grid[i][j]:
                        tsa = tsa - self.grid[i][j-1]
                    # right close
                    if j + 1 <= y and self.grid[i][j+1] >= self.grid[i][j]:
                        tsa = tsa - self.grid[i][j]
                    elif j + 1 <= y and self.grid[i][j+1] < self.grid[i][j]:
                        tsa = tsa - self.grid[i][j+1]
                j = j + 1
            i = i + 1
        return tsa

代碼二:仔細觀察代碼一,我們會發現其中的一些代碼沒有必要,每個長方體只需要考慮自己的下方和右方相鄰的長方體,然後減去2倍的重疊面積就可以了,精簡後的代碼如下:

'''
@auther: Jedi.L
@Date: Wed, Mar 27, 2019 12:44
@Email: [email protected]
@Blog: www.tundrazone.com
'''

class Solution:
    def surfaceArea(self, grid):
        self.grid = grid
        tsa = 0  # total surface area of resulting shapes
        x = len(self.grid) - 1  # max i for grid[i][j]
        y = len((self.grid[0])) - 1  # max j for grid[i][j]
        i = 0
        while i <= x:
            j = 0
            while j <= y:
                if self.grid[i][j] != 0:
                    tsa += self.grid[i][j] * 4 + 2  # surface are at point
                    # we count the left surface area of this point as follow:
                    # down close
                    if i + 1 <= x and self.grid[i + 1][j] >= self.grid[i][j]:
                        tsa = tsa - 2*self.grid[i][j]
                    elif i + 1 <= x and self.grid[i + 1][j] < self.grid[i][j]:
                        tsa = tsa - 2*self.grid[i + 1][j]
                    # right close
                    if j + 1 <= y and self.grid[i][j + 1] >= self.grid[i][j]:
                        tsa = tsa - 2*self.grid[i][j]
                    elif j + 1 <= y and self.grid[i][j + 1] < self.grid[i][j]:
                        tsa = tsa - 2*self.grid[i][j + 1]
                j = j + 1
            i = i + 1
        return tsa

源碼地址:
https://github.com/jediL/LeetCodeByPython

其它題目:[leetcode題目答案講解彙總(Python版 持續更新)]
(https://www.jianshu.com/p/60b5241ca28e)

ps:如果您有好的建議,歡迎交流 :-D,
也歡迎訪問我的個人博客 苔原帶 (www.tundrazone.com)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章