Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions disappeared_no.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def findDisappearedNumbers(self, nums):
n = len(nums)

for i in range(n):
idx = abs(nums[i]) - 1
if nums[idx] > 0:
nums[idx] *= -1

result = []
for i in range(n):
if nums[i] > 0:
result.append(i + 1)

return result

# Time: O(n)
# Space: O(h)
30 changes: 30 additions & 0 deletions game_of_life.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution:
def gameOfLife(self, board):
dirs = [(-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)]
m, n = len(board), len(board[0])

def getCount(i, j):
count = 0
for dx, dy in dirs:
r, c = i + dx, j + dy
if 0 <= r < m and 0 <= c < n:
if board[r][c] == 1 or board[r][c] == 2:
count += 1
return count

for i in range(m):
for j in range(n):
cnt = getCount(i, j)
if board[i][j] == 0 and cnt == 3:
board[i][j] = 3
elif board[i][j] == 1 and (cnt < 2 or cnt > 3):
board[i][j] = 2

for i in range(m):
for j in range(n):
if board[i][j] == 2:
board[i][j] = 0
elif board[i][j] == 3:
board[i][j] = 1
# Time: O(m + n)
# Space: O(1)