Skip to content

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

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 5 commits into from
Dec 4, 2021
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 @@ -53,14 +53,31 @@

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

单调栈。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

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

### **Java**
Expand All @@ -71,10 +88,28 @@

```

### **...**

```

### **C++**

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

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,29 @@ Person 5 can see no one since nobody is to the right of them.

## Solutions

Monotonic stack.

<!-- tabs:start -->

### **Python3**

```python

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

### **Java**
Expand All @@ -61,10 +78,28 @@ Person 5 can see no one since nobody is to the right of them.

```

### **...**

```

### **C++**

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

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +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;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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