def orangesRotting(self, grid: List[List[int]]) -> int: q = collections.deque() Array.print2DArray(grid) h = len(grid) w = len(grid[0]) for i in range(h): for j in range(w): if grid[i][j] == 2: q.append((i, j)) time = 0 while (q): print(q) for _ in range(len(q)): i, j = q.popleft() grid[i][j] = 2 for i_next, j_next in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]: if 0 <= i_next < h and 0 <= j_next < w and grid[i_next][ j_next] == 1: grid[i_next][j_next] = -1 q.append((i_next, j_next)) time += 1 Array.print2DArray(grid) print() for i in range(h): for j in range(w): if grid[i][j] == 1: return -1 if time == 0: return 0 else: return time - 11
def canPartition(self, nums: List[int]) -> bool: if len(nums) < 2: return False sum_of_nums = sum(nums) max_of_nums = max(nums) if sum_of_nums % 2 != 0: return False target = sum_of_nums // 2 if max_of_nums > target: return False dp = [[0] * (int(target) + 1) for _ in range(len(nums))] for each in dp: each[0] = 1 dp[0][nums[0]] = 1 for i in range(1, len(nums)): for j in range(1, int(target) + 1): if j < nums[i]: dp[i][j] = dp[i - 1][j] else: dp[i][j] = dp[i - 1][j] or dp[i - 1][j - nums[i]] array.print2DArray(dp) return dp[-1][-1] == 1
def exist(self, board: List[List[str]], word: str) -> bool: Array.print2DArray(board) rows = len(board) w = len(board[0]) visited = [[0] * w for _ in range(rows)] for i in range(rows): for j in range(w): if not self.solved: self.search(board, visited, w, rows, i, j, word) return self.solved
dirs = [(-1, -1), (1, 1), (1, -1), (-1, 1)] for direction in dirs: if self.canHitDiagonal(board, n, i, j, direction): return False return True def canHitDiagonal(self, board, n, i, j, direction) -> bool: v, h = direction next_i = i + v next_j = j + h if next_i < 0 or next_i >= n or next_j < 0 or next_j >= n: return False if board[next_i][next_j] == 'Q': return True return self.canHitDiagonal(board, n, next_i, next_j, direction) def renderResult(self, board): result = [''.join(x) for x in board] return result r = Solution().solveNQueens(4) for i, each in enumerate(r): print('solution {}'.format(i + 1)) Array.print2DArray(each) print()
i += 1 elif direction == 'd': direction = 'l' i -= 1 j -= 1 elif direction == 'l': direction = 'u' j += 1 i -= 1 elif direction == 'u': direction = 'r' j += 1 i += 1 visited[i][j] = 1 m[i][j] = count count += 1 if direction == 'r': j += 1 elif direction == 'd': i += 1 elif direction == 'l': j -= 1 elif direction == 'u': i -= 1 return m m = Solution().generateMatrix(2) Array.print2DArray(m)
w = len(grid[0]) count = 0 for i in range(h): for j in range(w): if grid[i][j] == 1: c = self.dfs(i, j, w, h, grid) if c != 1: count += c return count def dfs(self, i, j, w, h, grid): count = 0 for y in range(h): if grid[y][j] == 1: grid[y][j] = 2 count += 1 + self.dfs(y, j, w, h, grid) for x in range(w): if grid[i][x] == 1: grid[i][x] = 2 count += 1 + self.dfs(i, x, w, h, grid) return count grid = [[0,0,0,1,0,0,0,0,0,0],[1,0,1,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,1,0,0,0,0,0,1,0,0]] Array.print2DArray(grid) r = Solution().countServers(grid) print() Array.print2DArray(grid) print(r)
virtual_start = (i * N + j) * 4 if grid[i][j] == '/': union_find.union(virtual_start + 1, virtual_start + 2) union_find.union(virtual_start + 0, virtual_start + 3) elif grid[i][j] == '\\': union_find.union(virtual_start + 0, virtual_start + 1) union_find.union(virtual_start + 2, virtual_start + 3) else: union_find.union(virtual_start + 1, virtual_start + 0) union_find.union(virtual_start + 2, virtual_start + 1) union_find.union(virtual_start + 3, virtual_start + 2) # print(i,j) # 不是某行第一个 if j != 0: # print('union row') union_find.union(virtual_start, virtual_start - 2) # 不是某列第一个 if i != 0: # print('union column') union_find.union(virtual_start + 3, virtual_start - N * 4 + 1) # union_find.print() return union_find.num_of_groups grid = ["//", "/ "] array.print2DArray(grid) r = Solution().regionsBySlashes(grid) print(r)
if 0 <= i_next < h and 0 <= j_next < w: if board[i_next][j_next] == 'M': count += 1 return count b = [["E", "E", "E", "E", "E", "E", "E", "E"], ["E", "E", "E", "E", "E", "E", "E", "M"], ["E", "E", "M", "E", "E", "E", "E", "E"], ["M", "E", "E", "E", "E", "E", "E", "E"], ["E", "E", "E", "E", "E", "E", "E", "E"], ["E", "E", "E", "E", "E", "E", "E", "E"], ["E", "E", "E", "E", "E", "E", "E", "E"], ["E", "E", "M", "M", "E", "E", "E", "E"]] click = [0, 0] Array.print2DArray(b) r = Solution().updateBoard(board=b, click=click) Array.print2DArray(b) print() Array.print2DArray([["B", "B", "B", "B", "B", "B", "1", "E"], ["B", "1", "1", "1", "B", "B", "1", "M"], ["1", "2", "M", "1", "B", "B", "1", "1"], ["M", "2", "1", "1", "B", "B", "B", "B"], ["1", "1", "B", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B", "B", "B", "B"], ["B", "1", "2", "2", "1", "B", "B", "B"], ["B", "1", "M", "M", "1", "B", "B", "B"]])
for i in range(h): for j in range(w): cnt = self.numberOfLiveCells(i, j, w, h, board) if board[i][j] == 0: # dead if cnt == 3: next_state[i][j] = 1 else: # live: if cnt == 2 or cnt == 3: next_state[i][j] = 1 for i in range(h): for j in range(w): board[i][j] = next_state[i][j] def numberOfLiveCells(self, i, j, w, h, board): start_i = i - 1 start_j = j - 1 count = 0 for i_next in range(start_i, start_i + 3): for j_next in range(start_j, start_j + 3): if 0 <= i_next < h and 0 <= j_next < w and board[i_next][ j_next] == 1 and (i_next, j_next) != (i, j): count += 1 return count board = [[0, 1, 0], [0, 0, 1], [1, 1, 1], [0, 0, 0]] array.print2DArray(board) print() s = Solution().gameOfLife(board) array.print2DArray(board)
return ans def render_line(self, words, words_len, target_len): gap_num = len(words) - 1 if gap_num == 0: # 这一行可能只有一个单词 return self.render_last_line(words, target_len) spaces_total = target_len - words_len spaces = [spaces_total // gap_num] * gap_num for i in range(spaces_total % gap_num): spaces[i] += 1 # print(spaces) result = "" for i in range(len(words)): result += words[i] if i < len(spaces): result += ' ' * spaces[i] # print(result) return result def render_last_line(self, words, target_len): result = " ".join(words) extra_spaces = target_len - len(result) result += " " * extra_spaces return result words = ["What", "must", "be", "acknowledgment", "shall", "be"] maxWidth = 16 s = Solution().fullJustify(words, maxWidth) array.print2DArray(s)
for i in range(h): for j in range(w): if land[i][j] == 0: a = [] self.search(land, w, h, i, j, a) heapq.heappush(result, len(a)) return heapq.nsmallest(len(result), result) def search(self, land, w, h, i, j, ans): if i < 0 or i >= h or j < 0 or j >= w: return if land[i][j] != 0: return if land[i][j] == 0: land[i][j] = 1 ans.append(0) self.search(land, w, h, i + 1, j + 1, ans) self.search(land, w, h, i - 1, j - 1, ans) self.search(land, w, h, i + 1, j - 1, ans) self.search(land, w, h, i - 1, j + 1, ans) self.search(land, w, h, i - 1, j, ans) self.search(land, w, h, i + 1, j, ans) self.search(land, w, h, i, j + 1, ans) self.search(land, w, h, i, j - 1, ans) land = [[0, 2, 1, 0], [0, 1, 0, 1], [1, 1, 0, 1], [0, 1, 0, 1]] Array.print2DArray(land) r = Solution().pondSizes(land) print(r)
class Solution: def findCircleNum(self, M: List[List[int]]) -> int: n = len(M) if n == 0: return 0 count = 0 visited = [False for _ in range(n)] for i in range(n): if not visited[i]: # Search self.search(M, visited, i) count += 1 # print(i) # print(visited) return count def search(self, M, visited, i): n = len(M) for j in range(n): if not visited[j] and M[i][j] == 1: # 是朋友 visited[j] = True self.search(M, visited, j) # 找j的朋友 M = [[1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0], [0, 0, 1, 1, 0, 0], [0, 0, 1, 0, 1, 0], [0, 0, 0, 0, 0, 1]] Array.print2DArray(M) r = Solution().findCircleNum(M) print(r)