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 ...
随机推荐
- [自动运维]oracle自动备份
数据是应用的核心部分,程序坏了换台机器重新发布就可以,但数据一旦丢失,造成的损失将不可挽回,程序发布到生产后,数据的备份便显得尤为重要,由于不一定所有的服务均有资金完成高级的备份如RAC和DG,在我们 ...
- Compare Version Numbers
Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 & ...
- bzoj3212 pku3468 A Simple Problem with Integers
一个有初值的数列.区间加.区间查 用线段树直接水过 然而并没有1A,主要是做题太快没看规模结果没注意线段树要用longlong建 卧槽怎么可以这么坑爹,害得我看见wa心慌了,还以为连线段树都要跪 一开 ...
- python函数
一.函数: 创建函数:使用def语句 举例:定义一个返回斐波那楔数列列表的函数 def fibs(num): result = [0,1] for i in range(num-2): result. ...
- php字符串笔记
1.explode:英语的意思就是爆炸,此函数用于将字符串切成数组 2.implode:英语意思是向心聚,将数据拼成字符串. 3.substr:截取字符串,从指定位置截取指定长度,不指定就到最后 4. ...
- nginx域名隐性(地址栏域名不变)跳转
1.完全url的域名隐性跳转 server_name a.b.com location / { proxy_pass http://x.y.com; } 效果:浏览器地址栏中输入a.b.com域名不变 ...
- &与&&,|与||
http://bokeid.blog.163.com/blog/static/93102786201181710259178/ &&:逻辑运算符,连接两个或多个表达式,结果为TRUE或 ...
- linq To DataTable
//将IEnumerable<T>类型的集合转换为DataTable类型 1111 public static DataTable LINQToDataTable<T>(IEn ...
- 文本编辑器VIM/VI
vim/vi 功能强大全屏文本编辑器.主要是建立,编辑,显示文本. www.vim.org 构建shell脚本 :set number 使文件出现行号 a 光标后附件文件 i在光标前插入文 ...
- ccf模板生成
问题描述 成成最近在搭建一个网站,其中一些页面的部分内容来自数据库中不同的数据记录,但是页面的基本结构是相同的.例如,对于展示用户信息的页面,当用户为 Tom 时,网页的源代码是 而当用户为 Jerr ...