LeetCode 361. Bomb Enemy
原题链接在这里: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的更多相关文章
- [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 ...
- leetcode 361.Bomb Enemy(lintcode 553. Bomb Enemy)
dp 分别计算从左到右.从右到左.从上到下.从下到上4个方向可能的值,然后计算所有为‘0’的地方的4个方向的值的最大值 https://www.cnblogs.com/grandyang/p/5599 ...
- 【LeetCode】361. Bomb Enemy 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力搜索 日期 题目地址:https://leetco ...
- 361. Bomb Enemy
这个题确实不会..只能想到naive的做法,不过那样应该是O(n³),不会满足要求. 看TAG是DP,那应该是建立DP[][]记录每点可炸的情况.一个点如果左边/上边是墙,或者左边/上边是边界,就要重 ...
- [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: 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 ...
- 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 ...
随机推荐
- IDA 调试 Android 方法及简单的脱壳实现
IDA 调试 Android 方法及简单的脱壳实现 标签: android原创逆向调试dalvik 2016-05-24 14:24 9286人阅读 评论(3) 收藏 举报 分类: 原创(25) An ...
- PAT 天梯赛 L1-033. 出生年 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-033 AC代码 #include <cstdio> #include <cstring> ...
- Ajax+Spring MVC实现跨域请求(JSONP)
背景: AJAX向后台(springmvc)发送请求,报错:已阻止交叉源请求:同源策略不允许读取 http://127.0.0.1:8080/DevInfoWeb/getJsonp 上的远程资源.可 ...
- asp.net 在AcquireRequestState事件中判断登陆验证。
Global中添加AcquireRequestState事件. protected void Application_AcquireRequestState(object sender, EventA ...
- Hibernate关联关系的CRUD
本文以Group和User(一对多.多对一)双向关联为例,介绍关联关系的CRUD 下面先介绍两个属性 cascade:只影响CRUD中的CUD,即存储(save).更新(update).删除(de ...
- 什么是JavaBeans?
参看维基百科,归纳出以下几条: JavaBeans是指符合某些标准的类, Bean这个名称用于涵盖这个标准, 其目的在于创建可重用的Java组件. 由于Bean是很“死板”的东西,因此它可以持久存储, ...
- python中编写无参数decorator
Python的 decorator 本质上就是一个高阶函数,它接收一个函数作为参数,然后,返回一个新函数. 使用 decorator 用Python提供的 @ 语法,这样可以避免手动编写 f = de ...
- Python编程-数据类型方法
一.进制简介 进制也就是进位制,是人们规定的一种进位方法.对于任何一种进制---X进制,就表示某一位置上的数运算时是逢X进一位.十进制是逢十进一,十六进制是逢十六进一,二进制就是逢二进一,以此类推,x ...
- python配置文件操作
步骤: 1.导入模块 import configparser 2.创建实例 cf = configparser.ConfigParser() 3.读取配置文件,若配置文件中有中文,则需设置编码格式 ...
- 《Inode与Block重要知识总结核心讲解》【转】
本文转载自:https://blog.csdn.net/BlackEnn/article/details/50787092 1.查看/dev/sda1下磁盘分区的block大小: 2.查看单个inod ...