361. Bomb Enemy
最后更新
二刷
11-Jan-2017
首先,正常做的话,是每个格子都纵向横向CHECK一下,这样复杂度爆炸。。
看起来是用dp[][]记录情况,避免重复计算。但是这样的话,得有2个dp[][],分别记录某一个格子可在横向和纵向分别干死多少敌人。 空间复杂度又爆炸。。O(2MN)
不过可以优化。
遍历的时候是从上往下,从左往右,所以对于grid[i][j]来说,假如他左边也是炸弹,那他横向能消灭多少敌人 和 左边格子横向消灭敌人数量一致。
纵向看他上面是不是炸弹,是的话也和上面能消灭的纵向敌人的数量一致。
所以对于一个格子,如果他左面或者上面是炸弹,可以直接用上面的数据,否则就要重新计算需要的方向,最后2个方向加起来。
空间上,每一行用一个变量记录能干死多少人就行了,纵向得一维数组,记录上一行这个位置能干死几个。
叙述挺麻烦,看眼代码一目了然。
Time Complexity:
O(MN)? 不会做,每个格子遍历,但是貌似worst case不太肯定,WORSTCASE是2个方向都要测一下,这也就意味着下一行下一列的格子不需要这样做了。。所以= =不确定。
Space: O(N)
public class Solution {
public int maxKilledEnemies(char[][] grid) {
if (grid.length == 0) return 0;
int rows = grid.length;
int cols = grid[0].length;
int rowKill = 0;
int[] colKill = new int[cols];
int res = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// horizontal
if (j == 0 || grid[i][j-1] == 'W') {
rowKill = 0;
for (int t = j; t < cols && grid[i][t] != 'W'; t++) {
if (grid[i][t] == 'E') rowKill ++;
}
}
// vertical
if (i == 0 || grid[i-1][j] == 'W') {
colKill[j] = 0;
for (int t = i; t < rows && grid[t][j] != 'W'; t++) {
if (grid[t][j] == 'E') colKill[j] ++;
}
}
int tempTotal = rowKill + colKill[j];
if (grid[i][j] == '0') {
res = Math.max(res, tempTotal);
}
}
}
return res;
}
}
一刷
14-Oct-2016
这个题确实不会。。只能想到naive的做法,不过那样应该是O(n³),不会满足要求。
看TAG是DP,那应该是建立DP[][]记录每点可炸的情况。一个点如果左边/上边是墙,或者左边/上边是边界,就要重新计算,否则跟左边/上边的那个格子的情况一样。 是空位就来一发,然后取最大。
这样应该是mn的extra space,时间变成(常数,但是不知道是什么。。)*n²。
不是很确定,看了答案,发现很巧妙。。
因为遍历是在一行一行的基础上,每列每列,所以没必要MN的extra space,一个变量记录每行的情况,过了那一行就自动更新,另一个int[cols]记录每列的情况就行了。。
时间上还是常熟*(n²)
public class Solution
{
public int maxKilledEnemies(char[][] grid)
{
if(grid.length == 0) return 0;
int res = 0;
int row = 0;
int[] col = new int[grid[0].length];
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')
{
col[j] = 0;
int t = i;
while(t < grid.length && grid[t][j] != 'W')
{
if(grid[t][j] == 'E') col[j]++;
t++;
}
}
if(j == 0 || grid[i][j-1] == 'W')
{
row = 0;
int t = j;
while(t < grid[0].length && grid[i][t] != 'W')
{
if(grid[i][t] == 'E') row++;
t++;
}
}
if(grid[i][j] == '0') res = Math.max(res,col[j]+row);
}
}
return res;
}
}
361. Bomb Enemy的更多相关文章
- 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 ...
- 【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] 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 ...
随机推荐
- 用layer添加UIView的动画
项目有时会遇到用UIView 添加动画的情况,这里我觉得在layer上添加动画比较好,因为可以详细地设定动画属性,方便理解 下面是一个旋转动画: -(void)roundBtnAction:(id)s ...
- iOS UICollectionview的详细介绍
转载自:http://jinqianchina.github.io/2015/08/16/UICollectionview%E7%9A%84%E4%BD%BF%E7%94%A8%E8%AF%A6%E8 ...
- 十三、C# 事件
1.多播委托 2.事件 3.自定义事件 在上一章中,所有委托都只支持单一回调. 然而,一个委托变量可以引用一系列委托,在这一系列委托中,每个委托都顺序指向一个后续的委托, 从而形成了一个委托链,或 ...
- Qt经典出错信息之”Basic XLib functionality test failed!”
解决方法: 此完整出错信息是在./configure阶段Basic XLib functionality test failed!You might need to modify the includ ...
- 【HDU3487】【splay分裂合并】Play with Chain
Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...
- 【POJ2761】【区间第k大】Feed the dogs(吐槽)
Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...
- URPF 简单流程
主要功能是防止基于源地址欺骗的网络攻击. 路由器接口一旦使能URPF功能,当该接口收到数据报文时,首先会对数据报文的源地址进行合法性检查,对于源地址合法性检查通过的报文,才会进一步查找去往目的地址的转 ...
- Bootstrap_Javascript_图片轮播
一 . 结构分析 一个轮播图片主要包括三个部分: ☑ 轮播的图片 ☑ 轮播图片的计数器 ☑ 轮播图片的控制器 第一步:设计轮播图片的容器.在 Bootstrap 框架中采用 carousel 样式,并 ...
- PHP常用代码段:
1.PHP加密解密 function encryptDecrypt($key, $string, $decrypt){ if($decrypt){ $decrypted ...
- YII 验证功能
YII 表单的验证可以在module目录下的xxx.php里面定义验证方法,设定需要验证的字段就行 //用户表单验证,在模型里面设置一个方法,具体表单验证规则 public functi ...