-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0892-SurfaceAreaOf3DShapes.cs
35 lines (31 loc) · 1.07 KB
/
0892-SurfaceAreaOf3DShapes.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//-----------------------------------------------------------------------------
// Runtime: 92ms
// Memory Usage: 25.8 MB
// Link: https://leetcode.com/submissions/detail/338608246/
//-----------------------------------------------------------------------------
using System;
namespace LeetCode
{
public class _0892_SurfaceAreaOf3DShapes
{
public int SurfaceArea(int[][] grid)
{
var rows = grid.Length;
var cols = grid[0].Length;
var result = 0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
{
if (grid[i][j] > 0)
{
result += 2 + grid[i][j] * 4;
if (i > 0 && grid[i - 1][j] > 0)
result -= 2 * Math.Min(grid[i - 1][j], grid[i][j]);
if (j > 0 && grid[i][j - 1] > 0)
result -= 2 * Math.Min(grid[i][j - 1], grid[i][j]);
}
}
return result;
}
}
}