Skip to content

feat: add solutions to lc problem: No.2577 #3824

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
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
feat: add solutions to lc problem: No.2577
  • Loading branch information
rain84 committed Nov 29, 2024
commit c9b729f3de253d4ed1fa52f81bf3f9babe15a43c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,76 @@ func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) }
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
```

#### TypeScript

```ts
function minimumTime(grid: number[][]): number {
if (grid[0][1] > 1 && grid[1][0] > 1) return -1;

const [m, n] = [grid.length, grid[0].length];
const DIRS = [-1, 0, 1, 0, -1];
const q = new MinPriorityQueue({ priority: ([x]) => x });
const dist: number[][] = Array.from({ length: m }, () =>
new Array(n).fill(Number.POSITIVE_INFINITY),
);
dist[0][0] = 0;
q.enqueue([0, 0, 0]);

while (true) {
const [t, i, j] = q.dequeue().element;
if (i === m - 1 && j === n - 1) return t;

for (let k = 0; k < 4; k++) {
const [x, y] = [i + DIRS[k], j + DIRS[k + 1]];
if (x < 0 || x >= m || y < 0 || y >= n) continue;

let nt = t + 1;
if (nt < grid[x][y]) {
nt = grid[x][y] + ((grid[x][y] - nt) % 2);
}
if (nt < dist[x][y]) {
dist[x][y] = nt;
q.enqueue([nt, x, y]);
}
}
}
}
```

#### JavaScript

```js
function minimumTime(grid) {
if (grid[0][1] > 1 && grid[1][0] > 1) return -1;

const [m, n] = [grid.length, grid[0].length];
const DIRS = [-1, 0, 1, 0, -1];
const q = new MinPriorityQueue({ priority: ([x]) => x });
const dist = Array.from({ length: m }, () => new Array(n).fill(Number.POSITIVE_INFINITY));
dist[0][0] = 0;
q.enqueue([0, 0, 0]);

while (true) {
const [t, i, j] = q.dequeue().element;
if (i === m - 1 && j === n - 1) return t;

for (let k = 0; k < 4; k++) {
const [x, y] = [i + DIRS[k], j + DIRS[k + 1]];
if (x < 0 || x >= m || y < 0 || y >= n) continue;

let nt = t + 1;
if (nt < grid[x][y]) {
nt = grid[x][y] + ((grid[x][y] - nt) % 2);
}
if (nt < dist[x][y]) {
dist[x][y] = nt;
q.enqueue([nt, x, y]);
}
}
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ The final time is 7. It can be shown that it is the minimum time possible.
</ul>

<p>&nbsp;</p>
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
}
.spoiler {overflow:hidden;}
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
Expand Down Expand Up @@ -260,6 +260,76 @@ func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) }
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
```

#### TypeScript

```ts
function minimumTime(grid: number[][]): number {
if (grid[0][1] > 1 && grid[1][0] > 1) return -1;

const [m, n] = [grid.length, grid[0].length];
const DIRS = [-1, 0, 1, 0, -1];
const q = new MinPriorityQueue({ priority: ([x]) => x });
const dist: number[][] = Array.from({ length: m }, () =>
new Array(n).fill(Number.POSITIVE_INFINITY),
);
dist[0][0] = 0;
q.enqueue([0, 0, 0]);

while (true) {
const [t, i, j] = q.dequeue().element;
if (i === m - 1 && j === n - 1) return t;

for (let k = 0; k < 4; k++) {
const [x, y] = [i + DIRS[k], j + DIRS[k + 1]];
if (x < 0 || x >= m || y < 0 || y >= n) continue;

let nt = t + 1;
if (nt < grid[x][y]) {
nt = grid[x][y] + ((grid[x][y] - nt) % 2);
}
if (nt < dist[x][y]) {
dist[x][y] = nt;
q.enqueue([nt, x, y]);
}
}
}
}
```

#### JavaScript

```js
function minimumTime(grid) {
if (grid[0][1] > 1 && grid[1][0] > 1) return -1;

const [m, n] = [grid.length, grid[0].length];
const DIRS = [-1, 0, 1, 0, -1];
const q = new MinPriorityQueue({ priority: ([x]) => x });
const dist = Array.from({ length: m }, () => new Array(n).fill(Number.POSITIVE_INFINITY));
dist[0][0] = 0;
q.enqueue([0, 0, 0]);

while (true) {
const [t, i, j] = q.dequeue().element;
if (i === m - 1 && j === n - 1) return t;

for (let k = 0; k < 4; k++) {
const [x, y] = [i + DIRS[k], j + DIRS[k + 1]];
if (x < 0 || x >= m || y < 0 || y >= n) continue;

let nt = t + 1;
if (nt < grid[x][y]) {
nt = grid[x][y] + ((grid[x][y] - nt) % 2);
}
if (nt < dist[x][y]) {
dist[x][y] = nt;
q.enqueue([nt, x, y]);
}
}
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function minimumTime(grid) {
if (grid[0][1] > 1 && grid[1][0] > 1) return -1;

const [m, n] = [grid.length, grid[0].length];
const DIRS = [-1, 0, 1, 0, -1];
const q = new MinPriorityQueue({ priority: ([x]) => x });
const dist = Array.from({ length: m }, () => new Array(n).fill(Number.POSITIVE_INFINITY));
dist[0][0] = 0;
q.enqueue([0, 0, 0]);

while (true) {
const [t, i, j] = q.dequeue().element;
if (i === m - 1 && j === n - 1) return t;

for (let k = 0; k < 4; k++) {
const [x, y] = [i + DIRS[k], j + DIRS[k + 1]];
if (x < 0 || x >= m || y < 0 || y >= n) continue;

let nt = t + 1;
if (nt < grid[x][y]) {
nt = grid[x][y] + ((grid[x][y] - nt) % 2);
}
if (nt < dist[x][y]) {
dist[x][y] = nt;
q.enqueue([nt, x, y]);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function minimumTime(grid: number[][]): number {
if (grid[0][1] > 1 && grid[1][0] > 1) return -1;

const [m, n] = [grid.length, grid[0].length];
const DIRS = [-1, 0, 1, 0, -1];
const q = new MinPriorityQueue({ priority: ([x]) => x });
const dist: number[][] = Array.from({ length: m }, () =>
new Array(n).fill(Number.POSITIVE_INFINITY),
);
dist[0][0] = 0;
q.enqueue([0, 0, 0]);

while (true) {
const [t, i, j] = q.dequeue().element;
if (i === m - 1 && j === n - 1) return t;

for (let k = 0; k < 4; k++) {
const [x, y] = [i + DIRS[k], j + DIRS[k + 1]];
if (x < 0 || x >= m || y < 0 || y >= n) continue;

let nt = t + 1;
if (nt < grid[x][y]) {
nt = grid[x][y] + ((grid[x][y] - nt) % 2);
}
if (nt < dist[x][y]) {
dist[x][y] = nt;
q.enqueue([nt, x, y]);
}
}
}
}