【leetcode】Dungeon Game (middle)

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.

-2 (K) -3 3
-5 -10 1
10 30 -5 (P)

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.

思路:

题目虽然长,但是很明显是标准的动态规划。从右下角向前反向推算即可。minimumHP[i][j]存储到达[i][j]位置时需要的最少血量。

class Solution {
public:
    int calculateMinimumHP(vector<vector<int> > &dungeon) {
        if(dungeon.empty())
            return 0;
        vector<vector<int>> minimumHP(dungeon.size(), vector<int>(dungeon[0].size()));
        minimumHP.back().back() = (dungeon.back().back() >= 0) ? 1 : 1 - dungeon.back().back();
        for(int i = dungeon.size() - 2; i >= 0; i--) //最右边一列
        {
            minimumHP[i].back() = max(1, minimumHP[i + 1].back() - dungeon[i].back());
        }
        for(int i = dungeon[0].size() - 2; i >= 0; i--) //最下边一行
        {
            minimumHP.back()[i] = max(1, minimumHP.back()[i + 1] - dungeon.back()[i]);
        }
        for(int i = dungeon.size() - 2; i >= 0; i--)
        {
            for(int j = dungeon[0].size() - 2; j >= 0; j--)
            {
                minimumHP[i][j] = min(max(1, minimumHP[i][j + 1] - dungeon[i][j]), max(1, minimumHP[i + 1][j] - dungeon[i][j]));
            }
        }
        return minimumHP[0][0];
    }
};
时间: 2024-08-05 06:50:20

【leetcode】Dungeon Game (middle)的相关文章

【leetcode】Reverse Integer(middle)☆

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出的方法 ①用数据类型转换long  或 long long ②在每次循环时先保存下数字变化之前的值,处理后单步恢复看是否相等 (比③好) ③整体恢复,看数字是否相等. 思路:注意30000这样以0结尾的数字,注意越界时返回0. 我检查越界是通过把翻转的数字再翻转回去,看是否相等. int rever

【leetcode】Reorder List (middle)

Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 思路: 先把链表分成两节,后半部分翻转,然后前后交叉连接. 大神的代码比我的简洁,注意分两节时用快

【leetcode】Word Break (middle)

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【leetcode】House Robber (middle)

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will autom

【leetcode】3Sum Closest(middle)

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

【leetcode】Partition List(middle)

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3->2

【leetcode】Rotate Image(middle)

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 思路:我的思路,先沿对角线对称,再左右对称. void rotate(int **matrix, int n) { //先沿对角线对称 for(int i = 0; i < n; i++) { for(int j = 0;

【leetcode】Spiral Matrix(middle)

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 思路:就按照螺旋矩阵的规律 用n记录旋转圈数 每

【leetcode】Multiply Strings(middle)

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 思路:直观思路,就是模拟乘法过程.注意进位.我写的比较繁琐. string multiply(string num1, string num2) { vector<int> v1, v