Article directory
foreword
This question is the top 100 frequent questions of LeetCode
Since there is no suitable answer to this question, it is a question left over from the past. Recently, I have time to improve the questions left over from the past one by one.
Our community will continue to organize Gu Yi ( Netflix growth hacker, author of "iOS interview" and ACE professional fitness coach. )'s Swift algorithm solutions into text versions for everyone to learn and read.
We have updated the LeetCode algorithm to 83 issues so far. We will keep the update time and progress ( released at 9:00 am on Monday, Wednesday, and Friday ). There is not much content in each issue. We hope that everyone can read on the way to work and accumulate for a long time There will be a big improvement.
If you don’t accumulate steps, you can’t reach thousands of miles; if you don’t accumulate small streams, you can’t form rivers and seas. The Swift community is with you. If you have suggestions and comments, please leave a message at the end of the article, and we will try our best to meet your needs.
Difficulty Level: Difficult
1. Description
Given n non-negative integers, used to represent the height of each column in the histogram. Each column is adjacent to each other and has a width of 1.
Find the maximum area of the rectangle that can be outlined in the histogram.
2. Examples
Example 1
输入:heights = [2,1,5,6,2,3]
输出:10
解释:最大的矩形为图中红色区域,面积为 10
Example 2
输入: heights = [2,4]
输出: 4
hint:
1 <= heights.length <=10^5
0 <= heights[i] <= 10^4
3. Answer
Solution 1
class Solution {
func largestRectangleArea(_ heights: [Int]) -> Int {
if heights.count == 0 {
return 0
}
if heights.count == 1 {
return heights[0]
}
var leftIndexes = Array<Int>.init(repeating: 0, count: heights.count)
var rightIndexes = Array<Int>.init(repeating: 0, count: heights.count)
leftIndexes[0] = -1
rightIndexes[heights.count - 1] = heights.count
for i in 1..<heights.count {
var p = i - 1
while p >= 0, heights[p] >= heights[i] {
p = leftIndexes[p]
}
leftIndexes[i] = p
}
for i in (0...heights.count - 2).reversed() {
var p = i + 1
while p < heights.count, heights[p] >= heights[i] {
p = rightIndexes[p]
}
rightIndexes[i] = p
}
var maxArea = 0
for i in 0..<heights.count {
maxArea = max(maxArea, heights[i] * (rightIndexes[i] - leftIndexes[i] - 1))
}
return maxArea
}
}
Solution 2
class Solution {
func largestRectangleArea(_ heights: [Int]) -> Int {
let heights = heights + [0]
var indexStack: [Int] = []
var maxHeight = 0
for (i,height) in heights.enumerated() {
while let previousIndex = indexStack.last, heights[previousIndex] > height {
indexStack.removeLast()
let lastIndex = indexStack.last ?? -1
let width = (i-1) - lastIndex
let height = heights[previousIndex]
maxHeight = max(maxHeight, width * height)
}
indexStack.append(i)
}
return maxHeight
}
}
Click to go to LeetCode practice
about Us
We are jointly maintained by Swift enthusiasts. We will share technical content centered on Swift combat, SwiftUI, and Swift foundation, and also organize and collect excellent learning materials.