-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3dSurfaceArea.cpp
More file actions
127 lines (116 loc) · 4.18 KB
/
3dSurfaceArea.cpp
File metadata and controls
127 lines (116 loc) · 4.18 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*https://www.hackerrank.com/challenges/3d-surface-area/problem*/
int surfaceArea(vector<vector<int>> A) {
// // 1 3 4
// // 2 2 3
// // 1 2 4
// int max_height_front = 0, max_height_side = 0, max_top_view = A.size()*A.size();
// int tallest1 = 0, tallest2 = 0, smallest = 101;
// int tall1indexi = -1, tall1indexj = -1;
// int tall2indexi = -1, tall2indexj = -1;
// int smallindexi = -1, smallindexj = -1;
// int inside_side = 0;
// int surf_area = 0;
// for(int i = 0; i < A.size(); i++)
// {
// for(int j = 0; j < A[0].size(); j++)
// {
// if(A[i][j] > max_height_front)
// max_height_front = A[i][j];
// // if(A[i][j] == 0)
// // max_top_view--;
// if(A[i][j] > tallest1)
// {
// tallest2 = tallest1;
// tallest1 = A[i][j];
// cout << "tallest1 " << tallest1 << endl;
// tall1indexi = i;
// tall1indexj = j;
// }
// else if(A[i][j] > tallest2)
// {
// tallest2 = A[i][j];
// cout << "tallest2 " << tallest2 << endl;
// tall2indexi = i;
// tall2indexj = j;
// }
// if(A[i][j] < smallest)
// {
// smallest = A[i][j];
// cout << "smallest " << smallest << endl;
// smallindexi = i;
// smallindexj = j;
// }
// }
// if(smallindexj > tall1indexj && smallindexj < tall2indexj)
// {
// cout << "min(tallest1, tallest2) " << min(tallest1, tallest2) << endl;
// cout << "smallest in if indexes " << smallest << endl;
// inside_side += min(tallest1, tallest2) - smallest;
// cout << "inside_side " << inside_side << endl;
// }
// surf_area += max_height_front;
// cout << "max_height_front " << max_height_front << endl;
// max_height_front = 0;
// smallest = 101;
// tallest1 = 0;
// tallest2 = 0;
// }
// for(int j = 0; j < A[0].size(); j++)
// {
// for(int i = 0; i < A.size(); i++)
// {
// if(A[i][j] > max_height_side)
// max_height_side = A[i][j];
// // if(A[i][j] == 0)
// // max_top_view--;
// if(A[i][j] > tallest1)
// {
// tallest2 = tallest1;
// tallest1 = A[i][j];
// cout << "tallest1 " << tallest1 << endl;
// tall1indexi = i;
// tall1indexj = j;
// }
// else if(A[i][j] > tallest2)
// {
// tallest2 = A[i][j];
// cout << "tallest2 " << tallest2 << endl;
// tall2indexi = i;
// tall2indexj = j;
// }
// if(A[i][j] < smallest)
// {
// smallest = A[i][j];
// cout << "smallest " << smallest << endl;
// smallindexi = i;
// smallindexj = j;
// }
// }
// if(smallindexi > tall1indexi && smallindexi < tall2indexi)
// {
// cout << "min(tallest1, tallest2) " << min(tallest1, tallest2) << endl;
// cout << "smallest in if indexes " << smallest << endl;
// inside_side += min(tallest1, tallest2) - smallest;
// cout << "inside_side " << inside_side << endl;
// }
// surf_area += max_height_side;
// cout << "max_height_side " << max_height_side << endl;
// max_height_side = 0;
// smallest = 101;
// tallest1 = 0;
// tallest2 = 0;
// }
// //return (surf_area + max_top_view + inside_side) * 2;
// return (surf_area + max_top_view) * 2;
int sum=0;
for (int i=0; i<A.size(); i++)
for (int j=0; j<A[0].size(); j++)
{
sum+=A[i][j]*4+2;
if (j+1<A[0].size())
sum-=2*min(A[i][j], A[i][j+1]);
if (i+1<A.size())
sum-=2*min(A[i][j], A[i+1][j]);
}
return sum;
}