Skip to content

Commit 643048c

Browse files
Merge pull request #2383 from Relsola/master
Update 0084.柱状图中最大的矩形 变量名错误修复和可读性优化,0200.岛屿数量 新增TS深度搜索优先解法
2 parents 4223cad + 22ebe65 commit 643048c

File tree

3 files changed

+126
-26
lines changed

3 files changed

+126
-26
lines changed

problems/0084.柱状图中最大的矩形.md

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -515,55 +515,69 @@ func largestRectangleArea(heights []int) int {
515515
var largestRectangleArea = function(heights) {
516516
const len = heights.length;
517517
const minLeftIndex = new Array(len);
518-
const maxRigthIndex = new Array(len);
518+
const maxRightIndex = new Array(len);
519519
// 记录每个柱子 左边第一个小于该柱子的下标
520520
minLeftIndex[0] = -1; // 注意这里初始化,防止下面while死循环
521521
for(let i = 1; i < len; i++) {
522522
let t = i - 1;
523523
// 这里不是用if,而是不断向左寻找的过程
524-
while(t >= 0 && heights[t] >= heights[i]) t = minLeftIndex[t];
524+
while (t >= 0 && heights[t] >= heights[i]) {
525+
t = minLeftIndex[t];
526+
}
525527
minLeftIndex[i] = t;
526528
}
527529
// 记录每个柱子 右边第一个小于该柱子的下标
528-
maxRigthIndex[len - 1] = len; // 注意这里初始化,防止下面while死循环
530+
maxRightIndex[len - 1] = len; // 注意这里初始化,防止下面while死循环
529531
for(let i = len - 2; i >= 0; i--){
530532
let t = i + 1;
531533
// 这里不是用if,而是不断向右寻找的过程
532-
while(t < len && heights[t] >= heights[i]) t = maxRigthIndex[t];
533-
maxRigthIndex[i] = t;
534+
while (t <= n && heights[t] > heights[i]) {
535+
t = maxRightIndex[t];
536+
}
537+
maxRightIndex[i] = t;
534538
}
535539
// 求和
536540
let maxArea = 0;
537541
for(let i = 0; i < len; i++){
538-
let sum = heights[i] * (maxRigthIndex[i] - minLeftIndex[i] - 1);
542+
let sum = heights[i] * (maxRightIndex[i] - minLeftIndex[i] - 1);
539543
maxArea = Math.max(maxArea , sum);
540544
}
541545
return maxArea;
542546
};
543547
544548
//单调栈
545549
var largestRectangleArea = function(heights) {
546-
let maxArea = 0;
547-
const stack = [];
548-
heights = [0,...heights,0]; // 数组头部加入元素0 数组尾部加入元素0
549-
for(let i = 0; i < heights.length; i++){
550-
if(heights[i] > heights[stack[stack.length-1]]){ // 情况三
551-
stack.push(i);
552-
} else if(heights[i] === heights[stack[stack.length-1]]){ // 情况二
553-
stack.pop(); // 这个可以加,可以不加,效果一样,思路不同
554-
stack.push(i);
555-
} else { // 情况一
556-
while(heights[i] < heights[stack[stack.length-1]]){// 当前bar比栈顶bar矮
557-
const stackTopIndex = stack.pop();// 栈顶元素出栈,并保存栈顶bar的索引
558-
let w = i - stack[stack.length -1] - 1;
559-
let h = heights[stackTopIndex]
550+
let maxArea = 0;
551+
const stack = [0];
552+
heights.push(0);
553+
const n = heights.length;
554+
555+
for (let i = 1; i < n; i++) {
556+
let top = stack.at(-1);
557+
// 情况三
558+
if (heights[top] < heights[i]) {
559+
stack.push(i);
560+
}
561+
// 情况二
562+
if (heights[top] === heights[i]) {
563+
stack.pop(); // 这个可以加,可以不加,效果一样,思路不同
564+
stack.push(i);
565+
}
566+
// 情况一
567+
if (heights[top] > heights[i]) {
568+
while (stack.length > 0 && heights[top] > heights[i]) {
569+
// 栈顶元素出栈,并保存栈顶bar的索引
570+
const h = heights[stack.pop()];
571+
const left = stack.at(-1) ?? -1;
572+
const w = i - left - 1;
560573
// 计算面积,并取最大面积
561-
maxArea = Math.max(maxArea, w * h);
562-
}
563-
stack.push(i);// 当前bar比栈顶bar高了,入栈
564-
}
565-
}
566-
return maxArea;
574+
maxArea = Math.max(maxArea, w * h);
575+
top = stack.at(-1);
576+
}
577+
stack.push(i);
578+
}
579+
}
580+
return maxArea;
567581
};
568582
569583
//单调栈 简洁

problems/0200.岛屿数量.广搜版.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,52 @@ var numIslands = function (grid) {
276276
};
277277
```
278278

279+
### TypeScript
280+
281+
```TypeScript
282+
function numIslands2(grid: string[][]): number {
283+
// 四个方向
284+
const dir: number[][] = [[0, 1], [1, 0], [-1, 0], [0, -1]];
285+
const [m, n]: [number, number] = [grid.length, grid[0].length];
286+
287+
function dfs(grid: string[][], visited: boolean[][], x: number, y: number) {
288+
const queue: number[][] = [[x, y]];
289+
while (queue.length !== 0) {
290+
//取出队列头部元素
291+
const top: number[] = queue.shift()!;
292+
for (let i = 0; i < 4; i++) {
293+
const nextX: number = top[0] + dir[i][0];
294+
const nextY: number = top[1] + dir[i][1];
295+
// 越界了,直接跳过
296+
if (nextX < 0 || nextX >= m || nextY < 0 || nextY >= n) {
297+
continue;
298+
}
299+
if (!visited[nextX][nextY] && grid[nextX][nextY] === '1') {
300+
queue.push([nextX, nextY]);
301+
// 只要加入队列立刻标记
302+
visited[nextX][nextY] = true;
303+
}
304+
}
305+
}
306+
}
307+
308+
const visited: boolean[][] = Array.from({ length: m }, _ => new Array(n).fill(false));
309+
310+
let result = 0;
311+
for (let i = 0; i < m; i++) {
312+
for (let k = 0; k < n; k++) {
313+
if (!visited[i][k] && grid[i][k] === '1') {
314+
++result; // 遇到没访问过的陆地,+1
315+
visited[i][k] = true;
316+
dfs(grid, visited, i, k); // 将与其链接的陆地都标记上 true
317+
}
318+
}
319+
}
320+
return result;
321+
}
322+
```
323+
324+
279325
### Rust
280326

281327
```rust

problems/0200.岛屿数量.深搜版.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,46 @@ var numIslands = function (grid) {
346346
};
347347
```
348348

349+
### TypeScript
350+
351+
```TypeScript
352+
function numIslands(grid: string[][]): number {
353+
// 四个方向
354+
const dir: number[][] = [[0, 1], [1, 0], [-1, 0], [0, -1]];
355+
const [m, n]: [number, number] = [grid.length, grid[0].length];
356+
357+
function dfs(grid: string[][], visited: boolean[][], x: number, y: number) {
358+
for (let i = 0; i < 4; i++) {
359+
let nextX: number = x + dir[i][0];
360+
let nextY: number = y + dir[i][1];
361+
// 越界了,直接跳过
362+
if (nextX < 0 || nextX >= m || nextY < 0 || nextY >= n) {
363+
continue;
364+
}
365+
// 没有访问过同时是陆地
366+
if (!visited[nextX][nextY] && grid[nextX][nextY] === '1') {
367+
visited[nextX][nextY] = true;
368+
dfs(grid, visited, nextX, nextY);
369+
}
370+
}
371+
}
372+
373+
const visited: boolean[][] = Array.from({ length: m }, _ => new Array(n).fill(false));
374+
375+
let result: number = 0;
376+
for (let i = 0; i < m; i++) {
377+
for (let k = 0; k < n; k++) {
378+
if (!visited[i][k] && grid[i][k] === '1') {
379+
++result; // 遇到没访问过的陆地,+1
380+
visited[i][k] = true;
381+
dfs(grid, visited, i, k); // 将与其链接的陆地都标记上 true
382+
}
383+
}
384+
}
385+
return result;
386+
}
387+
```
388+
349389
### Go
350390

351391
```go

0 commit comments

Comments
 (0)