返回介绍

solution / 2200-2299 / 2282.Number of People That Can Be Seen in a Grid / README_EN

发布于 2024-06-17 01:03:07 字数 7987 浏览 0 评论 0 收藏 0

2282. Number of People That Can Be Seen in a Grid

中文文档

Description

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._

 

Example 1:

Input: heights = [[3,1,4,2,5]]
Output: [[2,1,2,1,0]]
Explanation:
- The person at (0, 0) can see the people at (0, 1) and (0, 2).
  Note that he cannot see the person at (0, 4) because the person at (0, 2) is taller than him.
- The person at (0, 1) can see the person at (0, 2).
- The person at (0, 2) can see the people at (0, 3) and (0, 4).
- The person at (0, 3) can see the person at (0, 4).
- The person at (0, 4) cannot see anybody.

Example 2:

Input: heights = [[5,1],[3,1],[4,1]]
Output: [[3,1],[2,1],[1,0]]
Explanation:
- The person at (0, 0) can see the people at (0, 1), (1, 0) and (2, 0).
- The person at (0, 1) can see the person at (1, 1).
- The person at (1, 0) can see the people at (1, 1) and (2, 0).
- The person at (1, 1) can see the person at (2, 1).
- The person at (2, 0) can see the person at (2, 1).
- The person at (2, 1) cannot see anybody.

 

Constraints:

  • 1 <= heights.length <= 400
  • 1 <= heights[i].length <= 400
  • 1 <= heights[i][j] <= 105

Solutions

Solution 1

class Solution:
  def seePeople(self, heights: List[List[int]]) -> List[List[int]]:
    def f(nums: List[int]) -> List[int]:
      n = len(nums)
      stk = []
      ans = [0] * n
      for i in range(n - 1, -1, -1):
        while stk and stk[-1] < nums[i]:
          ans[i] += 1
          stk.pop()
        if stk:
          ans[i] += 1
        while stk and stk[-1] == nums[i]:
          stk.pop()
        stk.append(nums[i])
      return ans

    ans = [f(row) for row in heights]
    m, n = len(heights), len(heights[0])
    for j in range(n):
      add = f([heights[i][j] for i in range(m)])
      for i in range(m):
        ans[i][j] += add[i]
    return ans
class Solution {
  public int[][] seePeople(int[][] heights) {
    int m = heights.length, n = heights[0].length;
    int[][] ans = new int[m][0];
    for (int i = 0; i < m; ++i) {
      ans[i] = f(heights[i]);
    }
    for (int j = 0; j < n; ++j) {
      int[] nums = new int[m];
      for (int i = 0; i < m; ++i) {
        nums[i] = heights[i][j];
      }
      int[] add = f(nums);
      for (int i = 0; i < m; ++i) {
        ans[i][j] += add[i];
      }
    }
    return ans;
  }

  private int[] f(int[] nums) {
    int n = nums.length;
    int[] ans = new int[n];
    Deque<Integer> stk = new ArrayDeque<>();
    for (int i = n - 1; i >= 0; --i) {
      while (!stk.isEmpty() && stk.peek() < nums[i]) {
        stk.pop();
        ++ans[i];
      }
      if (!stk.isEmpty()) {
        ++ans[i];
      }
      while (!stk.isEmpty() && stk.peek() == nums[i]) {
        stk.pop();
      }
      stk.push(nums[i]);
    }
    return ans;
  }
}
class Solution {
public:
  vector<vector<int>> seePeople(vector<vector<int>>& heights) {
    int m = heights.size(), n = heights[0].size();
    auto f = [](vector<int>& nums) {
      int n = nums.size();
      vector<int> ans(n);
      stack<int> stk;
      for (int i = n - 1; ~i; --i) {
        while (stk.size() && stk.top() < nums[i]) {
          ++ans[i];
          stk.pop();
        }
        if (stk.size()) {
          ++ans[i];
        }
        while (stk.size() && stk.top() == nums[i]) {
          stk.pop();
        }
        stk.push(nums[i]);
      }
      return ans;
    };
    vector<vector<int>> ans;
    for (auto& row : heights) {
      ans.push_back(f(row));
    }
    for (int j = 0; j < n; ++j) {
      vector<int> col;
      for (int i = 0; i < m; ++i) {
        col.push_back(heights[i][j]);
      }
      vector<int> add = f(col);
      for (int i = 0; i < m; ++i) {
        ans[i][j] += add[i];
      }
    }
    return ans;
  }
};
func seePeople(heights [][]int) (ans [][]int) {
  f := func(nums []int) []int {
    n := len(nums)
    ans := make([]int, n)
    stk := []int{}
    for i := n - 1; i >= 0; i-- {
      for len(stk) > 0 && stk[len(stk)-1] < nums[i] {
        ans[i]++
        stk = stk[:len(stk)-1]
      }
      if len(stk) > 0 {
        ans[i]++
      }
      for len(stk) > 0 && stk[len(stk)-1] == nums[i] {
        stk = stk[:len(stk)-1]
      }
      stk = append(stk, nums[i])
    }
    return ans
  }
  for _, row := range heights {
    ans = append(ans, f(row))
  }
  n := len(heights[0])
  for j := 0; j < n; j++ {
    col := make([]int, len(heights))
    for i := range heights {
      col[i] = heights[i][j]
    }
    for i, v := range f(col) {
      ans[i][j] += v
    }
  }
  return
}
function seePeople(heights: number[][]): number[][] {
  const f = (nums: number[]): number[] => {
    const n = nums.length;
    const ans: number[] = new Array(n).fill(0);
    const stk: number[] = [];
    for (let i = n - 1; ~i; --i) {
      while (stk.length && stk.at(-1) < nums[i]) {
        stk.pop();
        ++ans[i];
      }
      if (stk.length) {
        ++ans[i];
      }
      while (stk.length && stk.at(-1) === nums[i]) {
        stk.pop();
      }
      stk.push(nums[i]);
    }
    return ans;
  };
  const ans: number[][] = [];
  for (const row of heights) {
    ans.push(f(row));
  }
  const n = heights[0].length;
  for (let j = 0; j < n; ++j) {
    const col: number[] = [];
    for (const row of heights) {
      col.push(row[j]);
    }
    const add = f(col);
    for (let i = 0; i < ans.length; ++i) {
      ans[i][j] += add[i];
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文