首页 > 其他分享 >LeetCode 1944. Number of Visible People in a Queue

LeetCode 1944. Number of Visible People in a Queue

时间:2024-04-09 11:46:06浏览次数:23  
标签:People int Number 1944 heights person see Person stk

原题链接在这里:https://leetcode.com/problems/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.

题解:

From left to right, maintain a mono decreasing stack. Use the index in the stack.

When hitting a number >= stk top, stk top index could see this new number. res[stk.pop()]++.

After popping, if there is still index in the stk. that index could also see the new number.

Then push the latest index into stack.

Time Complexity: O(n). n = heights.length.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int[] canSeePersonsCount(int[] heights) {
 3         int n = heights.length;
 4         int[] res = new int[n];
 5         Stack<Integer> stk = new Stack<>();
 6         for(int i = 0; i < n; i++){
 7             while(!stk.isEmpty() && heights[stk.peek()] <= heights[i]){
 8                 res[stk.pop()]++;
 9             }
10 
11             if(!stk.isEmpty()){
12                 res[stk.peek()]++;
13             }
14 
15             stk.push(i);
16         }
17 
18         return res;
19     }
20 }

类似Next Greater Element I.

标签:People,int,Number,1944,heights,person,see,Person,stk
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18123608

相关文章

  • 17. InnoDB-spaceID.PageNumber/压缩表
    表空间内部组织结构表空间内部由多个段对象(Segment)组成每个段(Segment)由区(Extent)组成每个区(Extent)由页(Page)组成每个页(Page)里面保存数据(或者叫记录Row)段对用户来说是透明的段也是一个逻辑概念目前为止在information_schema中无法找到段的概念重点需要理解......
  • E. Vlad and a Pair of Numbers
    题解首先,我们知道异或运算是无进位相加,那么a^b=x我们不妨先让a=x,b=0;而a,b其余二进制位上要么同为0,要么同为1。接下来,根据题意a+b=2x,我们可知我们同时为a,b加上x/2。此时再判断a^b是否等于x即可。code#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;intma......
  • Apple iWork (Pages、Numbers、Keynote) 14.0 - 文档、电子表格、演示文稿
    AppleiWork(Pages、Numbers、Keynote)14.0-文档、电子表格、演示文稿请访问原文链接:AppleiWork(Pages、Numbers、Keynote)14.0-文档、电子表格、演示文稿,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org苹果今天将其专为iOS和macOS设备设计的iWork应......
  • 5.数字Number
    #数字Number"""python3支持三种不同的数值类型:整型(int):通常被称为整型或整数,正整数或负整数,不带小数点。python3整型大小是没有限制的可以当作Long类型使用,所以python3没有python2的Long类型bool是整型的子类型"""print(isinstance(True,int))"""浮点型(float):浮点型由整......
  • LeetCode 1539. Kth Missing Positive Number
    原题链接在这里:https://leetcode.com/problems/kth-missing-positive-number/description/题目:Givenanarray arr ofpositiveintegerssortedina strictlyincreasingorder,andaninteger k.Return the kth positive integerthatis missing fromthisarra......
  • 挑战程序设计竞赛 2.6章习题 UVA - 10006 Carmichael Numbers
    https://vjudge.csgrandeur.cn/problem/UVA-10006当今计算机科学的一个重要的领域就是密码学。有些人甚至认为密码学是计算机科学中唯一重要的领域,没有密码学生命都没有意义。阿尔瓦罗就是这样的一个人,它正在设计一个为西班牙杂烩菜饭加密的步骤。他在加密算法中应用了一......
  • [ABC211D] Number of Shortest paths 题解
    [ABC211D]NumberofShortestpaths题解思路解析题目其实说得很明白了,就是最短路计数。我们可以用一个\(s_i\)表示从起点到\(i\)的最短路计数,然后进行bfs,由于边权为\(1\),所以可以使用bfs求最短路。如果\(u\tov\)是\(v\)的最短路的其中一段,就把\(s_u\tos_v\)......
  • Hive的row_number和regexp_extract结合带来的乱码问题
    selectuserid,from_unixtime(createtime,'yyyy-MM-dd')asdateid,regexp_extract(browser,'^([^\\(]*).*$',1)asbrowser,operationsystem,device,row_number()over......
  • Enumerating Rational Numbers 题解
    EnumeratingRationalNumbers题解先下结论,这道题是一道欧拉函数板子题观察题面可以发现,生成的分数有如下特性:分数都是最简分数分母与分子互质,且分子$\le$分母当然第一个除外,那个特判即可,不用纳入考虑范围我们知道,对于任意正整数n,欧拉函数,即\(\varphi(n)\)是小......
  • js的Number对象和全局对象
    文章目录1.Number对象1.1.含义1.2.属性1.3.方法2.全局对象2.1.含义2.2.特点2.3.属性2.4.方法3.函数的本质1.Number对象1.1.含义Number对象是原始数值的包装对象。constnum=2.334;constobj=newNumber(num);console.log(obj);//Numberco......