3D Surface Area

3D Surface Area

Madison is a little girl who is fond of toys. Her friend Mason works in a toy manufacturing factory. Mason has a 2D board A of size with H rows and W columns. The board is divided into cells of size with each cell indicated by its coordinate (i,j). The cell (i,j) has an integer Aij written on it. To create the toy Mason stacks Aij number of cubes of size on the cell (i, j).

Given the description of the board showing the values of Aij and that the price of the toy is equal to the 3d surface area find the price of the toy.

Input Format

The first line contains two space-separated integers H and W the height and the width of the board respectively.

The next H lines contains W space separated integers. The jth integer in ith line denotes Aij.

Constraints

  • 1 <= H, W <= 100

Output Format

Print the required answer, i.e the price of the toy, in one line.

Sample Input 0

1
2
1 1
1

Sample Output 0

1
6

Explanation 0

The surface area of cube is 6.

Sample Input 1

1
2
3
4
3 3
1 3 4
2 2 3
1 2 4

Sample Output 1

1
60

Explanation 1

The object is rotated so the front row matches column 1 of the input, heights 1, 2, and 1.

The front face is 1 + 2 + 1 = 4 units in area.
The top is 3 units.
The sides are 4 units.
None of the rear faces are exposed.
The underside is 3 units.
The front row contributes 4 + 3 + 4 + 3 = 14 units to the surface area.


Solution

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
/*
* Complete the 'surfaceArea' function below.
*
* The function is expected to return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY A as parameter.
*/

function surfaceArea(A) {
// Write your code here
let count = 0;

for (let i = 0, itotal = A.length; i < itotal; i++) {
for (let j = 0, jtotal = A[0].length; j < jtotal; j++) {
count +=
[
(A[i - 1] && A[i - 1][j]) || 0,
(A[i + 1] && A[i + 1][j]) || 0,
A[i][j - 1] || 0,
A[i][j + 1] || 0
].reduce((target, item) => target + Math.max(0, A[i][j] - item), 0) + 2;
}
}

return count;
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×