-
Notifications
You must be signed in to change notification settings - Fork 29
/
64.minimum-path-sum.kt
51 lines (48 loc) · 1.23 KB
/
64.minimum-path-sum.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
/*
* @lc app=leetcode id=64 lang=kotlin
*
* [64] Minimum Path Sum
*
* https://leetcode.com/problems/minimum-path-sum/description/
*
* algorithms
* Medium (47.98%)
* Likes: 1523
* Dislikes: 42
* Total Accepted: 249.1K
* Total Submissions: 519.2K
* Testcase Example: '[[1,3,1],[1,5,1],[4,2,1]]'
*
* 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:
*
*
* Input:
* [
* [1,3,1],
* [1,5,1],
* [4,2,1]
* ]
* Output: 7
* Explanation: Because the path 1→3→1→1→1 minimizes the sum.
*
*
*/
class Solution {
fun minPathSum(grid: Array<IntArray>): Int {
var dp = Array(1005){IntArray(1005, {0})}
dp[0][0] = grid[0][0]
for (i in 1 until grid.size) dp[i][0] = dp[i-1][0]+grid[i][0]
for (i in 1 until grid[0].size) dp[0][i] = dp[0][i-1]+grid[0][i]
for (i in 1 until grid.size) {
for (j in 1 until grid[0].size) {
dp[i][j] = Math.min(dp[i][j-1], dp[i-1][j])+grid[i][j]
}
}
return dp[grid.size-1][grid[0].size-1]
}
}