Skip to content

Commit b1392f3

Browse files
committed
https://leetcode.cn/problems/shortest-bridge/
1 parent 42e2974 commit b1392f3

File tree

5 files changed

+1062
-15
lines changed

5 files changed

+1062
-15
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Step 2. Add the dependency
4545

4646
<summary>展开查看</summary>
4747

48+
https://leetcode.cn/problems/shortest-bridge/
49+
4850
https://leetcode.cn/problems/partition-array-into-disjoint-intervals/
4951

5052
https://leetcode.cn/problems/maximum-profit-in-job-scheduling/

booking-concert-tickets-in-groups/SegmentTree.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class SegmentTree<T> {
1010
) {
1111
this.#root = new TreeNode<T>();
1212
this.#build(this.#root, start, end);
13-
1413
}
1514
#build(node: TreeNode<T>, start: number, end: number) {
1615
if (start === end) {
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
export default function partitionDisjoint(nums: number[]): number {
2-
const n = nums.length;
3-
let leftMax = nums[0],
4-
leftPos = 0,
5-
curMax = nums[0];
6-
for (let i = 1; i < n - 1; i++) {
7-
curMax = Math.max(curMax, nums[i]);
8-
if (nums[i] < leftMax) {
9-
leftMax = curMax;
10-
leftPos = i;
11-
}
12-
}
13-
return leftPos + 1;
14-
}
1+
export default function partitionDisjoint(nums: number[]): number {
2+
const n = nums.length;
3+
let leftMax = nums[0],
4+
leftPos = 0,
5+
curMax = nums[0];
6+
for (let i = 1; i < n - 1; i++) {
7+
curMax = Math.max(curMax, nums[i]);
8+
if (nums[i] < leftMax) {
9+
leftMax = curMax;
10+
leftPos = i;
11+
}
12+
}
13+
return leftPos + 1;
14+
}

shortest-bridge/index.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
export default function shortestBridge(grid: number[][]): number {
2+
const directions = [
3+
[0, 1],
4+
[1, 0],
5+
[0, -1],
6+
[-1, 0],
7+
];
8+
9+
let firstIsland: [number, number][] = [];
10+
loop:
11+
for (const [i, a] of grid.entries()) {
12+
for (const [j, v] of a.entries()) {
13+
if (v === 1) {
14+
firstIsland.push([i, j]);
15+
16+
for (const [x, y] of firstIsland) {
17+
grid[x][y] = 2;
18+
for (const [q, w] of directions) {
19+
const [e, r] = [x + q, y + w];
20+
if (grid[e]?.[r] === 1) {
21+
firstIsland.push([e, r]);
22+
grid[e][r] = 2;
23+
}
24+
}
25+
}
26+
break loop;
27+
}
28+
}
29+
}
30+
31+
let step = 0;
32+
while (firstIsland.length) {
33+
const temp: [number, number][] = [];
34+
for (const [x, y] of firstIsland) {
35+
grid[x][y] = 2;
36+
for (const [q, w] of directions) {
37+
const [e, r] = [x + q, y + w];
38+
if (grid[e]?.[r] === 1) {
39+
return step;
40+
}
41+
if (grid[e]?.[r] === 0) {
42+
temp.push([e, r]);
43+
grid[e][r] = 2;
44+
}
45+
}
46+
}
47+
step++;
48+
firstIsland = temp;
49+
}
50+
return -1;
51+
}

0 commit comments

Comments
 (0)