Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- func maxKilledEnemies(_ grid: [[Character]]) -> Int {
- guard grid.count > 0 && grid[0].count > 0 else{ return 0}
- let n = grid.count
- let m = grid[0].count
- var left = [[Int]](repeating: [Int](repeating: 0 , count: m), count : n)
- var right = [[Int]](repeating: [Int](repeating: 0 , count: m), count : n)
- var up = [[Int]](repeating: [Int](repeating: 0 , count: m), count : n)
- var down = [[Int]](repeating: [Int](repeating: 0 , count: m), count : n)
- for i in 0 ..< n {
- for j in 0 ..< m{
- if grid[i][j] == "E"{
- left[i][j] = 1
- right[i][j] = 1
- up[i][j] = 1
- down[i][j] = 1
- }
- if grid[i][j] != "W"{
- if j > 0 {
- left[i][j] += left[i][j - 1]
- }
- if i > 0 {
- up[i][j] += up[i - 1][j]
- }
- }
- }
- }
- for i in stride(from: n - 1, through: 0 , by: -1) {
- for j in stride(from: m - 1, through: 0 , by: -1){
- if grid[i][j] != "W"{
- if i < n - 1 {
- down[i][j] += down[i + 1][j]
- }
- if j < m - 1 {
- right[i][j] += right[i][j + 1]
- }
- }
- }
- }
- var res = 0
- for i in 0 ..< n {
- for j in 0 ..< m{
- if grid[i][j] == "0"{
- res = max(res, right[i][j] + left[i][j] + up[i][j] + down[i][j])
- }
- }
- }
- return res
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement