Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
bomb-enemy.cpp 1.51 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2016-08-04 22:57 +08:00 . Update bomb-enemy.cpp
// Time: O(m * n)
// Space: O(m * n)
class Solution {
public:
int maxKilledEnemies(vector<vector<char>>& grid) {
int result = 0;
if (grid.empty() || grid[0].empty()) {
return result;
}
vector<vector<int>> down{grid.size(), vector<int>(grid[0].size())};
vector<vector<int>> right{grid.size(), vector<int>(grid[0].size())};
for (int i = grid.size() - 1; i >= 0; --i) {
for (int j = grid[0].size() - 1; j >= 0; --j) {
if (grid[i][j] != 'W') {
if (i + 1 < grid.size()) {
down[i][j] = down[i + 1][j];
}
if (j + 1 < grid[0].size()) {
right[i][j] = right[i][j + 1];
}
if (grid[i][j] == 'E') {
++down[i][j];
++right[i][j];
}
}
}
}
int left = 0;
vector<int> up(grid[0].size());
for (int i = 0; i < grid.size(); ++i) {
left = 0;
for (int j = 0; j < grid[0].size(); ++j) {
if (grid[i][j] == 'W') {
up[j] = 0;
left = 0;
} else if (grid[i][j] == 'E') {
++up[j];
++left;
} else {
result = max(result, left + up[j] + right[i][j] + down[i][j]);
}
}
}
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助