From e026dd34d21df3357af0f26cfd3427e2aa4a5d12 Mon Sep 17 00:00:00 2001 From: ashritha0806 <111675125+ashritha0806@users.noreply.github.com> Date: Thu, 23 Apr 2026 12:47:46 -0700 Subject: [PATCH] Done Array-2 --- Dissapper_number.py | 21 +++++++++++++++++++++ Game_of_life.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 Dissapper_number.py create mode 100644 Game_of_life.py diff --git a/Dissapper_number.py b/Dissapper_number.py new file mode 100644 index 00000000..4dd9db35 --- /dev/null +++ b/Dissapper_number.py @@ -0,0 +1,21 @@ +# Time Complexity : O(N) N:length of array. +# Space complexity :O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : Took sometime to reach O(1) space solution. + +# Your code here along with comments explaining your approach +# Iterate through the array and use each number's value as an index, flipping the number at that index to negative. +# A positive number remaining at an index i means the value i + 1 was never appeared in the original list so return those. + +class Solution: + def findDisappearedNumbers(self, nums): + result = [] + for i in range(len(nums)): + index = abs(nums[i]) - 1 + if nums[index] > 0: + nums[index] *= -1 + + for i in range(len(nums)): + if nums[i] > 0: + result.append(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..7e358044 --- /dev/null +++ b/Game_of_life.py @@ -0,0 +1,45 @@ +# 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 + +# Your code here along with comments explaining your approach +# Instead of immediate flipping, cells transitioning from 0 to 1 are marked as 2, and cells moving from 1 to 0 are marked as 3. +# When counting neighbors, both 1 (current alive) and 3 (was alive, now dying) are treated as active cells to maintain originality. +# A second pass iterates through the grid to convert all 2s back to 1s and 3s back to 0s. + +class Solution(object): + def gameOfLife(self, board): + """ + :type board: List[List[int]] + :rtype: None Do not return anything, modify board in-place instead. + """ + # 0 -> 1 : 2 + # 1 -> 0 : 3 + + m = len(board) + n = len(board[0]) + + for i in range(m): + for j in range(n): + count = self.countAlive(board, i, j,m,n) + if board[i][j] == 1 and (count < 2 or count > 3): + board[i][j] = 3 + elif board[i][j] == 0 and count == 3: + board[i][j] = 2 + + for i in range(m): + for j in range(n): + if board[i][j] == 2: + board[i][j] = 1 + elif board[i][j] == 3: + board[i][j] = 0 + + def countAlive(self, board,i,j,m,n): + count = 0 + direction = [(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1) ] + for dr, dc in direction: + r,c = i + dr, j + dc + if 0 <= r < m and 0 <= c < n and board[r][c] in [1,3]: + count += 1 + return count \ No newline at end of file