-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution.cpp
44 lines (40 loc) · 978 Bytes
/
Solution.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
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tuple>
#include <deque>
#include <unordered_map>
#include <cmath>
#include <queue>
using namespace std;
class Solution {
public:
// O(n) extra space can optimize?
// can be optimized to O(1) space but iterate through the grid more than once
int projectionArea(vector<vector<int>>& grid) {
int n = grid.size();
if (n < 1) return 0;
vector<int> x(n, 0), y(n, 0);
int sz = 0;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
int h = grid[i][j];
if (h != 0) sz ++ ;
if (h > x[i]) x[i] = h;
if (h > y[j]) y[j] = h;
}
}
int sx = 0, sy = 0;
for (int i=0; i<n; i++) {
sx += x[i];
sy += y[i];
}
return sz + sx + sy;
}
};
int main() {
Solution a;
return 0;
}