Skip to content

Commit 7fa4af7

Browse files
committed
Add solution #1219
1 parent 9380f3b commit 7fa4af7

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,182 LeetCode solutions in JavaScript
1+
# 1,183 LeetCode solutions in JavaScript
22

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

@@ -936,6 +936,7 @@
936936
1210|[Minimum Moves to Reach Target with Rotations](./solutions/1210-minimum-moves-to-reach-target-with-rotations.js)|Hard|
937937
1217|[Minimum Cost to Move Chips to The Same Position](./solutions/1217-minimum-cost-to-move-chips-to-the-same-position.js)|Easy|
938938
1218|[Longest Arithmetic Subsequence of Given Difference](./solutions/1218-longest-arithmetic-subsequence-of-given-difference.js)|Medium|
939+
1219|[Path with Maximum Gold](./solutions/1219-path-with-maximum-gold.js)|Medium|
939940
1232|[Check If It Is a Straight Line](./solutions/1232-check-if-it-is-a-straight-line.js)|Easy|
940941
1233|[Remove Sub-Folders from the Filesystem](./solutions/1233-remove-sub-folders-from-the-filesystem.js)|Medium|
941942
1249|[Minimum Remove to Make Valid Parentheses](./solutions/1249-minimum-remove-to-make-valid-parentheses.js)|Medium|
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* 1219. Path with Maximum Gold
3+
* https://leetcode.com/problems/path-with-maximum-gold/
4+
* Difficulty: Medium
5+
*
6+
* In a gold mine grid of size m x n, each cell in this mine has an integer representing the
7+
* amount of gold in that cell, 0 if it is empty.
8+
*
9+
* Return the maximum amount of gold you can collect under the conditions:
10+
* - Every time you are located in a cell you will collect all the gold in that cell.
11+
* - From your position, you can walk one step to the left, right, up, or down.
12+
* - You can't visit the same cell more than once.
13+
* - Never visit a cell with 0 gold.
14+
* - You can start and stop collecting gold from any position in the grid that has some gold.
15+
*/
16+
17+
/**
18+
* @param {number[][]} grid
19+
* @return {number}
20+
*/
21+
var getMaximumGold = function(grid) {
22+
const rows = grid.length;
23+
const cols = grid[0].length;
24+
let maxGold = 0;
25+
26+
for (let i = 0; i < rows; i++) {
27+
for (let j = 0; j < cols; j++) {
28+
if (grid[i][j] !== 0) {
29+
exploreGold(i, j, 0);
30+
}
31+
}
32+
}
33+
34+
return maxGold;
35+
36+
function exploreGold(row, col, currentGold) {
37+
if (row < 0 || row >= rows || col < 0 || col >= cols || grid[row][col] === 0) {
38+
maxGold = Math.max(maxGold, currentGold);
39+
return;
40+
}
41+
42+
const goldHere = grid[row][col];
43+
grid[row][col] = 0;
44+
45+
exploreGold(row - 1, col, currentGold + goldHere);
46+
exploreGold(row + 1, col, currentGold + goldHere);
47+
exploreGold(row, col - 1, currentGold + goldHere);
48+
exploreGold(row, col + 1, currentGold + goldHere);
49+
50+
grid[row][col] = goldHere;
51+
}
52+
};

0 commit comments

Comments
 (0)