Skip to content
Open

done #1822

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
25 changes: 25 additions & 0 deletions problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
We check for every index (nums[i] + 1) if the element present at that index is negative,
if its negative, mark it as negative i.e. it is present, and in the second pass, return all elements that remain positive
time is o(n) and space is o(1)
"""

class Solution(object):
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
res = []
n = len(nums)
for i in range(n):
index = abs(nums[i]) - 1 #to traverse through the array at each index and
if nums[index] > 0: #check if number is not negative, make it negative
nums[index] *= -1

for i in range(n): #second pass, check if any elem is positive, return that index + 1 as missing number
if nums[i] > 0:
res.append(i+1)
return res


42 changes: 42 additions & 0 deletions problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
We use the coundary conditions to moodify the pointers and apply the games of rule accordingly, usee the temporary state change method to analyse the board
then count the changes that were made through counting the number of variations we see (2 and 3 for dead and resurrected accordingly)
TC is O(m*n) and SC is o(n)
"""


class Solution(object):
def gameOfLife(self, board):
"""
:type board: List[List[int]]
:rtype: None Do not return anything, modify board in-place instead.
"""
m = len(board)
n = len(board[0])
dirs = [(-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)]
def countAlive(i, j):
count = 0
for dr, dc in dirs:
nr, nc = i + dr, j + dc
if nr >= 0 and nc >= 0 and nr < m and nc < n:
if board[nr][nc] == 1 or board[nr][nc] == 2: #2 for originally alive, but marked to die later on
count += 1
return count

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

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