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.

Example

Example1

Input:
grid =[
"0E00",
"E0WE",
"0E00"
]
Output: 3
Explanation:
Placing a bomb at (1,1) kills 3 enemies

Example2

Input:
grid =[
"0E00",
"EEWE",
"0E00"
]
Output: 2
Explanation:
Placing a bomb at (0,0) or (0,3) or (2,0) or (2,3) kills 2 enemies

思路:

预处理出每个点向四个方向能炸到的人数,然后枚举所有点,取最大值即可

public class Solution {
/**
* @param grid: Given a 2D grid, each cell is either 'W', 'E' or '0'
* @return: an integer, the maximum enemies you can kill using one bomb
*/
public int maxKilledEnemies(char[][] grid) {
int m = grid.length;
int n = m > 0 ? grid[0].length : 0; int result = 0, rows = 0;
int[] cols = new int[n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (j == 0 || grid[i][j-1] == 'W') {
rows = 0;
for (int k = j; k < n && grid[i][k] != 'W'; ++k)
if (grid[i][k] == 'E')
rows += 1;
}
if (i == 0 || grid[i-1][j] == 'W') {
cols[j] = 0;
for (int k = i; k < m && grid[k][j] != 'W'; ++k)
if (grid[k][j] == 'E')
cols[j] += 1;
} if (grid[i][j] == '0' && rows + cols[j] > result)
result = rows + cols[j];
}
}
return result;
}
}

  

Bomb Enemy的更多相关文章

  1. Leetcode: 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] Bomb Enemy 炸弹人

    Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...

  3. Bomb Enemy -- LeetCode

    Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...

  4. LeetCode 361. Bomb Enemy

    原题链接在这里:https://leetcode.com/problems/bomb-enemy/description/ 题目: Given a 2D grid, each cell is eith ...

  5. [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 ...

  6. Bomb Enemy 炸弹人

    Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...

  7. 【LeetCode】361. Bomb Enemy 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力搜索 日期 题目地址:https://leetco ...

  8. leetcode 361.Bomb Enemy(lintcode 553. Bomb Enemy)

    dp 分别计算从左到右.从右到左.从上到下.从下到上4个方向可能的值,然后计算所有为‘0’的地方的4个方向的值的最大值 https://www.cnblogs.com/grandyang/p/5599 ...

  9. 361. Bomb Enemy

    这个题确实不会..只能想到naive的做法,不过那样应该是O(n³),不会满足要求. 看TAG是DP,那应该是建立DP[][]记录每点可炸的情况.一个点如果左边/上边是墙,或者左边/上边是边界,就要重 ...

随机推荐

  1. Sitecore安全性第1部分:自定义角色和权限

    安全性是任何Sitecore构建的重要组成部分.它可确保您的内容作者具有适当级别的访问权限,以管理他们拥有的内容,并授予他们访问不同Sitecore功能的权限. Sitecore附带了许多提供功能访问 ...

  2. Java-关于接口调用的处理

    前言:这是我的第一篇博文,是我对现在一些接口调用的梳理,写的不好,请见谅. 序:接口无非就是“你调用别人的接口”和“别人调用你的接口”,我会对这两种情况分别的理一下我的思路. 准备:我使用的是Http ...

  3. Kafka学习笔记之如何永久删除Kafka的Topic

    0x00 问题描述 使用kafka-topics --delete命令删除topic时并没有真正的删除,而是把topic标记为:“marked for deletion”,导致重新创建相同名称的Top ...

  4. ClassPathBeanDefinitionScanner 说明

    Spring 工具类 ClassPathBeanDefinitionScanner 组件Bean定义扫描https://blog.csdn.net/andy_zhang2007/article/det ...

  5. C#实现服务器间文件同步

    using System.IO; /// <summary> /// 远程登陆服务器 /// </summary> /// <param name="remot ...

  6. java基础 接口常量

    /** * 接口当中也可以定义"成员变量", 但是必须使用public static final三个关键字进行修饰 * 从效果上看,这其实就是接口的[常用] * 格式: * pub ...

  7. 使用 HttpWebRequest 类做 POST 请求没有应反

    这几天给系统做第三方集成, 需要调用另一个软件的一个接口, 通过 HTTP 的方式调用,调用代码也挺简单的: string serviceUrl = string.Format("{0}/{ ...

  8. CSS文本居中问题

    文本水平居中 水平居中比较简单,将对应的html元素text-align属性值为center,其子元素就会水平居中. 文本垂直居中 单行文本垂直居中 设置文本元素的line-height属性值为元素高 ...

  9. Redux 中间件和异步操作

    回顾一下Redux的数据流转,用户点击按钮发送了一个action,  reducer 就根据action 和以前的state 计算出了新的state, store.subscribe 方法的回调函数中 ...

  10. element-ui 上传图片或视频时,先回显在上传

    <el-upload class="upload-demo" ref="vidos" :action="URL+'/api/post/file' ...