Page tree
Skip to end of metadata
Go to start of metadata
Rotting Oranges
class Solution:
    def tick(self):
        update_spot = []
        grid = self.grid
        
        for i in range(len(grid)):
            for j in range(len(grid[i])):
                cur = grid[i][j]
                if cur == 2:
                    if i-1 >= 0 and grid[i-1][j] == 1: update_spot.append((i-1, j))
                    if j-1 >= 0 and grid[i][j-1] == 1: update_spot.append((i, j-1))
                    if i+1 < len(grid) and grid[i+1][j] == 1: update_spot.append((i+1, j))
                    if j+1 < len(grid[i]) and grid[i][j+1] == 1: update_spot.append((i, j+1))

        if len(update_spot) == 0:
            for i in range(len(grid)):
                for j in range(len(grid[i])):
                    if grid[i][j] == 1:
                        return -1
            return False
        else:
            for spot in update_spot:
                self.grid[spot[0]][spot[1]] = 2

        return True

    def orangesRotting(self, grid: List[List[int]]) -> int:
        self.grid = grid
        count = 0
        while(True):
            res = self.tick()
            if res:
                count = count + 1
            else:
                break

            if res == -1:
                count = -1
                break
            
        return count
Most Common Word
class Solution:
    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
        d = {}
        
        paragraph = paragraph.replace(',', ' ').replace('.', ' ').replace('?', ' ').replace('!', ' ').replace("'", " ").replace(';', ' ')

        for word in paragraph.split():
            word = word.lower()

            if word not in d.keys():
                d[word] = 1
            else:
                d[word] = d[word] + 1

        max_count = 0
        result_word = ''
        
        for word in d.keys():
            if d[word] > max_count and word not in banned:
                max_count = d[word]
                result_word = word
                
        return result_word
  • No labels