1. Brute Force

class Solution:
    def seePeople(self, heights: List[List[int]]) -> List[List[int]]:
        m, n = len(heights), len(heights[0])

        def countVisible(line):
            size = len(line)
            cnt = [0] * size
            for i in range(size):
                mx = 0
                for j in range(i + 1, size):
                    if mx < line[j]:
                        cnt[i] += 1
                    mx = max(mx, line[j])
                    if mx >= line[i]:
                        break
            return cnt

        res = [countVisible(row) for row in heights]
        for j in range(n):
            col = [heights[i][j] for i in range(m)]
            cnt = countVisible(col)
            for i in range(m):
                res[i][j] += cnt[i]
        return res

Time & Space Complexity

  • Time complexity: O(m∗n∗(m+n))O(m * n * (m + n))
  • Space complexity: O(m+n)O(m + n) extra space.

Where mm is the number of rows and nn is the number of columns in the grid.


class Solution:
    def seePeople(self, heights: List[List[int]]) -> List[List[int]]:
        m, n = len(heights), len(heights[0])

        def countVisible(line):
            size = len(line)
            cnt = [0] * size
            stack = []
            for i in range(size - 1, -1, -1):
                h = line[i]
                l, r = 0, len(stack)
                while l < r:
                    mid = (l + r) // 2
                    if stack[mid] >= h:
                        l = mid + 1
                    else:
                        r = mid
                cnt[i] = len(stack) - l
                if l > 0:
                    cnt[i] += 1
                while stack and stack[-1] <= h:
                    stack.pop()
                stack.append(h)
            return cnt

        res = [countVisible(row) for row in heights]
        for j in range(n):
            col = [heights[i][j] for i in range(m)]
            cnt = countVisible(col)
            for i in range(m):
                res[i][j] += cnt[i]
        return res

Time & Space Complexity

  • Time complexity: O(m∗n∗log⁡(max⁡(m,n)))O(m * n * \log (\max(m, n)))
  • Space complexity: O(m+n)O(m + n) extra space.

Where mm is the number of rows and nn is the number of columns in the grid.


3. Stack - I

class Solution:
    def seePeople(self, heights: List[List[int]]) -> List[List[int]]:
        m, n = len(heights), len(heights[0])

        def countVisible(line):
            size = len(line)
            cnt = [0] * size
            stack = []
            for i in range(size):
                h = line[i]
                while stack and line[stack[-1]] < h:
                    cnt[stack.pop()] += 1
                if stack:
                    cnt[stack[-1]] += 1
                    if line[stack[-1]] == h:
                        stack.pop()
                stack.append(i)
            return cnt

        res = [countVisible(row) for row in heights]
        for j in range(n):
            col = [heights[i][j] for i in range(m)]
            cnt = countVisible(col)
            for i in range(m):
                res[i][j] += cnt[i]
        return res

Time & Space Complexity

  • Time complexity: O(m∗n)O(m * n)
  • Space complexity: O(m+n)O(m + n) extra space.

Where mm is the number of rows and nn is the number of columns in the grid.


4. Stack - II

class Solution:
    def seePeople(self, heights: List[List[int]]) -> List[List[int]]:
        m, n = len(heights), len(heights[0])

        def countVisible(line):
            size = len(line)
            cnt = [0] * size
            stack = []
            for i in range(size - 1, -1, -1):
                h = line[i]
                while stack and stack[-1] < h:
                    stack.pop()
                    cnt[i] += 1
                if stack:
                    cnt[i] += 1
                    if stack[-1] == h:
                        stack.pop()
                stack.append(h)
            return cnt

        res = [countVisible(row) for row in heights]
        for j in range(n):
            col = [heights[i][j] for i in range(m)]
            cnt = countVisible(col)
            for i in range(m):
                res[i][j] += cnt[i]
        return res

Time & Space Complexity

  • Time complexity: O(m∗n)O(m * n)
  • Space complexity: O(m+n)O(m + n) extra space.

Where mm is the number of rows and nn is the number of columns in the grid.


5. Stack (One Pass)

class Solution:
    def seePeople(self, heights: List[List[int]]) -> List[List[int]]:
        m, n = len(heights), len(heights[0])
        res = [[0] * n for _ in range(m)]
        colStacks = [[] for _ in range(n)]

        def countVisible(stack, h):
            cnt = 0
            while stack and stack[-1] < h:
                stack.pop()
                cnt += 1
            if stack:
                cnt += 1
                if stack[-1] == h:
                    stack.pop()
            stack.append(h)
            return cnt

        for i in range(m - 1, -1, -1):
            rowStack = []
            for j in range(n - 1, -1, -1):
                h = heights[i][j]
                res[i][j] = countVisible(rowStack, h) + countVisible(colStacks[j], h)
        return res

Time & Space Complexity

  • Time complexity: O(m∗n)O(m * n)
  • Space complexity: O(m∗n)O(m * n) extra space for the column stacks.

Where mm is the number of rows and nn is the number of columns in the grid.