返回介绍

solution / 1900-1999 / 1944.Number of Visible People in a Queue / README_EN

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

1944. Number of Visible People in a Queue

中文文档

Description

There are n people standing in a queue, and they numbered from 0 to n - 1 in left to right order. You are given an array heights of distinct integers where heights[i] represents the height of the ith person.

A person can see another person to their right in the queue if everybody in between is shorter than both of them. More formally, the ith person can see the jth person if i < j and min(heights[i], heights[j]) > max(heights[i+1], heights[i+2], ..., heights[j-1]).

Return _an array _answer_ of length _n_ where _answer[i]_ is the number of people the _ith_ person can see to their right in the queue_.

 

Example 1:

Input: heights = [10,6,8,5,11,9]
Output: [3,1,2,1,1,0]
Explanation:
Person 0 can see person 1, 2, and 4.
Person 1 can see person 2.
Person 2 can see person 3 and 4.
Person 3 can see person 4.
Person 4 can see person 5.
Person 5 can see no one since nobody is to the right of them.

Example 2:

Input: heights = [5,1,2,3,10]
Output: [4,1,1,1,0]

 

Constraints:

  • n == heights.length
  • 1 <= n <= 105
  • 1 <= heights[i] <= 105
  • All the values of heights are unique.

Solutions

Solution 1

class Solution:
  def canSeePersonsCount(self, heights: List[int]) -> List[int]:
    n = len(heights)
    ans = [0] * n
    stk = []
    for i in range(n - 1, -1, -1):
      while stk and stk[-1] < heights[i]:
        ans[i] += 1
        stk.pop()
      if stk:
        ans[i] += 1
      stk.append(heights[i])
    return ans
class Solution {
  public int[] canSeePersonsCount(int[] heights) {
    int n = heights.length;
    int[] ans = new int[n];
    Deque<Integer> stk = new ArrayDeque<>();
    for (int i = n - 1; i >= 0; --i) {
      while (!stk.isEmpty() && stk.peek() < heights[i]) {
        stk.pop();
        ++ans[i];
      }
      if (!stk.isEmpty()) {
        ++ans[i];
      }
      stk.push(heights[i]);
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> canSeePersonsCount(vector<int>& heights) {
    int n = heights.size();
    vector<int> ans(n);
    stack<int> stk;
    for (int i = n - 1; ~i; --i) {
      while (stk.size() && stk.top() < heights[i]) {
        ++ans[i];
        stk.pop();
      }
      if (stk.size()) {
        ++ans[i];
      }
      stk.push(heights[i]);
    }
    return ans;
  }
};
func canSeePersonsCount(heights []int) []int {
  n := len(heights)
  ans := make([]int, n)
  stk := []int{}
  for i := n - 1; i >= 0; i-- {
    for len(stk) > 0 && stk[len(stk)-1] < heights[i] {
      ans[i]++
      stk = stk[:len(stk)-1]
    }
    if len(stk) > 0 {
      ans[i]++
    }
    stk = append(stk, heights[i])
  }
  return ans
}
function canSeePersonsCount(heights: number[]): number[] {
  const n = heights.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) < heights[i]) {
      ++ans[i];
      stk.pop();
    }
    if (stk.length) {
      ++ans[i];
    }
    stk.push(heights[i]);
  }
  return ans;
}
impl Solution {
  pub fn can_see_persons_count(heights: Vec<i32>) -> Vec<i32> {
    let n = heights.len();
    let mut ans = vec![0; n];
    let mut stack = Vec::new();
    for i in (0..n).rev() {
      while !stack.is_empty() {
        ans[i] += 1;
        if heights[i] <= heights[*stack.last().unwrap()] {
          break;
        }
        stack.pop();
      }
      stack.push(i);
    }
    ans
  }
}
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* canSeePersonsCount(int* heights, int heightsSize, int* returnSize) {
  int* ans = malloc(sizeof(int) * heightsSize);
  memset(ans, 0, sizeof(int) * heightsSize);
  int stack[heightsSize];
  int i = 0;
  for (int j = heightsSize - 1; j >= 0; j--) {
    while (i) {
      ans[j]++;
      if (heights[j] <= heights[stack[i - 1]]) {
        break;
      }
      i--;
    }
    stack[i++] = j;
  }
  *returnSize = heightsSize;
  return ans;
}

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

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

发布评论

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