-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMinimumPathSum.kt
executable file
·52 lines (45 loc) · 1.33 KB
/
MinimumPathSum.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Given a m x n grid filled with non-negative numbers,
* find a path from top left to bottom right which minimizes the sum of all numbers along its path.
*
* Note: You can only move either down or right at any point in time.
*
* Example 1:
* [[1,3,1],
* [1,5,1],
* [4,2,1]]
* Given the above grid map, return 7. Because the path 1→3→1→1→1 minimizes the sum.
*
* Accepted.
*/
class MinimumPathSum {
fun minPathSum(grid: Array<IntArray>): Int {
if (grid.isEmpty()) {
return 0
}
if (grid.size == 1) {
if (grid[0].isEmpty()) {
return 0
}
if (grid[0].size == 1) {
return grid[0][0]
}
}
val matrix = Array(grid.size) {
IntArray(grid[0].size)
}
matrix[0][0] = grid[0][0]
(1 until grid.size).forEach {
matrix[it][0] = matrix[it - 1][0] + grid[it][0]
}
(1 until grid[0].size).forEach {
matrix[0][it] = matrix[0][it - 1] + grid[0][it]
}
(1 until grid.size).forEach { i ->
(1 until grid[0].size).forEach {
matrix[i][it] = Math.min(matrix[i - 1][it] + grid[i][it], matrix[i][it - 1] + grid[i][it])
}
}
return matrix[grid.size - 1][grid[0].size - 1]
}
}