-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
25 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
LeetCodePlayground.playground/Pages/174_DungeonGame .xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters