Skip to content

Commit

Permalink
Merge pull request youngyangyang04#2280 from ShuangmingMa/master
Browse files Browse the repository at this point in the history
修改 0503.下一个更大元素II go版本中的代码,添加 0042.接雨水 Go版本的单调栈压缩版解法,修改 0084.柱状图中最大的矩形 Go单调栈代码的格式
  • Loading branch information
youngyangyang04 authored Sep 27, 2023
2 parents 134692e + c26fddc commit 0e51e5b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
39 changes: 39 additions & 0 deletions problems/0042.接雨水.md
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,45 @@ func min(x, y int) int {
}
```

单调栈压缩版:

```go
func trap(height []int) int {
stack := make([]int, 0)
res := 0

// 无需事先将第一个柱子的坐标入栈,因为它会在该for循环的最后入栈
for i := 0; i < len(height); i ++ {
// 满足栈不为空并且当前柱子高度大于栈顶对应的柱子高度的情况时
for len(stack) > 0 && height[stack[len(stack) - 1]] < height[i] {
// 获得凹槽高度
mid := height[stack[len(stack) - 1]]
// 凹槽坐标出栈
stack = stack[: len(stack) - 1]

// 如果栈不为空则此时栈顶元素为左侧柱子坐标
if len(stack) > 0 {
// 求得雨水高度
h := min(height[i], height[stack[len(stack) - 1]]) - mid
// 求得雨水宽度
w := i - stack[len(stack) - 1] - 1
res += h * w
}
}
// 如果栈为空或者当前柱子高度小于等于栈顶对应的柱子高度时入栈
stack = append(stack, i)
}
return res
}

func min(x, y int) int {
if x < y {
return x
}
return y
}
```

### JavaScript:

```javascript
Expand Down
56 changes: 27 additions & 29 deletions problems/0084.柱状图中最大的矩形.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,36 +478,34 @@ class Solution:

```go
func largestRectangleArea(heights []int) int {
// 声明max并初始化为0
max := 0
// 使用切片实现栈
stack := make([]int, 0)
// 数组头部加入0
heights = append([]int{0}, heights...)
// 数组尾部加入0
heights = append(heights, 0)
// 初始化栈,序号从0开始
stack = append(stack, 0)
for i := 1; i < len(heights); i++ {
// 结束循环条件为:当即将入栈元素>top元素,也就是形成非单调递增的趋势
for heights[stack[len(stack)-1]] > heights[i] {
// mid 是top
mid := stack[len(stack)-1]
// 出栈
stack = stack[0 : len(stack)-1]
// left是top的下一位元素,i是将要入栈的元素
left := stack[len(stack)-1]
// 高度x宽度
tmp := heights[mid] * (i - left - 1)
if tmp > max {
max = tmp
}
}
stack = append(stack, i)
}
return max
max := 0
// 使用切片实现栈
stack := make([]int, 0)
// 数组头部加入0
heights = append([]int{0}, heights...)
// 数组尾部加入0
heights = append(heights, 0)
// 初始化栈,序号从0开始
stack = append(stack, 0)
for i := 1; i < len(heights); i ++ {
// 结束循环条件为:当即将入栈元素>top元素,也就是形成非单调递增的趋势
for heights[stack[len(stack) - 1]] > heights[i] {
// mid 是top
mid := stack[len(stack) - 1]
// 出栈
stack = stack[0 : len(stack) - 1]
// left是top的下一位元素,i是将要入栈的元素
left := stack[len(stack) - 1]
// 高度x宽度
tmp := heights[mid] * (i - left - 1)
if tmp > max {
max = tmp
}
}
stack = append(stack, i)
}
return max
}

```
### JavaScript:
Expand Down
2 changes: 1 addition & 1 deletion problems/0503.下一个更大元素II.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class Solution:
```go
func nextGreaterElements(nums []int) []int {
length := len(nums)
result := make([]int,length,length)
result := make([]int,length)
for i:=0;i<len(result);i++{
result[i] = -1
}
Expand Down

0 comments on commit 0e51e5b

Please sign in to comment.