from collections import deque class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: queue = deque() for x in range(len(grid)): for y in range(len(grid[x])): if grid[x][y] == 2: queue.append([x, y]) news = [[-1, 0], [0, 1], [0, -1], [1, 0]] ans = 0 while queue: s = len(queue) for i in range(s): x, y = queue.popleft() for l, m in news: if 0 <= x + l < len(grid) and 0 <= y + m < len(grid[0]): if grid[x + l][y + m] == 1: grid[x + l][y + m] = 2 queue.append([x + l, y + m]) if queue: ans += 1 for x in range(len(grid)): for y in range(len(grid[0])): if grid[x][y] == 1: return -1 return ans |
import re class Solution: def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: tokens = re.split("[!?',;. ]", paragraph) words = list(map(lambda x: re.sub(r"[!?',;.]", "", x), tokens)) words = list(map(lambda x: x.lower(), words)) words = list(filter(lambda x: x != '', words)) dic = {} for word in words: dic.setdefault(word, 0) dic[word] += 1 for b in banned: dic.pop(b, None) max = 0 for d in dic: if dic[d] > max: max = dic[d] ans = d return ans |