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 ...
随机推荐
- OpenGL学习进程(4)第二课:绘制图形
本节是OpenGL学习的第二个课时,下面介绍如何用点和线来绘制图形: (1)用点的坐标来绘制矩形: #include <GL/glut.h> void display(void) ...
- 自己动手编译Android源码(超详细)
http://www.jianshu.com/p/367f0886e62b 在Android Studio代码调试一文中,简单的介绍了代码调试的一些技巧.现在我们来谈谈android源码编译的一些事. ...
- transition失效问题
关于transition,css教程中有一个很简单的例子: <!DOCTYPE html> <html> <head> <meta charset=" ...
- Bootstrap3全局CSS样式
目录 1. 概览 2. 栅栏系统 3. 文本 4. 列表 5. 代码 6. 表格 7. 表单 7.1 基本实例 7.2 内联表单 7.3 水平排列的表单 8. 按钮 9. 图片 10. 辅助类 10. ...
- Python 字符串概念和操作
# 字符串概念:由单个字符串组成的一个集合 # 普通字符串(非原始字符串) str = "abc" print(str) # abc # 原始字符串(前面加r) str = r&q ...
- 使用eclipse搭建第一个python+Django的web开发实例
python+Django的web开发实例 一.创建一个项目如果这是你第一次使用Django,那么你必须进行一些初始设置.也就是通过自动生成代码来建立一个Django项目--一个Django项目的 ...
- CodeWars上的JavaScript技巧积累
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice The sli ...
- linux基础(8)-文件处理(awk 、sed、grep)
grep基本用法 格式:grep [选项] [模式] [文件] 选项: -c:只显示有多少行匹配 ,而不具体显示匹配的行 -n:在每一行前面打印该行在文件中的行数 -i:在字符串比较的时候忽略大小 ...
- Spring Boot 注释
1.@RestController@RestController ≍ @Controller + @ResponseBody在Controller文件 public class xxxx 前面加用于返 ...
- openstack-ansible -- 3 Target hosts
Installing the operating system Install the Ubuntu Server 14.04 (Trusty Tahr) LTS 64-bit operating s ...