Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #20

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
pull merged 4 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jun 21, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
添加(0084.柱状图中最大的矩形.md):增加typescript版本
  • Loading branch information
xiaofei-2020 committed May 22, 2022
commit b06b83867b466fa2d72268c0a6ef50e4da52aac3
90 changes: 90 additions & 0 deletions problems/0084.柱状图中最大的矩形.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -486,5 +486,95 @@ var largestRectangleArea = function(heights) {
return maxArea;
};
```
TypeScript:

> 双指针法(会超时):

```typescript
function largestRectangleArea(heights: number[]): number {
let resMax: number = 0;
for (let i = 0, length = heights.length; i < length; i++) {
// 左开右开
let left: number = i - 1,
right: number = i + 1;
while (left >= 0 && heights[left] >= heights[i]) {
left--;
}
while (right < length && heights[right] >= heights[i]) {
right++;
}
resMax = Math.max(resMax, heights[i] * (right - left - 1));
}
return resMax;
};
```

> 动态规划预处理:

```typescript
function largestRectangleArea(heights: number[]): number {
const length: number = heights.length;
const leftHeightDp: number[] = [],
rightHeightDp: number[] = [];
leftHeightDp[0] = -1;
rightHeightDp[length - 1] = length;
for (let i = 1; i < length; i++) {
let j = i - 1;
while (j >= 0 && heights[i] <= heights[j]) {
j = leftHeightDp[j];
}
leftHeightDp[i] = j;
}
for (let i = length - 2; i >= 0; i--) {
let j = i + 1;
while (j < length && heights[i] <= heights[j]) {
j = rightHeightDp[j];
}
rightHeightDp[i] = j;
}
let resMax: number = 0;
for (let i = 0; i < length; i++) {
let area = heights[i] * (rightHeightDp[i] - leftHeightDp[i] - 1);
resMax = Math.max(resMax, area);
}
return resMax;
};
```

> 单调栈:

```typescript
function largestRectangleArea(heights: number[]): number {
heights.push(0);
const length: number = heights.length;
// 栈底->栈顶:严格单调递增
const stack: number[] = [];
stack.push(0);
let resMax: number = 0;
for (let i = 1; i < length; i++) {
let top = stack[stack.length - 1];
if (heights[top] < heights[i]) {
stack.push(i);
} else if (heights[top] === heights[i]) {
stack.pop();
stack.push(i);
} else {
while (stack.length > 0 && heights[top] > heights[i]) {
let mid = stack.pop();
let left = stack.length > 0 ? stack[stack.length - 1] : -1;
let w = i - left - 1;
let h = heights[mid];
resMax = Math.max(resMax, w * h);
top = stack[stack.length - 1];
}
stack.push(i);
}
}
return resMax;
};
```



-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

AltStyle によって変換されたページ (->オリジナル) /