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 ...
随机推荐
- UVA 12544 - Beehives O(nm) 无向图最小环
Bees are one of the most industrious insects. Since they collect nectarand pollen from flowers, they ...
- 【算法杂谈】LJX的迪杰斯特拉算法报告
迪杰斯特拉(di jie qi)算法 这里有一张图: 假设要求从1号节点到5号节点的最短路.那么根据迪杰斯特拉算法的思想,我们先看: 节点1,从节点1出发的一共有3条路,分别是1-6.1-3.1-2. ...
- 【转】有关Oracle随机字符串的生成方法及具体应用
Oracle生成随机字符串的方法是通过dbms_random.string实现的. 1.dbms_random.string用法Oracle官方文档参考链接:http://download.oracl ...
- ZeroMQ接口函数之 :zmq_bind - 绑定一个socket
ZeroMQ 官方地址 : http://api.zeromq.org/4-0:zmq-bind zmq_bind(3) ZMQ Manual - ZMQ/3.2.5 Name zmq_bind - ...
- java强制类型转换
在Java项目的实际开发和应用中,常常需要用到将对象转为String这一基本功能.本文将对常用的转换方法进行一个总结.常用的方法有Object.toString(),(String)要转换的对象,St ...
- jquery mobile系列问题汇总整理(传播知识,利己利人)
我在用jquery mobile做项目时,遇到jm在下拉框等组件里不能正常动态更新内容,查找了相关资料,在这里抛砖引玉,先提供一个解决下拉框内容写入更新的解决方法: jm解决下拉框内容写入的方法可以这 ...
- android——判断网络状态
平常我们玩qq的时候我们没联网 ,qq上面回显示"网络连接不给力,请检查网络连接"那么它是 通过什么方式判断网络是否连接的呢? 下面将写个案例展示如何检查网络连接状态的 由于我们这 ...
- 【Java】实战Java虚拟机之五“开启JIT编译”
今天开始实战Java虚拟机之五“开启JIT编译” 总计有5个系列 实战Java虚拟机之一“堆溢出处理” 实战Java虚拟机之二“虚拟机的工作模式” 实战Java虚拟机之三“G1的新生代GC” 实战Ja ...
- 通过PowerShell启用AADC的密码同步功能
在AADC工具里面也可以启用. 下面这个是世纪互联工程师提供的Powershell脚本(我测试了下是可以的),其中,$adConnector和$aadConnector可以在AADC工具里面找到. $ ...
- 本周psp
本周PSP 类别 内容 开始时间 中止时间 终止时间 总用时 产品计划会议 定义产品的用户需求,以及从这个产品中得到什么.解决啥问题 18:00 0 20:00 120分钟 撰写博客 会议记录与个 ...