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 ...
随机推荐
- Tomcat下work文件夹的作用
1.打补丁,重启tomcat时要删除work文件夹,有缓存. 2.work目录只是tomcat的工作目录,也就是tomcat把jsp转换为class文件的工作目录 jsp,tomcat的工作原理: 当 ...
- CSS布局模型思考
flow模型:默认布局模型,元素从左向右.从上到下依次排列,块状元素独占一行.Position属性对应值static. float模型:主要效果是让本来独占一行的块状元素变成内联-块状元素,并到一排显 ...
- What is SaaS?
SaaS, or Software as a Service, describes any cloud service where consumers are able to access softw ...
- java设计模式——单例(Singleton)模式
在某些场景,你需要找到一个承担职责的对象,并且这个对象是他所属类的唯一实例.此时可以使用单例模式. 单例模式的意图是为了确保一个类有且仅有一个实例,并为他提供一个全局的访问点.创建一个担当独一无二角色 ...
- python中归并排序
# coding=UTF-8 #!/usr/bin/python import sys def merge(nums, first, middle, last): "merge" ...
- Python中几种数据结构的整理,列表、字典、元组、集合
列表:shoplist = ['apple', 'mango', 'carrot', 'banana']字典:di = {'a':123,'b':'something'}集合:jihe = {'app ...
- bzoj3637: Query on a tree VI
Description You are given a tree (an acyclic undirected connected graph) with n nodes. The tree node ...
- Windows开发技术的历史
原文地址:http://www.kuqin.com/windows/20090315/40172.html Windows已经有22年的历史,这22年来,微软官方主力推行的编程语言与API有四个分水岭 ...
- mysql oracle 删除外键约束
mysql alter table xxx drop foreign key xxx cascade; oracle alter table drop constraint xxxxx cascade ...
- 整理的Java资源
这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Maven优于Apache Ant.后者采用了一种过程化的方式 ...