From 11db0a6ff3488581a753bcfea35c5a2bac23c61f Mon Sep 17 00:00:00 2001 From: PrasiddhShah Date: Wed, 15 Oct 2025 21:59:30 -0700 Subject: [PATCH] Array - 2 Complete --- program1.py | 41 +++++++++++++++++++++++++++++++++++++++++ program2.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 program1.py create mode 100644 program2.py diff --git a/program1.py b/program1.py new file mode 100644 index 00000000..70948f2e --- /dev/null +++ b/program1.py @@ -0,0 +1,41 @@ +""" + +Time Complexity : o(2N) +Space Complexity :O(N) +Did this code successfully run on Leetcode :yes +Any problem you faced while coding this :no + + +Approach +Here we are using indexs and temp array mutation to get the result +because of the mutation we are keeping the space complexity same + +basic idea is that we keep track of all the number that we have seen in the array but, +converting the number at the index of the number found +as negative after we are do with one pass + +on the second pass we just see if a number is negative of not if now then that index or nunmber is missing + +""" + + +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + + result = list() + + 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) + for i in range(len(nums)): + if nums[i] < 0: + nums[i] *= -1 + return result + + + \ No newline at end of file diff --git a/program2.py b/program2.py new file mode 100644 index 00000000..f5974f0a --- /dev/null +++ b/program2.py @@ -0,0 +1,49 @@ +""" + +Time Complexity : o(2nm) +Space Complexity :O(N) +Did this code successfully run on Leetcode :yes +Any problem you faced while coding this :no + + +Approach +we are travesing all the numbers one by one and checking for the rules +and according to that we are changing the value, we are comparing all the 8 possible values + +and to do the change in place we are changing value to 2 and 3 as temp so was to keep track of all the value before and after mutation + +""" + + + + +class Solution: + def countalive(self, board: List[List[int]], r: int, c: int) -> int: + m, n = len(board), len(board[0]) + dirs = [(0,1),(0,-1),(-1,0),(1,0),(-1,1),(-1,-1),(1,1),(1,-1)] + count = 0 + for dr, dc in dirs: + nr, nc = r + dr, c + dc + if 0 <= nr < m and 0 <= nc < n and (board[nr][nc] == 1 or board[nr][nc] == 2): + count += 1 + return count + + def gameOfLife(self, board: List[List[int]]) -> None: + m, n = len(board), len(board[0]) + + # 1st pass: mark transitions + for i in range(m): + for j in range(n): + alive_neighbors = self.countalive(board, i, j) + if board[i][j] == 1 and (alive_neighbors < 2 or alive_neighbors > 3): + board[i][j] = 2 + elif board[i][j] == 0 and alive_neighbors == 3: + board[i][j] = 3 + + # 2nd pass: finalize + 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