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
29 changes: 29 additions & 0 deletions FindAllNumbersDisappearedInAnArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'''
To solve this problem in O(n) time and without any extra space, I changed the state of the elements temporarily to negative.
Initially I caluclated the index and iterated through the array to change the state at that index
while iterating again through the state changed array, if the elemet is positive I stored the index+1 value which is the missing elemnt.
If the element is negative changed it to positive.
'''
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
result = []
n = len(nums)
# finding the index and changing the state
for i in range(n):
index = abs(nums[i]) - 1
if nums[index] > 0:
nums[index] *= -1
# adding the missing elements to the array and changing the state back
for i in range(n):
if nums[i] > 0:
result.append(i+1)
else:
nums[i] *= -1
return result

'''
Time Complexity: O(n)
Since we iterating on the same array twice, the time complexity would be O(2n) -> O(n) average time complexity
Space Complexity: O(1)
We are not using any extra space, we are just temporarity modifying the same array and changing it back to its original state
'''
45 changes: 45 additions & 0 deletions GameOfLife.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'''
In this problem, for the given 4 conditions, using my countAlive() helper function, I checked all the neighbors of each cells interaction.
For every time if the state of the cell changes from alive to dead I represented it with 2 and for dead to alive I represnted it with 3.
Finally after checking the neighbors of each cell, I replaced my represented values with actual values.
'''
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m = len(board)
n = len(board[0])
for i in range(m):
for j in range(n):
countAlives = self.countAlive(board, i, j, m, n)
if board[i][j] == 1 and (countAlives < 2 or countAlives > 3):
# 1 -> 0 => 2
board[i][j] = 2
if board[i][j] == 0 and countAlives == 3:
# 0 -> 1 => 3
board[i][j] = 3

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

def countAlive(self, board, i, j, m, n) -> int:
dirs = [(-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)]
count = 0
for dx, dy in dirs:
newRow = i + dx
newCol = j + dy
if(newRow >= 0 and newCol >= 0 and newRow < m and newCol < n and (board[newRow][newCol] == 1 or board[newRow][newCol] == 2)):
count += 1

return count
'''
Time Complexity: O(m*n)
Here we are iterattig on the board of size m*n twice, so the average time taken m*n
Space Complexity: O(1)
I modified same board without taking any extra space so the space is O(1)
'''