Bomb Enemy -- LeetCode

Given a 2D grid, each cell is either a wall ‘W‘, an enemy ‘E‘ or empty ‘0‘ (the number zero), return the maximum enemies you can kill using one bomb.
The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since the wall is too strong to be destroyed.
Note that you can only put the bomb at an empty cell.

Example:

For the given grid

0 E 0 0
E 0 W E
0 E 0 0

return 3. (Placing a bomb at (1,1) kills 3 enemies)

思路:扫描矩阵,用rowCount表示该点所在行内有多少敌人可见。用colCount[i]表示该点所在列i内有多少敌人可见。然后该点可见的敌人为两者之和。

初始时两值为0.

当我们位于行首或者该行上一格为墙,那么在该行向右扫描直到行尾或者遇见一堵墙,更新rowCount。

当我们位于列首或者该列上一格为墙,那么在该列向下扫描直到列尾或者遇见一堵墙,更新colCount[i]。

时间复杂度O(mn),空间复杂度O(N)。

 1 class Solution {
 2 public:
 3     int maxKilledEnemies(vector<vector<char>>& grid) {
 4         if (grid.size() == 0) return 0;
 5         int rowCount = 0, height = grid.size(), width = grid[0].size(), res = 0;
 6         vector<int> colCount(width, 0);
 7         for (int i = 0; i < height; i++) {
 8             for (int j = 0; j < width; j++) {
 9                 if (!j || grid[i][j-1] == ‘W‘) {
10                     rowCount = 0;
11                     for (int k = j; k < width && grid[i][k] != ‘W‘; k++)
12                         rowCount += grid[i][k] == ‘E‘;
13                 }
14                 if (!i || grid[i-1][j] == ‘W‘) {
15                     colCount[j] = 0;
16                     for (int k = i; k < height && grid[k][j] != ‘W‘; k++)
17                         colCount[j] += grid[k][j] == ‘E‘;
18                 }
19                 if (grid[i][j] == ‘0‘)
20                     res = std::max(res, rowCount + colCount[j]);
21             }
22         }
23         return res;
24     }
25 };
时间: 2024-12-07 18:44:48

Bomb Enemy -- LeetCode的相关文章

Leetcode: Bomb Enemy

Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return the maximum enemies you can kill using one bomb. The bomb kills all the enemies in the same row and column from the planted point until it hits the w

[LeetCode] 361. Bomb Enemy 炸敌人

Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return the maximum enemies you can kill using one bomb.The bomb kills all the enemies in the same row and column from the planted point until it hits the wa

361.&#160;Bomb Enemy

Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return the maximum enemies you can kill using one bomb.?The bomb kills all the enemies in the same row and column from the planted point until it hits the w

Bomb Enemy

1 public class Solution { 2 public int maxKilledEnemies(char[][] grid) { 3 if (grid.length == 0 || grid[0].length == 0) { 4 return 0; 5 } 6 int result = 0; 7 int rowCount = 0; 8 int[] colCount = new int[grid[0].length]; 9 10 for (int i = 0; i < grid.

Leetcode 前 400 重点 250 题

这个重点题目是把Leetcode前400题进行精简,划分精简规则如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & 389,保留一个) 所有题目尽量保证客观公正,只是按大概率删除不常考题目,很多题目面经出现过, 但出现次数属于个位数或者只有一两家出现进行删除.所以如在面试中出现删除题目概不负责,这只是从概率上删除低频,简单题目. 旨在减轻大家的刷

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

[CareerCup] 2. Bomberman 炸弹人

We have a 2D grid. Each cell is either a wall, an enemy or empty. For example (0-empty, X-enemy, Y-wall): 0 X 0 0X 0 Y X0 X 0 0You have one bomb and you want to kill as many as possible enemies with it. The bomb will kill all the enemies in the same