原题链接在这里: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. Python 5 面对对象编程

    面向对象编程: 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 面向过程编程 ...

  2. 混淆Android JAR包的方法

    http://blog.csdn.net/vurtne_ye/article/details/35599491 1)导出jar包 如何在eclipse上Android工程中导出jar包?google, ...

  3. 80211N、80211AC建链速率值(对应MCS)

    转:http://www.docin.com/p-1713976102.html?docfrom=rrela 注:各种技术对速率提升的贡献各项技术对提高传输速率的贡献如下:子载波数目增加: 802.1 ...

  4. Java中系统时间的获取_currentTimeMillis()函数应用解读

    快速解读 System.currentTimeMillis()+time*1000) 的含义 一.时间的单位转换 1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)1秒=1,000,000 微 ...

  5. 吴恩达深度学习笔记(十一)—— dropout正则化

    主要内容: 一.dropout正则化的思想 二.dropout算法流程 三.dropout的优缺点 一.dropout正则化的思想 在神经网络中,dropout是一种“玄学”的正则化方法,以减少过拟合 ...

  6. MySQL/MariaDB数据库备份与恢复之mysqlpump入门操作

    创建测试用表:MariaDB [music]>  create table summary(id int,info char(128));Query OK, 0 rows affected (0 ...

  7. EF Code-First 学习之旅 Fluent API

    Mappings To Database Model-wide Mapping Set default Schema Set Custom Convetions Entity Mapping To S ...

  8. pandas通过字典生成dataframe

    1.将一个字典输入: 该字典必须满足:value是一个list类型的元素,且每一个key对应的value长度都相同: (以该字典的key为columns) >>> import pa ...

  9. Python3一些包的下载

    首先在windows的Python扩展包网址:http://www.lfd.uci.edu/~gohlke/pythonlibs/ 这里举例下载opencv3.2.0的安装包 我的电脑是win10,6 ...

  10. 在openrc中从keystone V2 到 V3

    #!/bin/bash ] then echo "Usage $0 <keystone.rc>" exit fi . $ NEW_OS_AUTH_URL=`echo $ ...