174. 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) 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.
  • 本题又把最大子序列和的写法又复习了一遍,在本题中可以用到最小子序列和。其中还是有许多疑惑的地方。不过本题中的discuss中的解法确实比较精巧,让人惊讶的感觉,还有如此简单的解法,数学真是一个神奇而又奇妙事物,远比那些人文科学有意思的多。总的来说,终于在9月份基本上达到了做够两百题的目标,在做动态规划题目时有时感觉特别难,其中的部分递推公式非常难以推出。下一阶段目标:
  • (一). 争取每天能够做完一道题目,并且能够理解别人的解法。(目前10月份坚持达到300题)。
  • ( 二).要有危机感,加强学习,加强对知识技能的理解和消化,坚持自己的目标,不松懈。加强自己对算法知识的学习。
class Solution {
public:
    int minSubSequenceSum(vector<int> nums){
        int n = nums.size();
        int thisSum = 0;
        int minSum = 0;

        for(int i = 0;i < n;++i){
            thisSum += nums[i];
            minSum = thisSum < minSum?thisSum:minSum;
            if(thisSum > 0){
                thisSum = 0;
                continue;
            }
        }

        return minSum;
    }

     int minSubSequenceSum_1(vector<int> nums){
        int n = nums.size();
        vector<int> dp[n+1];
        int min = 0;

         dp[n] = 0;
        for(int i = n-1;i >= 0;--i){
            int min = dp[i+1] - nums[i] ;
            dp[i] = min <= 0?0:min;
        }

        return dp[0];
    }

    int calculateMinimumHP(vector<vector<int>>& dungeon) {
        int n = dungeon.size();
        int m = dungeon[0].size();

        if(n <= 0){
            return 0;
        }

        vector<int> t(m+1,INT_MAX);
        vector<vector<int>> hp(n+1,t);
        int need = 0;


        /*intitial*/
        hp[n-1][m] = 1;
        hp[n][m-1] = 1;

        for(int i = n-1;i >= 0;--i){
           for(int j = m-1;j >= 0;--j){
               int need = min(hp[i+1][j],hp[i][j+1]) - dungeon[i][j];//min one is need the morest HP
               hp[i][j] = need <= 0?1:need; 
           }
        }



        return hp[0][0];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值