); } } } returnMin[len-1]; }}2, in turn, starting at the end, indicating the minimum amount of blood required for the current position to the end Public classSolution { Public intCALCULATEMINIMUMHP (int[] Dungeon) { if(Dungeon.length = = 1 dungeon[0].length = 1){ if(Dungeon[0][0] > 0){ return1; } Else {
is the Princess room starting life, and then slowly spread to the first room , the optimal starting health of each position is constantly obtained.The recursive equation is: The recursive equation is dp[i][j] = max (1, Min (Dp[i+1][j], dp[i][j+1])-dungeon[i][j]). The code is as follows:classSolution { Public: intCALCULATEMINIMUMHP (vectorint> > Dungeon) { intm =dungeon.size (); intn =
The original title link is here: https://leetcode.com/problems/dungeon-game/This is a DP problem, save the current grid to the bottom right of the minimum required strength, M*n DP array saved.The update is math.min (take the right hand minimum strength, the lower left side of the minimum strength). Take the right hand minimum strength = Math.max (dp[i][j+1]-dungeon
https://leetcode.com/problems/dungeon-game/The Demons had captured the Princess (P) and imprisoned her in the Bottom-right corner of a dungeon. The dungeon consists of M x N Rooms laid out in a 2D grid. Our Valiant Knight (K) is initially positioned in the top-left, and must fight his, through the
/** 174. Dungeon Game * 12.30 by Mingyang * Dp[i][j] means the minimum HP required to ensure Knight does not die after entering this lattice. * Dp[i][j] is the minimum health value before he enters (I,J). * If the value of a lattice is negative, then the minimum HP required before entering this lattice is-dungeon[i][j] + 1. If the value of the lattice
/bottom of the required magic, then there is no need for additional magic, otherwise, the warrior will arrive at the place need to have some magic to meet the needs of the right/bottom and the magic."Java Code"public class Solution {public int calculateminimumhp (int[][] dungeon) {int m = dungeon.length; int n = dungeon[0].length; int[][] ans = new int[m][n]; Initialize last ro
Leetcode: Dungeon Game, leetcodedungeon
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the
10
30
-5 (P)
This problem is two-dimensional dynamic programming, the key is the tectonic transfer equation. The first requirement for each step of the move process Knight HP is greater than 0, followed by the minimum requirements.
So we can construct a matrix DP of MXN to record the moving process. Its transfer equation is Dp[row][col]=max (1,min (dp[row+1][col],dp[row][col+1])-dungeon[row][col]) space complex
30
-5 (P)
Notes:
The Knight ' s Health has no upper bound.
Any hostel can contain threats or power-ups, even the first, the knight enters and the Bottom-right, where the Princ ESS is imprisoned.
Credits:Special thanks to @stellari for adding this problem and creating all test cases.classSolution { Public: intCALCULATEMINIMUMHP (vectorint> > Dungeon) { intm =dungeon.size (); intn =
)
Notes:
The Knight ' s Health has no upper bound.
Any hostel can contain threats or power-ups, even the first, the knight enters and the Bottom-right, where the Princ ESS is imprisoned.
Classic DP problem, take the map, ensure that each school is greater than or equal to 1DP[I][J] = max (1, Min (dp[i][j+1], dp[i+1][j]) + dungeon[i][j])public class Solution {public int calculateminimumhp (int[][]
)
Notes:
The Knight ' s Health has no upper bound.
Any hostel can contain threats or power-ups, even the first, the knight enters and the Bottom-right, where the Princ ESS is imprisoned. Public classSolution { Public intCALCULATEMINIMUMHP (int[] Dungeon) { /*DP thought, and it was from the right down to the top left, and walked backwards. The meaning of dp[i][j] is the minimum health required to i,j points, note that t
] represents the minimum health required from (I,J) to the destination (m-1,n-1)//Initialize dp[m-1][n-1] = Math. Max (0-dungeon[m-1][n-1], 0); for (int i = m-2; I >= 0; i--) {dp[i][n-1] = Math.max (dp[i+1][n-1]-dungeon[i][n-1], 0); } for (int i = n-2; I >= 0; i--) {Dp[m-1][i] = Math.max (dp[m-1][i+1]-dungeon[m-1][i], 0); }//From the bottom u
Leetcode 174: Dungeon Game, leetcodedungeon.Dungeon GameTotal Accepted:332Total Submissions:2130
The demons had captured the princess (P) And imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) Was initially positioned in the top-lef
10
30
-5 (P)
Notes:
The Knight ' s Health has no upper bound.
Any hostel can contain threats or power-ups, even the first, the knight enters and the Bottom-right, where the Princ ESS is imprisoned.
Credits:Special thanks to @stellari for adding this problem and creating all test cases.This problem is not the same as the previous one, the dynamic planning direction is from the lower right corner to the upper left corner. The procedure is
Original title Address:https://oj.leetcode.com/problems/dungeon-game/Topic content:The Demons had captured the Princess (P) and imprisoned her in the Bottom-right corner of a dungeon. The dungeon consists of M x N Rooms laid out in a 2D grid. Our Valiant Knight (K) is initially positioned in the top-left, and must figh
-1; J >=0; j--)9DP[I][J] = max (min (dp[i][j +1], Dp[i +1][J])-dungeon[i][j],1);Ten returndp[0][0]; One } A};Of course, the 2d array can be simplified to a 1d array if we ' ve been clear of what DP is updated (in the above CO DE, dp is updated row by row from the bottom one to the top one and the all row it is updated from right to left) . The 1d version is as follows.1 classSolution {2 Public:3 intCALCULATEMINIMUMHP (vectorint>>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.