Skip to content

feat: add solutions to lc problem: No.1944 #1524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,19 @@

<!-- 这里可写通用的实现逻辑 -->

单调栈。
**方法一:单调栈**

我们观察发现,对于第 $i$ 个人来说,他能看到的人一定是按从左到右高度严格单调递增的。

因此,我们可以倒序遍历数组 $heights$,用一个从栈顶到栈底单调递增的栈 $stk$ 记录已经遍历过的人的高度。

对于第 $i$ 个人,如果栈不为空并且栈顶元素小于 $heights[i]$,累加当前第 $i$ 个人能看到的人数,然后将栈顶元素出栈,直到栈为空或者栈顶元素大于等于 $heights[i]$。如果此时栈不为空,说明栈顶元素大于等于 $heights[i]$,那么第 $i$ 个人能看到的人数还要再加 $1$。

接下来,我们将 $heights[i]$ 入栈,继续遍历下一个人。

遍历结束后,返回答案数组 $ans$。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $heights$ 的长度。

<!-- tabs:start -->

Expand All @@ -65,17 +77,14 @@ class Solution:
def canSeePersonsCount(self, heights: List[int]) -> List[int]:
n = len(heights)
ans = [0] * n
stack = list()

stk = []
for i in range(n - 1, -1, -1):
while stack:
while stk and stk[-1] < heights[i]:
ans[i] += 1
if heights[i] > stack[-1]:
stack.pop()
else:
break
stack.append(heights[i])

stk.pop()
if stk:
ans[i] += 1
stk.append(heights[i])
return ans
```

Expand All @@ -84,7 +93,24 @@ class Solution:
<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

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;
}
}
```

### **C++**
Expand All @@ -96,35 +122,58 @@ public:
int n = heights.size();
vector<int> ans(n);
stack<int> stk;
for (int i = n - 1; i >= 0; --i) {
while (!stk.empty()) {
ans[i]++;
if (heights[i] <= stk.top()) break;
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;
}
};
```

### **Go**

```go
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
}
```

### **TypeScript**

```ts
function canSeePersonsCount(heights: number[]): number[] {
const n = heights.length;
const ans = new Array(n).fill(0);
const stack = [];
for (let i = n - 1; i >= 0; i--) {
while (stack.length !== 0) {
ans[i]++;
if (heights[i] <= heights[stack[stack.length - 1]]) {
break;
}
stack.pop();
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];
}
stack.push(i);
stk.push(heights[i]);
}
return ans;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,38 @@ class Solution:
def canSeePersonsCount(self, heights: List[int]) -> List[int]:
n = len(heights)
ans = [0] * n
stack = list()

stk = []
for i in range(n - 1, -1, -1):
while stack:
while stk and stk[-1] < heights[i]:
ans[i] += 1
if heights[i] > stack[-1]:
stack.pop()
else:
break
stack.append(heights[i])

stk.pop()
if stk:
ans[i] += 1
stk.append(heights[i])
return ans
```

### **Java**

```java

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;
}
}
```

### **C++**
Expand All @@ -86,35 +100,58 @@ public:
int n = heights.size();
vector<int> ans(n);
stack<int> stk;
for (int i = n - 1; i >= 0; --i) {
while (!stk.empty()) {
ans[i]++;
if (heights[i] <= stk.top()) break;
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;
}
};
```

### **Go**

```go
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
}
```

### **TypeScript**

```ts
function canSeePersonsCount(heights: number[]): number[] {
const n = heights.length;
const ans = new Array(n).fill(0);
const stack = [];
for (let i = n - 1; i >= 0; i--) {
while (stack.length !== 0) {
ans[i]++;
if (heights[i] <= heights[stack[stack.length - 1]]) {
break;
}
stack.pop();
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];
}
stack.push(i);
stk.push(heights[i]);
}
return ans;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
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 >= 0; --i) {
while (!stk.empty()) {
ans[i]++;
if (heights[i] <= stk.top()) break;
stk.pop();
}
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;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
class Solution:
def canSeePersonsCount(self, heights: List[int]) -> List[int]:
n = len(heights)
ans = [0] * n
stack = list()

for i in range(n - 1, -1, -1):
while stack:
ans[i] += 1
if heights[i] > stack[-1]:
stack.pop()
else:
break
stack.append(heights[i])

return ans
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
function canSeePersonsCount(heights: number[]): number[] {
const n = heights.length;
const ans = new Array(n).fill(0);
const stack = [];
for (let i = n - 1; i >= 0; i--) {
while (stack.length !== 0) {
ans[i]++;
if (heights[i] <= heights[stack[stack.length - 1]]) {
break;
}
stack.pop();
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();
}
stack.push(i);
if (stk.length) {
++ans[i];
}
stk.push(heights[i]);
}
return ans;
}