Straight-forward and Slow Python Solution


  • 0
    K

    Because it's a bomb, we conduct the traverse in a "spreading-out" manner.

    class Solution(object):
        def hmax(self,grid,i,j):
            count = 0
            for m in range(j-1,-1,-1):
                if grid[i][m]!='W':
                    if grid[i][m]=='E': count+=1
                else:break
            for m in range(j+1,len(grid[0])):
                if grid[i][m]!='W':
                    if grid[i][m]=='E': count+=1
                else:break
            return count
                
        def vmax(self,grid,i,j):
            count = 0
            for m in range(i-1,-1,-1):
                if grid[m][j]!='W':
                    if grid[m][j]=='E': count+=1
                else:break
            for m in range(i+1,len(grid)):
                if grid[m][j]!='W':
                    if grid[m][j]=='E': count+=1
                else:break
            return count        
            
        def maxKilledEnemies(self, grid):
            """
            :type grid: List[List[str]]
            :rtype: int
            """
            ret = 0
            if not grid: return ret
            for i in range(len(grid)):
                for j in range(len(grid[0])):
                    if grid[i][j] == '0':
                        ret = max(ret,self.vmax(grid,i,j)+self.hmax(grid,i,j))
            return ret
            
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.