2282. Number of People That Can Be Seen in a Grid
You are given an m x n 0-indexed 2D array of positive integers heights where heights[i][j] is the height of the person standing at position (i, j).
A person standing at position (row1, col1) can see a person standing at position (row2, col2) if:
- The person at (row2, col2) is to the right or below the person at (row1, col1). More formally, this means that either row1 == row2 and col1 < col2 or row1 < row2 and col1 == col2.
- Everyone in between them is shorter than both of them.
Return an m x n 2D array of integers answer where answer[i][j] is the number of people that the person at position (i, j) can see.
- monotonic stack + binary search solution
- Time : O(nm(logn + logm))
- Space : O(max(n,m))
c++
1 | class Solution { |
- monotonic stack solution
- Time : O(nm)
- Space : O(max(n,m))
c++
1 | class Solution { |