From 81b9f26f1c5e9a73e5a8f7fe7379338f5e54b127 Mon Sep 17 00:00:00 2001 From: Kattuvila Milkiyas Date: Sat, 6 Dec 2025 18:07:46 -0800 Subject: [PATCH] array-2 completed --- find-disapprearing-numbers.py | 33 ++++++++++++++++++ game-of-life.py | 63 +++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 find-disapprearing-numbers.py create mode 100644 game-of-life.py diff --git a/find-disapprearing-numbers.py b/find-disapprearing-numbers.py new file mode 100644 index 00000000..a0c85583 --- /dev/null +++ b/find-disapprearing-numbers.py @@ -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 + + + + + \ No newline at end of file diff --git a/game-of-life.py b/game-of-life.py new file mode 100644 index 00000000..0031fa5e --- /dev/null +++ b/game-of-life.py @@ -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<=nr3: + 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 + + + +