forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber-of-people-that-can-be-seen-in-a-grid.cpp
131 lines (125 loc) · 4 KB
/
number-of-people-that-can-be-seen-in-a-grid.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
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
128
129
130
131
// Time: O(m * n)
// Space: O(m + n)
// mono stack, optimized from solution2
class Solution {
public:
vector<vector<int>> seePeople(vector<vector<int>>& heights) {
vector<vector<int>> result(size(heights), vector<int>(size(heights[0])));
const auto& count = [](int h, vector<int> *stk) {
int cnt = 0;
while (!empty(*stk) && stk->back() < h) {
stk->pop_back();
++cnt;
}
if (!empty(*stk)) {
++cnt;
}
if (empty(*stk) || stk->back() != h) {
stk->emplace_back(h);
}
return cnt;
};
for (int i = 0; i < size(heights); ++i) {
vector<int> stk;
for (int j = size(heights[0]) - 1; j >= 0; --j) {
result[i][j] += count(heights[i][j], &stk);
}
}
for (int j = 0; j < size(heights[0]); ++j) {
vector<int> stk;
for (int i = size(heights) - 1; i >= 0; --i) {
result[i][j] += count(heights[i][j], &stk);
}
}
return result;
}
};
// Time: O(m * n)
// Space: O(m + n)
// mono stack
class Solution2 {
public:
vector<vector<int>> seePeople(vector<vector<int>>& heights) {
vector<vector<int>> result(size(heights), vector<int>(size(heights[0])));
const auto& count = [](const auto& height, int i, vector<int> *stk) {
int cnt = 0;
while (!empty(*stk) && height(stk->back()) < height(i)) {
stk->pop_back();
++cnt;
}
if (!empty(*stk)) {
++cnt;
}
if (!empty(*stk) && height(stk->back()) == height(i)) {
stk->pop_back();
}
stk->emplace_back(i);
return cnt;
};
for (int i = 0; i < size(heights); ++i) {
const auto& height = [&](int x) {
return heights[i][x];
};
vector<int> stk;
for (int j = size(heights[0]) - 1; j >= 0; --j) {
result[i][j] += count(height, j, &stk);
}
}
for (int j = 0; j < size(heights[0]); ++j) {
const auto& height = [&](int x) {
return heights[x][j];
};
vector<int> stk;
for (int i = size(heights) - 1; i >= 0; --i) {
result[i][j] += count(height, i, &stk);
}
}
return result;
}
};
// Time: O(m * n)
// Space: O(m + n)
// mono stack
class Solution3 {
public:
vector<vector<int>> seePeople(vector<vector<int>>& heights) {
vector<vector<int>> result(size(heights), vector<int>(size(heights[0])));
const auto& count = [](const auto& height, int i, vector<int> *stk, const auto& increase) {
while (!empty(*stk) && height(stk->back()) < height(i)) {
increase(stk->back()); stk->pop_back();
}
if (!empty(*stk)) {
increase(stk->back());
}
if (!empty(*stk) && height(stk->back()) == height(i)) {
stk->pop_back();
}
stk->emplace_back(i);
};
for (int i = 0; i < size(heights); ++i) {
const auto& height = [&](int x) {
return heights[i][x];
};
const auto& increase = [&](int x) {
++result[i][x];
};
vector<int> stk;
for (int j = 0; j < size(heights[0]); ++j) {
count(height, j, &stk, increase);
}
}
for (int j = 0; j < size(heights[0]); ++j) {
const auto& height = [&](int x) {
return heights[x][j];
};
const auto& increase = [&](int x) {
++result[x][j];
};
vector<int> stk;
for (int i = 0; i < size(heights); ++i) {
count(height, i, &stk, increase);
}
}
return result;
}
};