forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
65 lines (48 loc) · 1.48 KB
/
main.cpp
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
/// Source : https://leetcode.com/problems/projection-area-of-3d-shapes/description/
/// Author : liuyubobobo
/// Time : 2018-08-04
#include <iostream>
#include <vector>
using namespace std;
/// Ad-Hoc
/// Time Complexity: O(m*n)
/// Space Complexity: O(1)
class Solution {
public:
int projectionArea(vector<vector<int>>& grid) {
int n = grid.size();
int m = grid[0].size();
int res = n * m;
for(int i = 0; i < n ; i ++)
for(int j = 0 ; j < m ; j ++)
if(grid[i][j] == 0)
res --;
for(const vector<int>& line: grid)
res += *max_element(line.begin(), line.end());
for(int j = 0; j < m ; j ++){
int v = grid[0][j];
for(int i = 1 ; i < n ; i ++)
v = max(v, grid[i][j]);
res += v;
}
return res;
}
};
int main() {
vector<vector<int>> grid1 = {{2}};
cout << Solution().projectionArea(grid1) << endl;
// 5
vector<vector<int>> grid2 = {{1, 2}, {3, 4}};
cout << Solution().projectionArea(grid2) << endl;
// 17
vector<vector<int>> grid3 = {{1, 0}, {0, 2}};
cout << Solution().projectionArea(grid3) << endl;
// 8
vector<vector<int>> grid4 = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
cout << Solution().projectionArea(grid4) << endl;
// 14
vector<vector<int>> grid5 = {{2, 2, 2}, {2, 1, 2}, {2, 2, 2}};
cout << Solution().projectionArea(grid5) << endl;
// 21
return 0;
}