[go: nahoru, domu]

Skip to content

Commit

Permalink
Submit 174_DungeonGame
Browse files Browse the repository at this point in the history
  • Loading branch information
Binlogo committed Jul 12, 2020
1 parent c0df9ef commit 8c670e0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//: [Previous](@previous)

import Foundation

class Solution {
func calculateMinimumHP(_ dungeon: [[Int]]) -> Int {
guard !dungeon.isEmpty else { return -1 }
let n = dungeon.count, m = dungeon[0].count
var dp = [[Int]].init(repeating: [Int](repeating: .max, count: m + 1), count: n + 1)
dp[n][m - 1] = 1
dp[n - 1][m] = 1
for i in (0..<n).reversed() {
for j in (0..<m).reversed() {
let minN = min(dp[i + 1][j], dp[i][j + 1])
dp[i][j] = max(minN - dungeon[i][j], 1)
}
}
return dp[0][0]
}
}

//: [Next](@next)
3 changes: 3 additions & 0 deletions LeetCodePlayground.playground/contents.xcplayground
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,8 @@
<page name='程序员面试金典 - 面试题 17.13. 恢复空格'/>
<page name='121_BestTimetoBuyandSellStock'/>
<page name='122_BestTimetoBuyandSellStockII'/>
<page name='309_BestTimetoBuyandSellStockwithCooldown'/>
<page name='315_CountofSmallerNumbersAfterSelf'/>
<page name='174_DungeonGame '/>
</pages>
</playground>

0 comments on commit 8c670e0

Please sign in to comment.