From 70362cc30b276582e7b7d9d861dac226158daea7 Mon Sep 17 00:00:00 2001 From: Bharath Vuppala Date: Wed, 15 Jan 2025 00:10:19 -0800 Subject: [PATCH 1/2] 2 problems completed --- Find all numbers disappeared in Array.py | 18 ++++++++++ Game of Life.py | 45 ++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 Find all numbers disappeared in Array.py create mode 100644 Game of Life.py diff --git a/Find all numbers disappeared in Array.py b/Find all numbers disappeared in Array.py new file mode 100644 index 00000000..d0f227c6 --- /dev/null +++ b/Find all numbers disappeared in Array.py @@ -0,0 +1,18 @@ +#Time complexity - 0(n) & space complexity - o(1) +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + if nums == None or len(nums)==0: + return [] + result = [] + + for i in range (len(nums)): + index = abs(nums[i])-1 + if nums[index]>0: + nums[index] = nums[index]*-1 + + for i in range (len(nums)): + if nums[i]>0: + result.append(i+1) + else: + nums[i] = nums[i]*-1 + return result \ No newline at end of file diff --git a/Game of Life.py b/Game of Life.py new file mode 100644 index 00000000..520ef7c4 --- /dev/null +++ b/Game of Life.py @@ -0,0 +1,45 @@ +#Time complexity - 0(m*n) & space complexity - o(1) +class Solution: + def gameOfLife(self, board: List[List[int]]) -> None: + + m= len(board) + n= len(board[0]) + + #1 - 0 - 2 + #0- 1 -3 + + for i in range(m): + for j in range (n): + countn= self.liveNeighbourCount(board, i, j) + if board[i][j]==1: + if countn < 2 or countn>3: + board[i][j]=2 + else: + if countn ==3: + board[i][j]=3 + for i in range(m): + for j in range (n): + if board[i][j]==2: + board[i][j]=0 + elif board[i][j]==3: + board[i][j]=1 + + # we need to define function for counting + def liveNeighbourCount(self, board: List[List[int]], r:int , c:int)-> int: + count =0 + m= len(board) + n= len(board[0]) + dirs=[[-1,-1], [-1,0], [-1,1], [0,1], [1,1], [1,0], [1,-1], [0,-1]] + for dir in dirs: + nr= r+dir[0] + nc= c+ dir[1] + if nr>=0 and nc>=0 and nr Date: Wed, 7 Jan 2026 14:40:23 -0800 Subject: [PATCH 2/2] 2 problems completed --- Find all numbers disappeared in Array.py | 18 +++----- Game of Life.py | 54 +++++++++++------------- 2 files changed, 31 insertions(+), 41 deletions(-) diff --git a/Find all numbers disappeared in Array.py b/Find all numbers disappeared in Array.py index d0f227c6..05b72bca 100644 --- a/Find all numbers disappeared in Array.py +++ b/Find all numbers disappeared in Array.py @@ -1,18 +1,14 @@ -#Time complexity - 0(n) & space complexity - o(1) class Solution: def findDisappearedNumbers(self, nums: List[int]) -> List[int]: - if nums == None or len(nums)==0: - return [] - result = [] - - for i in range (len(nums)): - index = abs(nums[i])-1 - if nums[index]>0: - nums[index] = nums[index]*-1 + n=len(nums) + result=[] + for i in range(len(nums)): + if nums[abs(nums[i])-1]>0: + nums[abs(nums[i])-1]=nums[abs(nums[i])-1]*-1 - for i in range (len(nums)): + for i in range(len(nums)): if nums[i]>0: result.append(i+1) else: - nums[i] = nums[i]*-1 + nums[i]=nums[i]*-1 return result \ No newline at end of file diff --git a/Game of Life.py b/Game of Life.py index 520ef7c4..9c7fc8f5 100644 --- a/Game of Life.py +++ b/Game of Life.py @@ -1,45 +1,39 @@ -#Time complexity - 0(m*n) & space complexity - o(1) 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]) - m= len(board) - n= len(board[0]) - - #1 - 0 - 2 - #0- 1 -3 - + #1-0 ->2 + #0-1-> 3 for i in range(m): - for j in range (n): - countn= self.liveNeighbourCount(board, i, j) + for j in range(n): + countn=self.neighbourCount(board, i , j) if board[i][j]==1: - if countn < 2 or countn>3: + if countn<2 or countn>3: board[i][j]=2 + else: - if countn ==3: + if countn==3: board[i][j]=3 for i in range(m): - for j in range (n): + for j in range(n): if board[i][j]==2: board[i][j]=0 elif board[i][j]==3: board[i][j]=1 - - # we need to define function for counting - def liveNeighbourCount(self, board: List[List[int]], r:int , c:int)-> int: - count =0 - m= len(board) - n= len(board[0]) - dirs=[[-1,-1], [-1,0], [-1,1], [0,1], [1,1], [1,0], [1,-1], [0,-1]] - for dir in dirs: - nr= r+dir[0] - nc= c+ dir[1] - if nr>=0 and nc>=0 and nrint: + count=0 + m=len(board) + n=len(board[0]) + dirr=[[-1,-1],[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1],[0,-1]] + for dir in dirr: + nr=r+dir[0] + nc=c+dir[1] + if nr>=0 and nc>=0 and nr