原题链接在这里: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学习进程(8)字符串內建函数

        Python字符串內建函数实现了string模块的大部分方法,并包括了对Unicode编码方式的支持.     (1)capitalize(): 将字符串的第一个字母变成大写,其他字母变小写. ...

  2. The Collections Module内建collections集合模块

    https://www.bilibili.com/video/av17396749/?p=12 Python函数式编程中的迭代器,生成器详解 课程内容 1.iterators are objects ...

  3. 大数据架构之:Kafka

    Kafka 是一个高吞吐.分布式.基于发布订阅的消息系统,利用Kafka技术可在廉价PC Server上搭建起大规模消息系统.Kafka具有消息持久化.高吞吐.分布式.多客户端支持.实时等特性,适用于 ...

  4. STP生成树协议原理与算法解析

    转:https://wenku.baidu.com/view/2e52b91d866fb84ae45c8d34.html

  5. 物理分辨率与逻辑分辨率,pt与px

    有些小伙伴们,在使用chrome的移动端调试工具调试网页的时候,会发现iphone6上的尺寸为375*667,不禁差异,iphone6的分辨率不是750*1334吗? 实际上调试器上的大小单位不是px ...

  6. 优美的英文诗歌Beautiful English Poetry

    <When you are old>——<当你老了> --- William Butler Yeats ——威廉·巴特勒·叶芝When you are old and grey ...

  7. WINDOWS下好用的MongoDB 3.0以上客户端工具: NoSql Manager

      WINDOWS下好用的MongoDB 3.0以上客户端工具: NoSql Manager https://www.mongodbmanager.com/download  

  8. sg函数的应用

    刚刚接触到sg函数突然感觉到原来可以这么好用,sg函数应该算是博弈论中比较经典的东西了.下面来说说sg函数: 从网上搜集资料终于能看懂了下面解释来自http://www.cnblogs.com/cj6 ...

  9. hadoop 知识点总结

    关于元数据的checkpoint 每隔一段时间,会由secondary namenode将namenode上积累的所有edits和一个最新的fsimage下载到本地,并加载到内存进行merge(这个过 ...

  10. SQL Server technical bulletin - How to resolve a deadlock

    https://support.microsoft.com/en-us/help/832524/sql-server-technical-bulletin-how-to-resolve-a-deadl ...