-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path174.cpp
More file actions
29 lines (27 loc) · 676 Bytes
/
174.cpp
File metadata and controls
29 lines (27 loc) · 676 Bytes
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
//
// 174.cpp
// leetcode
//
// Created by R Z on 2018/7/12.
// Copyright © 2018年 R Z. All rights reserved.
//
#include <stdio.h>
#include <vector>
using namespace std;
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
int m = dungeon.size();
int n = dungeon[0].size();
vector<vector<int>> hp(m+1, vector<int>(n+1, INT_MAX));
hp[m][n-1]=1;
hp[m-1][n]=1;
for(int i=m-1;i>=0; i--){
for(int j=n-1;j>=0;j--){
int need = min(hp[i+1][j],hp[i][j+1])-dungeon[i][j];
hp[i][j]=need<=0?1:need;
}
}
return hp[0][0];
}
};