[InterviewBit] Dungeon Princess

Dungeon Princess

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
int Solution::calculateMinimumHP(vector<vector<int> > &A) {
int n = A.size(), m = A[0].size();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, INT_MAX));
dp[n][m-1] = dp[n-1][m] = 1;
for(int i = n - 1; i >= 0; i--) {
for(int j = m - 1; j >= 0; j--) {
int hp = min(dp[i+1][j], dp[i][j+1]) - A[i][j];
dp[i][j] = hp <= 0 ? 1 : hp;
}
}
return dp[0][0];
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/11/PS/interviewbit/dungeon-princess/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.