File tree 4 files changed +115
-10
lines changed
solution/1900-1999/1944.Number of Visible People in a Queue
4 files changed +115
-10
lines changed Original file line number Diff line number Diff line change 53
53
54
54
<!-- 这里可写通用的实现逻辑 -->
55
55
56
+ 单调栈。
57
+
56
58
<!-- tabs:start -->
57
59
58
60
### ** Python3**
59
61
60
62
<!-- 这里可写当前语言的特殊实现逻辑 -->
61
63
62
64
``` python
63
-
65
+ class Solution :
66
+ def canSeePersonsCount (self , heights : List[int ]) -> List[int ]:
67
+ n = len (heights)
68
+ ans = [0 ] * n
69
+ stack = list ()
70
+
71
+ for i in range (n - 1 , - 1 , - 1 ):
72
+ while stack:
73
+ ans[i] += 1 ;
74
+ if heights[i] > stack[- 1 ]:
75
+ stack.pop()
76
+ else :
77
+ break
78
+ stack.append(heights[i])
79
+
80
+ return ans
64
81
```
65
82
66
83
### ** Java**
71
88
72
89
```
73
90
74
- ### ** ...**
75
-
76
- ```
77
-
91
+ ### ** C++**
92
+
93
+ ``` cpp
94
+ class Solution {
95
+ public:
96
+ vector<int > canSeePersonsCount(vector<int >& heights) {
97
+ int n = heights.size();
98
+ vector<int > ans(n);
99
+ stack<int > stk;
100
+ for (int i = n - 1; i >= 0; --i)
101
+ {
102
+ while (!stk.empty())
103
+ {
104
+ ans[ i] ++;
105
+ if (heights[ i] <= stk.top()) break;
106
+ stk.pop();
107
+ }
108
+ stk.push(heights[ i] );
109
+ }
110
+ return ans;
111
+ }
112
+ };
78
113
```
79
114
80
115
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -47,12 +47,29 @@ Person 5 can see no one since nobody is to the right of them.
47
47
48
48
## Solutions
49
49
50
+ Monotonic stack.
51
+
50
52
<!-- tabs:start -->
51
53
52
54
### ** Python3**
53
55
54
56
``` python
55
-
57
+ class Solution :
58
+ def canSeePersonsCount (self , heights : List[int ]) -> List[int ]:
59
+ n = len (heights)
60
+ ans = [0 ] * n
61
+ stack = list ()
62
+
63
+ for i in range (n - 1 , - 1 , - 1 ):
64
+ while stack:
65
+ ans[i] += 1 ;
66
+ if heights[i] > stack[- 1 ]:
67
+ stack.pop()
68
+ else :
69
+ break
70
+ stack.append(heights[i])
71
+
72
+ return ans
56
73
```
57
74
58
75
### ** Java**
@@ -61,10 +78,28 @@ Person 5 can see no one since nobody is to the right of them.
61
78
62
79
```
63
80
64
- ### ** ...**
65
-
66
- ```
67
-
81
+ ### ** C++**
82
+
83
+ ``` cpp
84
+ class Solution {
85
+ public:
86
+ vector<int > canSeePersonsCount(vector<int >& heights) {
87
+ int n = heights.size();
88
+ vector<int > ans(n);
89
+ stack<int > stk;
90
+ for (int i = n - 1; i >= 0; --i)
91
+ {
92
+ while (!stk.empty())
93
+ {
94
+ ans[ i] ++;
95
+ if (heights[ i] <= stk.top()) break;
96
+ stk.pop();
97
+ }
98
+ stk.push(heights[ i] );
99
+ }
100
+ return ans;
101
+ }
102
+ };
68
103
```
69
104
70
105
<!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<int > canSeePersonsCount (vector<int >& heights) {
4
+ int n = heights.size ();
5
+ vector<int > ans (n);
6
+ stack<int > stk;
7
+ for (int i = n - 1 ; i >= 0 ; --i)
8
+ {
9
+ while (!stk.empty ())
10
+ {
11
+ ans[i]++;
12
+ if (heights[i] <= stk.top ()) break ;
13
+ stk.pop ();
14
+ }
15
+ stk.push (heights[i]);
16
+ }
17
+ return ans;
18
+ }
19
+ };
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def canSeePersonsCount (self , heights : List [int ]) -> List [int ]:
3
+ n = len (heights )
4
+ ans = [0 ] * n
5
+ stack = list ()
6
+
7
+ for i in range (n - 1 , - 1 , - 1 ):
8
+ while stack :
9
+ ans [i ] += 1 ;
10
+ if heights [i ] > stack [- 1 ]:
11
+ stack .pop ()
12
+ else :
13
+ break
14
+ stack .append (heights [i ])
15
+
16
+ return ans
You can’t perform that action at this time.
0 commit comments