原题链接在这里:https://leetcode.com/problems/bomb-enemy/description/

题目:

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: You can only put the bomb at an empty cell.

Example:

Input: [["0","E","0","0"],["E","0","W","E"],["0","E","0","0"]]
Output: 3
Explanation: For the given grid,
0 E 0 0
E 0 W E
0 E 0 0
Placing a bomb at (1,1) kills 3 enemies.

题解:

DP问题. 要储存当前格子所在行和列能看到的敌人个数. 就拿两排array 来计数. 当遇到边界或者墙,就需要重新计算.

用数组colHits储存列的. 因为行的更新和指针是同向移动的,所以用一个number就可以.

递推时, 在遇到墙和边界时清零重算, 否则敌人数目不变.

起始值都是0.

Time Complexity: O(m*n). m = grid.length, n = grid[0].length. 每个格子至多走两边.

Space: O(n). 可选m 和n中较小的.

AC Java:

 class Solution {
public int maxKilledEnemies(char[][] grid) {
if(grid == null || grid.length == 0 || grid[0].length == 0){
return 0;
} int m = grid.length;
int n = grid[0].length; int res = 0;
int [] colHits = new int[n];
int rowHits = 0; for(int i = 0; i<grid.length; i++){
for(int j = 0; j<grid[0].length; j++){
if(i==0 || grid[i-1][j]=='W'){
colHits[j] = 0;
for(int k = i; k<m&&grid[k][j]!='W'; k++){
colHits[j] += (grid[k][j] == 'E' ? 1 : 0);
}
} if(j==0 || grid[i][j-1]=='W'){
rowHits = 0;
for(int k = j; k<n&&grid[i][k]!='W'; k++){
rowHits += (grid[i][k] == 'E' ? 1 : 0);
}
} if(grid[i][j] == '0'){
res = Math.max(res, rowHits + colHits[j]);
}
}
} return res;
}
}

LeetCode 361. Bomb Enemy的更多相关文章

  1. [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 ...

  2. leetcode 361.Bomb Enemy(lintcode 553. Bomb Enemy)

    dp 分别计算从左到右.从右到左.从上到下.从下到上4个方向可能的值,然后计算所有为‘0’的地方的4个方向的值的最大值 https://www.cnblogs.com/grandyang/p/5599 ...

  3. 【LeetCode】361. Bomb Enemy 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力搜索 日期 题目地址:https://leetco ...

  4. 361. Bomb Enemy

    这个题确实不会..只能想到naive的做法,不过那样应该是O(n³),不会满足要求. 看TAG是DP,那应该是建立DP[][]记录每点可炸的情况.一个点如果左边/上边是墙,或者左边/上边是边界,就要重 ...

  5. [LeetCode] Bomb Enemy 炸弹人

    Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...

  6. Leetcode: Bomb Enemy

    Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...

  7. Bomb Enemy -- LeetCode

    Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...

  8. Bomb Enemy

    Description Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number z ...

  9. Bomb Enemy 炸弹人

    Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...

随机推荐

  1. 加载顺序 ready onload onreadystatechange

    js文件是异步加载, js是在什么时候被加载执行的   动态引入的外部 JS 文件在各浏览器中的加载顺序不一致       1/ready,表示文档结构已经加载完成(不包含图片等非文字媒体文件):比如 ...

  2. Linux文件系统管理 fdisk分区命令

    概述 我们在安装操作系统的过程中已经对系统硬盘进行了分区,但是如果我新添加了一块硬盘,想要正常使用时,在Linux中有专门的分区命令 fdisk 和 parted.其中 fdisk 命令较为常用,但不 ...

  3. linux 音频驱动

    转:https://wenku.baidu.com/view/7394e16d7e21af45b307a8dc.html?pn=51 linux_sound_alsa_ALSA体系SOC子系统中数据流 ...

  4. CSS3 3D旋转下拉菜单

    在线演示 本地下载

  5. INSPIRED启示录 读书笔记 - 第23章 改进现有产品

    不是一味地添加功能 改进产品不是简单地满足个别用户的要求,也不能对用户调查的结果照单全收.能提高指标的功能才是关注的重点.应该找准方向,分析关键指标,有针对性地改进产品

  6. Yii和ThinkPHP对比杂谈

    关于ThinkPHP(以下简称TP)和Yii Framework(以下简称Yii)的背景.作者和速度方面就不涉及了.因为速度是一个很复杂的问题,牵扯的因素很多.我不得不承认ThinkPHP是 一个是国 ...

  7. 【转载】树链剖分.By.Xminh

    轻重链剖分 其实就是俗称的树链剖分. PS:树链剖分不止有轻重链剖分.但是大多数时候的树链剖分指的就是轻重链剖分. dfs序 给树的节点重新编号,使得任意一个节点满足子树的dfs序都比它要大,而且它子 ...

  8. MyEclipse+PyDev配置搭建Python开发环境

    打开help-> install from catalog 输入pydev查找并安装pydev 根据提示进行安装,安装完后重启myeclipse

  9. 先有Class还是先有Object?

    先有Class还是先有Object? Java的对象模型中: 所有的类都是Class类的实例,Object是类,那么Object也是Class类的一个实例. 所有的类都最终继承自Object类,Cla ...

  10. Linux嵌入式 -- 内核 - 内存管理

    1.  逻辑地址 线性地址 物理地址 段式管理: 16位CPU,20根地址总线,可寻址1M内存,但是只有16位的寄存器,64K. 逻辑地址  =  段基地址 + 段内偏移地址 物理地址 PA  = 段 ...