Skip to content

Commit

Permalink
Create 2282.Number-of-People-That-Can-Be-Seen-in-a-Grid.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Jun 20, 2022
1 parent f3ef226 commit 4eb13b0
Showing 1 changed file with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Solution {
public:
vector<vector<int>> seePeople(vector<vector<int>>& heights)
{
int m = heights.size(), n = heights[0].size();
vector<vector<int>>rets(m, vector<int>(n,0));

for (int i=0; i<m; i++)
{
stack<int>stk;
for (int j=0; j<n; j++)
{
int lastRemove = -1;
while (!stk.empty() && heights[i][j] >= heights[i][stk.top()])
{
rets[i][stk.top()]++;
lastRemove = heights[i][stk.top()];
stk.pop();
}
if (!stk.empty() && lastRemove != heights[i][j])
{
rets[i][stk.top()]++;
}
stk.push(j);
}

}

for (int j=0; j<n; j++)
{
stack<int>stk;
for (int i=0; i<m; i++)
{
int lastRemove = -1;
while (!stk.empty() && heights[i][j] >= heights[stk.top()][j])
{
rets[stk.top()][j]++;
lastRemove = heights[stk.top()][j];
stk.pop();
}
if (!stk.empty() && lastRemove != heights[i][j])
{
rets[stk.top()][j]++;
}
stk.push(i);
}
}

return rets;

}
};

0 comments on commit 4eb13b0

Please sign in to comment.