Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3280605

Browse files
author
Kohei Asai
authored
1219. Path with Maximum Gold (#125)
1 parent10a05c0 commit3280605

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

‎solutions/path_with_maximum_gold.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 1219. Path with Maximum Gold
2+
// https://leetcode.com/problems/path-with-maximum-gold/
3+
exportdefaultfunctiongetMaximumGold(grid:number[][]):number{
4+
constwidth=grid[0].length;
5+
constheight=grid.length;
6+
letmaxGold=0;
7+
8+
functionexplore(y:number,x:number):number{
9+
if(x<0||x===width||y<0||y===height)return0;
10+
if(grid[y][x]===0)return0;
11+
12+
constgold=grid[y][x];
13+
14+
grid[y][x]=0;
15+
16+
consttotalGold=
17+
gold+
18+
Math.max(
19+
explore(y-1,x),
20+
explore(y+1,x),
21+
explore(y,x-1),
22+
explore(y,x+1)
23+
);
24+
25+
grid[y][x]=gold;
26+
27+
returntotalGold;
28+
}
29+
30+
for(lety=0;y<grid.length;++y){
31+
for(letx=0;x<grid[0].length;++x){
32+
maxGold=Math.max(maxGold,explore(y,x));
33+
}
34+
}
35+
36+
returnmaxGold;
37+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import{test}from"https://deno.land/std/testing/mod.ts";
2+
import{assert}from"https://deno.land/std/testing/asserts.ts";
3+
importgetMaximumGoldfrom"./path_with_maximum_gold.ts";
4+
5+
test("1219. Path with Maximum Gold",()=>{
6+
assert(getMaximumGold([[0,6,0],[5,8,7],[0,9,0]])===24);
7+
assert(
8+
getMaximumGold([[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]])===
9+
28
10+
);
11+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp