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 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)
Walk through the matrix. At the start of each non-wall-streak (row-wise or column-wise), count the number of hits in that streak and remember it.
For the other positions, if it's still in the non-wall-streak(row-wise or col-wise), its hit has already been calculated.
Once we meet 'W' in either row-wise or col-wise direction, we should recalculate the number of hits in that direction.
O(mn) time, O(n) space.
public class Solution {
public int maxKilledEnemies(char[][] grid) {
if (grid==null || grid.length==0 || grid[0].length==0) return 0;
int row = 0;
int[] col = new int[grid[0].length];
int max = 0;
for (int i=0; i<grid.length; i++) {
for (int j=0; j<grid[0].length; j++) {
if (grid[i][j] == 'W') continue;
if (j==0 || grid[i][j-1]=='W') {
row = calcRowEnemy(grid, i, j);
}
if (i==0 || grid[i-1][j]=='W') {
col[j] = calcColEnemy(grid, i, j);
}
if (grid[i][j] == '0') {
max = Math.max(max, row+col[j]);
}
}
}
return max;
} public int calcRowEnemy(char[][] grid, int i, int j) {
int res = 0;
while (j<grid[0].length && grid[i][j]!='W') {
res = res + (grid[i][j]=='E'? 1 : 0);
j++;
}
return res;
} public int calcColEnemy(char[][] grid, int i, int j) {
int res = 0;
while (i<grid.length && grid[i][j]!='W') {
res = res + (grid[i][j]=='E'? 1 : 0);
i++;
}
return res;
}
}
Leetcode: Bomb Enemy的更多相关文章
- [LeetCode] Bomb Enemy 炸弹人
Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...
- LeetCode 361. Bomb Enemy
原题链接在这里:https://leetcode.com/problems/bomb-enemy/description/ 题目: Given a 2D grid, each cell is eith ...
- [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 ...
- Bomb Enemy -- LeetCode
Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...
- 【LeetCode】361. Bomb Enemy 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力搜索 日期 题目地址:https://leetco ...
- leetcode 361.Bomb Enemy(lintcode 553. Bomb Enemy)
dp 分别计算从左到右.从右到左.从上到下.从下到上4个方向可能的值,然后计算所有为‘0’的地方的4个方向的值的最大值 https://www.cnblogs.com/grandyang/p/5599 ...
- [LeetCode] Boom Enemy 炸弹人
Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...
- Bomb Enemy
Description Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number z ...
- Bomb Enemy 炸弹人
Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...
随机推荐
- 设计模式-1-概要(c#版)
最近又重新看了几本设计模式的书籍和文章,现在再看时又有了新的感悟,而这些书籍和文章都是从需求和业务场景讲什么业务可以用什么模式,要不就是纯理论不好理解,其实我们也要理解和佩服这些概括理论的大牛,必须让 ...
- php排序
关于order by排序:单条件排序:order by id(按照id排序默认从小到大) order by id desc(按照id排序从大到小) 多条件排序:order by date,id(先按照 ...
- 使用BOOTICE 恢复系统启动项
使用BOOTICE 恢复系统启动项 我在安装deepin 系统的时候,经常遇到重启进不去系统,每次重启都会进入windows 系统,这让我感到特别头疼,试了好多次都不成功,有些情况是,成功后再次重启又 ...
- FireDAC 连接SQL Server一些要注意的地方
TFDConnection: FetchOptions.Mode 设置为fmAll, 返回全部结果, 否则默认只返回前50条, 效果与open以后再执行FetchAll一样 Specifies how ...
- JavaScript的3大组成部分&&ECMAScript函数&&闭包
1.JavaScript实现是由ECMAScript.DOM和BOM组成.a.ECMAScript仅仅是一个描述,定义了脚本语言的所有属性.方法和对象.b.DOM[文档对象模型]是HTML和XML的应 ...
- 在Ubuntu上安装网易云音乐
对于网易,我只服云音乐,业界良心,用过的人都知道.我最喜欢的就是歌曲的评论功能,还有朋友圈子.里面有很多好段子,还有很多的好故事,基本上,不是分手,就是回忆初恋,还有吐槽的.我认为音乐带给人的不仅是耳 ...
- QMap
#include <QCoreApplication> #include<QMap> #include<QDebug> int main(int argc, cha ...
- linux suse系统防火墙端口开放配置
1.切换到root用户下 2.进入到/etc/sysconfig/SuSEfirewall2 3.修改FW_SERVICES_EXT_TCP=" 22 80 8080 8081 8082 8 ...
- JMeter学习-032-JMeter常见四种变量简介
在JMeter自动化测试脚本编写过程中,经常需要对测试脚本进行一些参数设置.例如,设置测试计划的全局变量(方便切换不同的测试环境).样本线程(HTTP请求等)的参数传递等. 通常,JMeter中常用的 ...
- 关于问题ld:library not found for -lXXX的错误
我猜想错误引起的原因可能是因为我复制target的时候原来的工程中的的link binary with libraries中原来的libpods-xxx.a没有删除.我将多余的libPods删除后解决 ...