Header Ad

Leetcode Dungeon Game problem solution

In this Leetcode Dungeon Game problem solution, The demons had captured the princess 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 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 (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).

To reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step. Return the knight's minimum initial health so that he can rescue the princess.

Leetcode Dungeon Game problem solution


Problem solution in Python.

class Solution:
    def calculateMinimumHP(self, dungeon: List[List[int]]) -> int:
        # perhaps we can solve this problem reverse
        if len(dungeon)<1 or len(dungeon[0])<1:
            return 0
        h=len(dungeon)
        w=len(dungeon[0])
        dungeon[-1][-1]=max(1,1-dungeon[-1][-1])
        for x in range(w-2,-1,-1):
            dungeon[-1][x]=max(1,dungeon[-1][x+1]-dungeon[-1][x])
        for y in range(h-2,-1,-1):
            dungeon[y][-1]=max(1,dungeon[y+1][-1]-dungeon[y][-1])
        for y in range(h-2,-1,-1):
            for x in range(w-2,-1,-1):
                right=max(1,dungeon[y][x+1]-dungeon[y][x])
                down=max(1,dungeon[y+1][x]-dungeon[y][x])
                dungeon[y][x]=min(right,down)
        return dungeon[0][0]



Problem solution in Java.

public class Solution {
    public int calculateMinimumHP(int[][] dungeon) {
        int[][] result = new int[dungeon.length][dungeon[0].length];

        for (int i = dungeon.length - 1; i >= 0; i--) {
            int[] row = dungeon[i];
            for (int j = row.length - 1; j >= 0; j--) {
                boolean rowEnd = i == dungeon.length - 1;
                boolean columnEnd = j == row.length - 1;
                if (rowEnd && columnEnd) {
                    result[i][j] = dungeon[i][j];
                } else if (!rowEnd && !columnEnd) {
                    result[i][j] = dungeon[i][j] - Math.min(result[i + 1][j], result[i][j + 1]);
                } else {
                    result[i][j] = dungeon[i][j] - result[i + (rowEnd ? 0 : 1)][j + (columnEnd ? 0 : 1)];
                }
                result[i][j] = result[i][j] > 0 ? 0 : -result[i][j];
            }
        }
        return result[0][0] + 1;
    }
}


Problem solution in C++.

struct Solution {
    int calculateMinimumHP(vector<vector<int>>& d) {
        vector<int> dp(d.size() + 1, INT_MAX);
        dp[d.size() - 1] = 1;
        for (int i = d[0].size() - 1; i >= 0; --i)
            for (int j = d.size() - 1; j >= 0; --j)
                dp[j] = max(1, min(dp[j + 1], dp[j]) - d[j][i]);
        return dp[0];
    }
};


Problem solution in C.

#define min(a, b)   (a < b ? a : b)

int calculateMinimumHP(int** dungeon, int dungeonRowSize, int dungeonColSize) {
    int result = 0;

    int** dp = malloc(sizeof(int*) * dungeonRowSize);
    for(int row = 0; row < dungeonRowSize; ++row)
        dp[row] = malloc(sizeof(int) * dungeonColSize);

    int lastRow = dungeonRowSize - 1;
    int lastCol = dungeonColSize - 1;

    if(dungeon[lastRow][lastCol] > 0)
        dp[lastRow][lastCol] = 1;
    else
        dp[lastRow][lastCol] = 1 - dungeon[lastRow][lastCol];

    for(int row = lastRow - 1; row >= 0; --row) {
        dp[row][lastCol] = dp[row + 1][lastCol] - dungeon[row][lastCol];
        if(dp[row][lastCol] <= 0)
            dp[row][lastCol] = 1;
    }
    for(int col = lastCol - 1; col >= 0; --col) {
        dp[lastRow][col] = dp[lastRow][col + 1] - dungeon[lastRow][col];
        if(dp[lastRow][col] <= 0)
            dp[lastRow][col] = 1;
    }

    for(int row = lastRow - 1; row >= 0; --row) {
        for(int col = lastCol - 1; col >= 0; --col) {
            dp[row][col] = min(dp[row + 1][col], dp[row][col + 1]) - dungeon[row][col];
            if(dp[row][col] <= 0)
                dp[row][col] = 1;
        }
    }
    result = dp[0][0];

    for(int row = 0; row < dungeonRowSize; ++row)
        free(dp[row]);
    free(dp);
    return result;
}


Post a Comment

0 Comments