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 ...
随机推荐
- cve-2015-5122漏洞分析
HackTem爆出的第二枚0day,分析了下,做个记录. Poc中一开始会分配一个Array类型的_ar结构. 第一次赋值 此时在a[0 ] –a[1e-1] 处已被赋值为Vector.<uin ...
- Tastypie与Backbone交互
上回玩到Tastypie与jQuery交互,那么现在接着玩玩Tastypie与Backbone.js交互 先把上篇的template文件:D:\project\tastypie\mysite\blog ...
- android--gradle编译龟速?offline!
- 每日一问:面试结束时面试官问"你有什么问题需要问我呢",该如何回答?
面试结束时面试官问"你有什么问题需要问我呢",该如何回答?
- CodeForces 515A
A. Drazil and Date time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- js简单放羊式单元测试-上
这是看了很多js单元测试资料后第一次自己做单元测试,因为资料都在介绍工具怎么使用,js单元测试的工具是在是太多了,各种风格,各种支持的,新的旧的,so 还是自己动手来体验一次 简单 是我给自己的需求很 ...
- [Android] 升级了新的android studio之后 发生如下的报错,The following classes could not be instantiated:
The following classes could not be instantiated:- android.support.v4.widget.DrawerLayout (Open Class ...
- ubuntu下mediawiki的使用
wiki语法确实比较麻烦 终于找到一种简单的方法,LibreOffice可以直接导出为wiki格式 https://apps.ubuntu.com/cat/applications/libreoffi ...
- iOS 模仿一个小项目,总结一下里边的模块
ManoBoo: 参考链接:http://www.jianshu.com/p/fd4c46c31508 这个小的项目是参考ManoBoo的简书的,链接在上方,自己在仿做的过程中,也离不开Man ...
- 对拍老是忘记的看这里:bat代码
需要写三个程序,makedata.exe 产生测试数据, program1.exe 是你要检测的程序,program2.exe 往往是一个正确但效率不高(暴力的居多)的程序. 代码很简单,稍作解释:l ...