leetcode algorithm interview questions: island number question, dungeon game question

The question of the number of islands

You are given a two-dimensional grid consisting of '1' (land) and '0' (water). Please count the number of islands in the grid.

Islands are always surrounded by water, and each island can only be formed by connecting horizontally and/or vertically adjacent land masses.

Additionally, you can assume that all four sides of the grid are surrounded by water.

Example 1:

Input: grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
]
Output: 1

Example 2:

Input: grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]
Output: 3

hint:

  • m == grid.length

  • n == grid[i].length

  • 1 <= m, n <= 300

Reference answer

class Solution {
private:
    void dfs(vector<vector<char>>& grid, int r, int c) {
        int nr = grid.size();
        int nc = grid[0].size();
 
        grid[r][c] = '0';
        if (r - 1 >= 0 && grid[r-1][c] == '1') dfs(grid, r - 1, c);
        if (r + 1 < nr && grid[r+1][c] == '1') dfs(grid, r + 1, c);
        if (c - 1 >= 0 && grid[r][c-1] == '1') dfs(grid, r, c - 1);
        if (c + 1 < nc && grid[r][c+1] == '1') dfs(grid, r, c + 1);
    }
 
public:
    int numIslands(vector<vector<char>>& grid) {
        int nr = grid.size();
        if (!nr) return 0;
        int nc = grid[0].size();
 
        int num_islands = 0;
        for (int r = 0; r < nr; ++r) {
            for (int c = 0; c < nc; ++c) {
                if (grid[r][c] == '1') {
                    ++num_islands;
                    dfs(grid, r, c);
                }
            }
        }
 
        return num_islands;
    }
};

Dungeon game issues

Some demons captured the princess (P) and locked her up in the lower right corner of the dungeon. A dungeon is a two-dimensional grid consisting of M x N rooms. Our heroic knight (K) is initially placed in the upper left room, where he must travel through the dungeon and save the princess by fighting the demon.

The knight's initial health points are a positive integer. If his health points drop to 0 or below at any point, he dies instantly.

Some rooms are guarded by demons, so the knight will lose health points when entering these rooms (if the value in the room is a negative integer, it means the knight will lose health points); other rooms are either empty (the value in the room is 0) , or contain a magic orb that increases the knight's health points (if the value in the room is a positive integer, it means the knight will increase his health points).

In order to reach the princess as quickly as possible, the knight decides to move only one step to the right or down at a time.

Write a function to calculate the minimum initial health points required to ensure that the knight can save the princess.

For example, considering a dungeon with the following layout, if the knight follows the optimal path Right -> Right -> Down -> Down, the knight's initial health points will be at least 7.

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

illustrate:

  • There is no upper limit to the number of health points a knight can have.

  • Any room may pose a threat to or increase the knight's health points, including the upper left room where the knight enters and the lower right room where the princess is imprisoned.

Reference answer

class Solution {
public:
    int calculateMinimumHP(vector<vector<int>>& dungeon) {
        int n = dungeon.size(), m = dungeon[0].size();
        vector<vector<int>> dp(n + 1, vector<int>(m + 1, INT_MAX));
        dp[n][m - 1] = dp[n - 1][m] = 1;
        for (int i = n - 1; i >= 0; --i) {
            for (int j = m - 1; j >= 0; --j) {
                int minn = min(dp[i + 1][j], dp[i][j + 1]);
                dp[i][j] = max(minn - dungeon[i][j], 1);
            }
        }
        return dp[0][0];
    }
};

The question of the number of islands

You are given a two-dimensional grid consisting of '1' (land) and '0' (water). Please count the number of islands in the grid.

Islands are always surrounded by water, and each island can only be formed by connecting horizontally and/or vertically adjacent land masses.

Additionally, you can assume that all four sides of the grid are surrounded by water.

Example 1:

Input: grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
]
Output: 1

Example 2:

Input: grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]
Output: 3

hint:

  • m == grid.length

  • n == grid[i].length

  • 1 <= m, n <= 300

Reference answer

おすすめ

転載: blog.csdn.net/baidu_33164415/article/details/135026930