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 dungeon to rescue the princess.
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).
In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
Write a function to determine the knight's minimum initial health so that he is able to rescue the princess.
For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path
RIGHT-> RIGHT -> DOWN -> DOWN
.Notes:
- The knight's health has no upper bound.
- Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
I was tackling this problem and I ended up with code that is functional, but times out with really large input (meaning it's poorly optimized). I'm a beginner in programming and working out a functional algorithm was extremely hard for me. Now that I have to optimize it, I don't even know how to start.
Is there a way to optimize this code that doesn't change the fundamental underlying logic or is the approach I took doomed to be inefficient? If there is a way to optimize this, could you please provide some examples?
class Solution {
public:
int calculateMinimumHP(vector< vector<int> > &dungeon) {
return 1 - actualFunction(dungeon, 0, 0, 0);
}
int actualFunction(vector< vector<int> > &dungeon, int roomCoordX, int roomCoordY, int shield){
if(dungeon.size() == roomCoordX + 1 && dungeon[0].size() == roomCoordY + 1){
if(shield + dungeon[roomCoordX][roomCoordY] < 0)
return (dungeon[roomCoordX][roomCoordY] + shield);
else
return (0);
}
if(dungeon.size() == roomCoordX + 1){
if(shield + dungeon[roomCoordX][roomCoordY] < 0)
return (dungeon[roomCoordX][roomCoordY] + shield) + actualFunction(dungeon, roomCoordX, roomCoordY + 1, 0);
else
return 0 + actualFunction(dungeon, roomCoordX, roomCoordY + 1, dungeon[roomCoordX][roomCoordY] + shield);
}
if(dungeon[0].size() == roomCoordY + 1){
if(shield + dungeon[roomCoordX][roomCoordY] < 0)
return (dungeon[roomCoordX][roomCoordY] + shield) + actualFunction(dungeon, roomCoordX + 1, roomCoordY, 0);
else
return 0 + actualFunction(dungeon, roomCoordX + 1, roomCoordY, dungeon[roomCoordX][roomCoordY] + shield);
}
int temp1;
int temp2;
if(shield + dungeon[roomCoordX][roomCoordY] < 0){
temp1 = actualFunction(dungeon, roomCoordX + 1, roomCoordY, 0);
temp2 = actualFunction(dungeon, roomCoordX, roomCoordY + 1, 0);
return(shield + dungeon[roomCoordX][roomCoordY]) + ((temp1 > temp2) ? temp1 : temp2);
}
else{
temp1 = actualFunction(dungeon, roomCoordX + 1, roomCoordY, dungeon[roomCoordX][roomCoordY] + shield);
temp2 = actualFunction(dungeon, roomCoordX, roomCoordY + 1, dungeon[roomCoordX][roomCoordY] + shield);
return 0 + ((temp1 > temp2) ? temp1 : temp2);
}
}
};