MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / surfaceArea

Function surfaceArea

Hackerrank_problems/3D_Surface_Area/solution2.cpp:17–51  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15
16
17int surfaceArea(vector<vector<int>> A) {
18 int n = A.size(), m = A[0].size(); //size of the board
19 int ans = 0;
20 for(int i = 0;i<n;i++){
21 for(int j = 0;j<m;j++){
22 ans += 2; //surface area of top and bottom face will be added regardless
23
24 //cell above
25 if(i - 1 >= 0){
26 ans += max(0, A[i][j] - A[i-1][j]);
27 }else{
28 ans += A[i][j];
29 }
30 //cell below
31 if(i + 1 < n){
32 ans += max(0, A[i][j] - A[i+1][j]);
33 }else{
34 ans += A[i][j];
35 }
36 //cell left
37 if(j -1 >= 0){
38 ans += max(0, A[i][j] - A[i][j-1]);
39 }else{
40 ans += A[i][j];
41 }
42 //cell right
43 if(j + 1 < m){
44 ans += max(0, A[i][j] - A[i][j+1]);
45 }else{
46 ans += A[i][j];
47 }
48 }
49 }
50 return ans;
51}
52
53int main()
54{

Callers 1

mainFunction · 0.85

Calls 1

maxFunction · 0.85

Tested by

no test coverage detected