classSolution: defcontainsPattern(self, arr: List[int], m: int, k: int) -> bool: print('ohhh') n = len(arr) if m * k > n: returnFalse for i in range(n - m + 1): a = arr[i : i + m] c = 1 j = i + m while j <= n - m: kk = 0 while kk < m and a[kk] == arr[j + kk]: kk += 1 if kk == m: c += 1 else: break j += kk # print(str(i) + " " + str(c) + "-----") if c >= k: returnTrue returnFalse
classSolution: defgetMaxLen(self, nums: List[int]) -> int: l0, l1, l_1 = -1, 0, -1 flag = False res = 0 for i in range(len(nums)): n = nums[i] if n == 0: l0 = i l1 = i + 1 l_1 = -1 flag = False else: if n < 0: flag = not flag if l_1 == -1: l_1 = i if flag and l_1 != -1: res = max(res, i - l_1) elifnot flag: res = max(res, i - l1 + 1) # print(res, end=' ') # print('----') return res
classSolution: defminDays(self, grid: List[List[int]]) -> int: n, m = len(grid), len(grid[0]) cnt1 = 0 vis = [[False] * m for _ in range(n)] defdfs(g, v, x, y): if v[x][y]: returnTrue v[x][y] = True if x > 0and g[x - 1][y] == 1andnot v[x - 1][y]: dfs(g, v, x - 1, y) if y > 0and g[x][y - 1] == 1andnot v[x][y - 1]: dfs(g, v, x, y - 1) if x < len(g) - 1and g[x + 1][y] == 1andnot v[x + 1][y]: dfs(g, v, x + 1, y) if y < len(g[0]) - 1and g[x][y + 1] == 1andnot v[x][y + 1]: dfs(g, v, x, y + 1) flag = False for i in range(n): for j in range(m): if grid[i][j] == 1: cnt1 += 1 if grid[i][j] == 1andnot vis[i][j]: if flag: return0 flag = True dfs(grid, vis, i, j) if cnt1 == 2: return2 for i in range(n): for j in range(m): if grid[i][j] == 0: continue c = [] if i > 0and grid[i - 1][j] == 1: c.append((i - 1, j)) if j > 0and grid[i][j - 1] == 1: c.append((i, j - 1)) if i < n - 1and grid[i + 1][j] == 1: c.append((i + 1, j)) if j < m - 1and grid[i][j + 1] == 1: c.append((i, j + 1)) if len(c) == 1: return1 if len(c) != 2: continue grid[i][j] = 0 for ii in range(n): for jj in range(m): vis[ii][jj] = False dfs(grid, vis, c[0][0], c[0][1]) ifnot vis[c[1][0]][c[1][1]]: return1 grid[i][j] = 1 return2