GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — LeetCode/Stack/084.Largest-Rectangle-in-Histogram at master · wisdompeak/LeetCode

Expand file tree

/

084.Largest-Rectangle-in-Histogram

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

084.Largest-Rectangle-in-Histogram

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
084.Largest-Rectangle-in-Histogram.py
084.Largest-Rectangle-in-Histogram.py
 
 
084.Largest-Rectangle-in-Histogram_v1.cpp
084.Largest-Rectangle-in-Histogram_v1.cpp
 
 
084.Largest-Rectangle-in-Histogram_v2.cpp
084.Largest-Rectangle-in-Histogram_v2.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

Outline

leetcode-84-Largest-Rectangle-in-Histogram

此题是单调栈的经典应用。对于每个位置height[i],找出它的prevSmaller和nextSmaller,那么中间的区间,就可以构建一个以height[i]为高的矩形。

比较笨的方法就是写3-pass。第一遍是用单调栈求出所有每个元素i的prevSmaller[i]。再逆序遍历一遍,求出每个元素i的nextSmaller[i]。最后一遍计算area[i] = height[i]*(nextSmaller[i]-prevSmaller[i]-1).

高级一点的方法只需要1-pass。维护一个递增的单调栈。当新元素i比栈顶元素小时,说明栈顶元素的nextSmaller就是i,而栈顶元素的prevSmaller就是栈的次顶元素。

其他的技巧:

在height数组末添加元素0,是为了保证最后强制回溯。在height数组首端添加元素0,是为了便于处理s.pop()之后栈为空的特殊情况;这样处理后永远不会栈空。

Leetcode Link

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.