返回介绍

solution / 0300-0399 / 0361.Bomb Enemy / README_EN

发布于 2024-06-17 01:04:01 字数 6714 浏览 0 评论 0 收藏 0

361. Bomb Enemy

中文文档

Description

Given an m x n matrix grid where each cell is either a wall 'W', an enemy 'E' or empty '0', return _the maximum enemies you can kill using one bomb_. You can only place the bomb in an empty cell.

The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since it is too strong to be destroyed.

 

Example 1:

Input: grid = [["0","E","0","0"],["E","0","W","E"],["0","E","0","0"]]
Output: 3

Example 2:

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

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 500
  • grid[i][j] is either 'W', 'E', or '0'.

Solutions

Solution 1

class Solution:
  def maxKilledEnemies(self, grid: List[List[str]]) -> int:
    m, n = len(grid), len(grid[0])
    g = [[0] * n for _ in range(m)]
    for i in range(m):
      t = 0
      for j in range(n):
        if grid[i][j] == 'W':
          t = 0
        elif grid[i][j] == 'E':
          t += 1
        g[i][j] += t
      t = 0
      for j in range(n - 1, -1, -1):
        if grid[i][j] == 'W':
          t = 0
        elif grid[i][j] == 'E':
          t += 1
        g[i][j] += t
    for j in range(n):
      t = 0
      for i in range(m):
        if grid[i][j] == 'W':
          t = 0
        elif grid[i][j] == 'E':
          t += 1
        g[i][j] += t
      t = 0
      for i in range(m - 1, -1, -1):
        if grid[i][j] == 'W':
          t = 0
        elif grid[i][j] == 'E':
          t += 1
        g[i][j] += t
    return max(
      [g[i][j] for i in range(m) for j in range(n) if grid[i][j] == '0'],
      default=0,
    )
class Solution {
  public int maxKilledEnemies(char[][] grid) {
    int m = grid.length;
    int n = grid[0].length;
    int[][] g = new int[m][n];
    for (int i = 0; i < m; ++i) {
      int t = 0;
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 'W') {
          t = 0;
        } else if (grid[i][j] == 'E') {
          ++t;
        }
        g[i][j] += t;
      }
      t = 0;
      for (int j = n - 1; j >= 0; --j) {
        if (grid[i][j] == 'W') {
          t = 0;
        } else if (grid[i][j] == 'E') {
          ++t;
        }
        g[i][j] += t;
      }
    }
    for (int j = 0; j < n; ++j) {
      int t = 0;
      for (int i = 0; i < m; ++i) {
        if (grid[i][j] == 'W') {
          t = 0;
        } else if (grid[i][j] == 'E') {
          ++t;
        }
        g[i][j] += t;
      }
      t = 0;
      for (int i = m - 1; i >= 0; --i) {
        if (grid[i][j] == 'W') {
          t = 0;
        } else if (grid[i][j] == 'E') {
          ++t;
        }
        g[i][j] += t;
      }
    }
    int ans = 0;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == '0') {
          ans = Math.max(ans, g[i][j]);
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int maxKilledEnemies(vector<vector<char>>& grid) {
    int m = grid.size(), n = grid[0].size();
    vector<vector<int>> g(m, vector<int>(n));
    for (int i = 0; i < m; ++i) {
      int t = 0;
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 'W')
          t = 0;
        else if (grid[i][j] == 'E')
          ++t;
        g[i][j] += t;
      }
      t = 0;
      for (int j = n - 1; j >= 0; --j) {
        if (grid[i][j] == 'W')
          t = 0;
        else if (grid[i][j] == 'E')
          ++t;
        g[i][j] += t;
      }
    }
    for (int j = 0; j < n; ++j) {
      int t = 0;
      for (int i = 0; i < m; ++i) {
        if (grid[i][j] == 'W')
          t = 0;
        else if (grid[i][j] == 'E')
          ++t;
        g[i][j] += t;
      }
      t = 0;
      for (int i = m - 1; i >= 0; --i) {
        if (grid[i][j] == 'W')
          t = 0;
        else if (grid[i][j] == 'E')
          ++t;
        g[i][j] += t;
      }
    }
    int ans = 0;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == '0') ans = max(ans, g[i][j]);
      }
    }
    return ans;
  }
};
func maxKilledEnemies(grid [][]byte) int {
  m, n := len(grid), len(grid[0])
  g := make([][]int, m)
  for i := range g {
    g[i] = make([]int, n)
  }
  for i := 0; i < m; i++ {
    t := 0
    for j := 0; j < n; j++ {
      if grid[i][j] == 'W' {
        t = 0
      } else if grid[i][j] == 'E' {
        t++
      }
      g[i][j] += t
    }
    t = 0
    for j := n - 1; j >= 0; j-- {
      if grid[i][j] == 'W' {
        t = 0
      } else if grid[i][j] == 'E' {
        t++
      }
      g[i][j] += t
    }
  }
  for j := 0; j < n; j++ {
    t := 0
    for i := 0; i < m; i++ {
      if grid[i][j] == 'W' {
        t = 0
      } else if grid[i][j] == 'E' {
        t++
      }
      g[i][j] += t
    }
    t = 0
    for i := m - 1; i >= 0; i-- {
      if grid[i][j] == 'W' {
        t = 0
      } else if grid[i][j] == 'E' {
        t++
      }
      g[i][j] += t
    }
  }
  ans := 0
  for i, row := range grid {
    for j, v := range row {
      if v == '0' && ans < g[i][j] {
        ans = g[i][j]
      }
    }
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文