Python, beats 100% O(mn) time O(n) space


  • 0
    G
    class Solution:
        def maxKilledEnemies(self, grid):
            """
            :type grid: List[List[str]]
            :rtype: int
            """
            if not grid: return 0
            self.doRows(grid)
            return self.doCols(grid)
            
            
        def doRows(self, grid):
            for i in range(len(grid)):
                m = []
                e = 0
                for j in range(len(grid[0])):
                    if grid[i][j] == 'W':
                        while m:
                            l = m.pop()
                            grid[i][l] = e
                        e = 0
                    elif grid[i][j] == 'E':
                        e += 1
                    else:
                        m.append(j)
                while m:
                    l = m.pop()
                    grid[i][l] = e
                    
        def doCols(self, grid):
            res = 0;
            for j in range(len(grid[0])):
                m = []
                e = 0
                for i in range(len(grid)):
                    if grid[i][j] == 'W':
                        while m:
                            l = m.pop()
                            grid[l][j] += e
                            res = max(res, grid[l][j])
                        e = 0
                    elif grid[i][j] == 'E':
                        e += 1
                    else:
                        m.append(i)
                while m:
                    l = m.pop()
                    grid[l][j] += e
                    res = max(res, grid[l][j])
            return res
    

Log in to reply
 

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