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

Commit 52cd198

Browse files
committed
minimum path sum
1 parent 4064a4b commit 52cd198

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎64_minimum_path_sum.scala‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
object Solution {
2+
3+
def solve(grid: Array[Array[Int]], pathMin: Array[Array[Int]], x_max: Int, y_max: Int): Int = {
4+
if (pathMin(y_max)(x_max) < 0){
5+
-pathMin(y_max)(x_max)
6+
}else if (x_max == 0){
7+
(0 to y_max).map(grid(_)(0)).sum
8+
}else if (y_max == 0){
9+
grid(0).take(x_max + 1).sum
10+
}else{
11+
pathMin(y_max)(x_max) = -(Math.min(solve(grid, pathMin, x_max - 1, y_max), solve(grid, pathMin, x_max, y_max - 1)) + grid(y_max)(x_max))
12+
-pathMin(y_max)(x_max)
13+
}
14+
}
15+
16+
def minPathSum(grid: Array[Array[Int]]): Int = {
17+
val x_max = grid(0).length - 1
18+
val y_max = grid.length - 1
19+
solve(grid, grid.clone, x_max, y_max)
20+
}
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /