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
38 changes: 38 additions & 0 deletions find-all-numbers-disappeared-in-an-array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution:
'''
TC: O(2n) = O(n)
SC: O(1)
Leetcode: Compeleted the problem
Issue: no issue
'''
def findDisappearedNumbers(self, nums):
n = len(nums)
for i in range(n):
# Convert to abs to get the actual element
idx = abs(nums[i]) - 1

if nums[idx] > 0:
# convert the element to neg to mark it as available.
nums[idx] = - nums[idx]

res = []
# Get the result
for i in range(n):
if nums[i] > 0:
# + 1 for getting index for 0 based list.
res.append(i + 1)

return res

def findDisappearedNumbers(self, nums):
n = len(nums)
id = nums[0]




s = Solution()
nums = [4,3,2,7,8,2,3,1]
print(s.findDisappearedNumbers(nums))
nums = [1,1]
print(s.findDisappearedNumbers(nums))
50 changes: 50 additions & 0 deletions game-of-life.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Solution:
'''
TC: O(n^2)
SC: O(1)
Leetcode: Completed with no issue
issueAny problem you faced while coding this : no issue.
'''
def countAlive(self, board, i, j, n, m):
count = 0
dirs = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
for x, y in dirs:
rx = x + i
ry = y + j
if 0 <= rx < n and 0 <= ry < m:
if board[rx][ry] in [1, 2]:
count += 1

return count

def gameOfLife(self, board):
"""
Do not return anything, modify board in-place instead.
"""
n = len(board)
m = len(board[0])

for i in range(n):
for j in range(m):
countAlive = self.countAlive(board, i, j, n, m)
if board[i][j] == 1:
if countAlive < 2 or countAlive > 3:
board[i][j] = 2
else:
if countAlive == 3:
board[i][j] = 3

for i in range(n):
for j in range(m):
if board[i][j] == 2:
board[i][j] = 0
if board[i][j] == 3:
board[i][j] = 1

s = Solution()
board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
s.gameOfLife(board)
print(board)
board = [[1,1],[1,0]]
s.gameOfLife(board)
print(board)