title: Dungeon Game Solution explanation
body: <p>The dungeon game is described as:</p>
  
  <p>The demons had captured the princess (P) and imprisoned her 
  in the bottom-right corner of a dungeon. T
  he 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.</p>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <p>In order to reach the princess as quickly as possible, 
  the knight decides to move only rightward or downward in each step.</p>
  
  <p>Write a function to determine the knight's minimum initial health 
  so that he is able to rescue the princess.</p>
  
  <p>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.</p>
  
  <p>Notes:</p>
  
  <p>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.</p>
  
  <p>Example:</p>
  
  <pre><code>dungeon = [[-2,  -3,  4],
             [-6, -15,  0],
             [10,  25, -6]]
  </code></pre>
  
  <p>Answer: <code>8</code></p>
  
  <p>The code solution is:</p>
  
  <pre><code>def dungeonGame(dungeon):
      dp = [float("inf") for _ in dungeon[0]] 
      dp[-1] = 1 
  
      for i in reversed(range(len(dungeon))):
          dp[-1] = max(dp[-1] - dungeon[i][-1], 1)
          for j in reversed(range(len(dungeon[i]) - 1)):
              min_HP_on_exit = min(dp[j], dp[j + 1])
              dp[j] = max(min_HP_on_exit - dungeon[i][j], 1)
  
      return dp[0]
  </code></pre>
  
  <p>Can somebody explain how the solution above is working?  Why is the dp only len 3 with the provided example?  Is it because there are only 3 steps required, excluding start and finish rooms?  Why is it getting the minimum on the adjacent dp's and then the maximum?  Also how come it seems that the last column is not being taken into consideration since dungeon[i][j], where j only goes up to 1 (taking the given example matrix).  I know the solution is written well, just trying to understand how its taking all the path into consideration.</p>