Skip to content

Commit e43ff27

Browse files
committedMar 29, 2025
Add solution #980
1 parent def29e2 commit e43ff27

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,062 LeetCode solutions in JavaScript
1+
# 1,063 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -788,6 +788,7 @@
788788
977|[Squares of a Sorted Array](./solutions/0977-squares-of-a-sorted-array.js)|Easy|
789789
978|[Longest Turbulent Subarray](./solutions/0978-longest-turbulent-subarray.js)|Medium|
790790
979|[Distribute Coins in Binary Tree](./solutions/0979-distribute-coins-in-binary-tree.js)|Medium|
791+
980|[Unique Paths III](./solutions/0980-unique-paths-iii.js)|Hard|
791792
985|[Sum of Even Numbers After Queries](./solutions/0985-sum-of-even-numbers-after-queries.js)|Easy|
792793
989|[Add to Array-Form of Integer](./solutions/0989-add-to-array-form-of-integer.js)|Easy|
793794
994|[Rotting Oranges](./solutions/0994-rotting-oranges.js)|Medium|

‎solutions/0980-unique-paths-iii.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* 980. Unique Paths III
3+
* https://leetcode.com/problems/unique-paths-iii/
4+
* Difficulty: Hard
5+
*
6+
* You are given an m x n integer array grid where grid[i][j] could be:
7+
* - 1 representing the starting square. There is exactly one starting square.
8+
* - 2 representing the ending square. There is exactly one ending square.
9+
* - 0 representing empty squares we can walk over.
10+
* - -1 representing obstacles that we cannot walk over.
11+
*
12+
* Return the number of 4-directional walks from the starting square to the ending
13+
* square, that walk over every non-obstacle square exactly once.
14+
*/
15+
16+
/**
17+
* @param {number[][]} grid
18+
* @return {number}
19+
*/
20+
var uniquePathsIII = function(grid) {
21+
const rows = grid.length;
22+
const cols = grid[0].length;
23+
let emptySquares = 0;
24+
let startRow;
25+
let startCol;
26+
27+
for (let i = 0; i < rows; i++) {
28+
for (let j = 0; j < cols; j++) {
29+
if (grid[i][j] === 0) emptySquares++;
30+
if (grid[i][j] === 1) [startRow, startCol] = [i, j];
31+
}
32+
}
33+
34+
function explorePaths(row, col, remaining) {
35+
if (row < 0 || row >= rows || col < 0 || col >= cols || grid[row][col] < 0) {
36+
return 0;
37+
}
38+
if (grid[row][col] === 2) {
39+
return remaining === 0 ? 1 : 0;
40+
}
41+
42+
const current = grid[row][col];
43+
grid[row][col] = -1;
44+
const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
45+
let pathCount = 0;
46+
47+
for (const [dr, dc] of directions) {
48+
pathCount += explorePaths(row + dr, col + dc, remaining - 1);
49+
}
50+
51+
grid[row][col] = current;
52+
return pathCount;
53+
}
54+
55+
return explorePaths(startRow, startCol, emptySquares + 1);
56+
};

0 commit comments

Comments
 (0)
Please sign in to comment.