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
26 changes: 26 additions & 0 deletions find-all-numbers-disappeared-in-array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
''' Time Complexity : O(n)
Space Complexity : O(1) ;
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No



Approach : Inplace marking method : traverse the array and mark the indexes negative for each element
'''

class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
result = []
n = len(nums)
for i in range(0,n):
idx = abs(nums[i]) - 1
if nums[idx] > 0:
nums[idx] *= -1
print(nums)
for i in range(n):
if nums[i] < 0:
nums[i] *= -1
else:
result.append(i+1)
return result

36 changes: 36 additions & 0 deletions game-of-life.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
''' Time Complexity : O(m * n)
Space Complexity : O(1) ;
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Approach : Temporary mark hthe change 1 -> 0 : 2 and 0 -> 1 : 3
'''

class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
def getLiveCount(board, i , j):
#
count = 0
dirs = [(i,j+1),(i,j-1),(i+1,j),(i-1,j),(i -1,j+1),(i+1,j+1),(i+1,j-1),(i-1,j-1)]
for r, c in dirs:
if (0<=r<rows and 0<=c<cols and (board[r][c] == 1 or board[r][c] == 2)):
count += 1
return count

rows, cols = len(board), len(board[0])
for i in range(rows):
for j in range(cols):
count = getLiveCount(board, i , j)
if board[i][j] == 0 and count == 3:
board[i][j] = 3
elif (board[i][j] == 1 and (count < 2 or count > 3)):
board[i][j] = 2
for i in range(rows):
for j in range(cols):
if board[i][j] == 2:
board[i][j] = 0
if board[i][j] == 3:
board[i][j] = 1