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
33 changes: 33 additions & 0 deletions find-disapprearing-numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Loop through nums. If the element is >0 go to the elelment-1 th index and make the
# element there negative if it is postive . Else if the element<0, go to the index of the
# abs(element) and make the element negative if its positive.

# Loop through the nums array array and check if any element is > 0, if so append it to an
# array and return the array

# Time complexity: O(n)
# Space complexity: O(1)

class Solution(object):
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""

for i in nums:
if i>0:
nums[i-1]= -1*nums[i-1] if nums[i-1]>0 else nums[i-1]
elif i<0:
r=(-1*i)-1
nums[r]=-1*nums[r] if nums[r]>0 else nums[r]
arr=[]
for ind, i in enumerate(nums):
if i >0:
arr.append(ind+1)
return arr





63 changes: 63 additions & 0 deletions game-of-life.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Loop through each element in the array and check if it is alive:
# Iterate through all its 8 neigbours to count the number of dead and live neighbpurs:
# If the element ==1 and live<2, change the cell to 2
# If the element ==1 and live> 3, change the cell to 2
# If the element ==0 and live==3, change the cell to 3

# In the end, loop through all the elements and change the cells with 2 to 0 and 3 to 1
# and return the matrix
# Time complexity:O(n)
# Space complexity:(1)


class Solution(object):
def isAlive(self,r, c, board):
current_state=board[r][c]
dir = [[-1,0],[1,0],[0,1],[0,-1],[-1,1],[1,1],[1,-1],[-1,-1]]
live=0
dead=0
for i in dir:
nr = r+i[0]
nc= c+i[1]
if 0<=nr<len(board) and 0<=nc<len(board[0]):
if board[nr][nc] in [1,2]:
live+=1
else:
dead+=1
print("live", live,"dead",dead)
if current_state==1 and live<2:
return 2
elif current_state==1 and live in [2,3]:
return 1
elif current_state==1 and live>3:
return 2
elif current_state==0 and live==3:
return 3
return current_state




def gameOfLife(self, board):
"""
:type board: List[List[int]]
:rtype: None Do not return anything, modify board in-place instead.
"""
r=len(board)
c=len(board[0])

for i in range(r):
for j in range(c):
x=self.isAlive(i,j,board)
board[i][j]= x
print(board)
for i in range(r):
for j in range(c):
if board[i][j]==2:
board[i][j]=0
elif board[i][j] == 3:
board[i][j] =1