From 372ec8ed34eecf4583469544d7932dd8708fe9f8 Mon Sep 17 00:00:00 2001 From: Shinjanee Gupta Date: Thu, 19 Feb 2026 18:26:21 -0800 Subject: [PATCH] Complete Array-2 --- DisappearedNumbers.py | 23 +++++++++++++++++++++++ GameOfLife.py | 41 +++++++++++++++++++++++++++++++++++++++++ MinAndMax.py | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 DisappearedNumbers.py create mode 100644 GameOfLife.py create mode 100644 MinAndMax.py diff --git a/DisappearedNumbers.py b/DisappearedNumbers.py new file mode 100644 index 00000000..44424b25 --- /dev/null +++ b/DisappearedNumbers.py @@ -0,0 +1,23 @@ +# Time Complexity : O(n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach : Mark corresponding index of each number negative. +# If any index remains positive then that number is missing from array. + +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + for i, num in enumerate(nums): + idx = abs(num) - 1 + + if nums[idx] > 0: + nums[idx] *= -1 + + res = [] + + for i, num in enumerate(nums): + if num > 0: + res.append(i+1) + + return res + \ No newline at end of file diff --git a/GameOfLife.py b/GameOfLife.py new file mode 100644 index 00000000..e8e6c581 --- /dev/null +++ b/GameOfLife.py @@ -0,0 +1,41 @@ +# 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 +# Approach : At each cell, count number os live neighbors. +# Mark transitions with values: 2 (alive -> dead) and 3 (dead -> alive). +# in the final result, convert 2 -> 0 and 3 -> 1. + + +class Solution: + def gameOfLife(self, board: List[List[int]]) -> None: + """ + Do not return anything, modify board in-place instead. + """ + dirs = [(-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)] + rows, cols = len(board), len(board[0]) + + def getCount(i, j): + count = 0 + for dx, dy in dirs: + r, c = i + dx, j + dy + if 0 <= r < rows and 0 <= c < cols: + if board[r][c] == 1 or board[r][c] == 2: + count += 1 + return count + + for i in range(rows): + for j in range(cols): + count = getCount(i, j) + if board[i][j] == 0 and count == 3: + board[i][j] = 3 + elif board[i][j] == 1 and (count < 2 or count > 3): + board[i][j] = 2 + + for i in range(rows): + for j in range(cols): + if board[i][j] == 2: + board[i][j] = 0 + elif board[i][j] == 3: + board[i][j] = 1 + \ No newline at end of file diff --git a/MinAndMax.py b/MinAndMax.py new file mode 100644 index 00000000..3c1375ef --- /dev/null +++ b/MinAndMax.py @@ -0,0 +1,36 @@ +# Time Complexity : O(n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach : Scan the array in pairs. + + +class Solution: + def getMinMax(self, nums): + # code here + n = len(nums) + i = 0 + + if n % 2 == 0: + if nums[0] < nums[1]: + min_val = nums[0] + max_val = nums[1] + else: + min_val = nums[1] + max_val = nums[0] + i = 2 + else: + min_val = max_val = nums[0] + i = 1 + + + while i < n - 1: + if nums[i] < nums[i + 1]: + min_val = min(min_val, nums[i]) + max_val = max(max_val, nums[i + 1]) + else: + min_val = min(min_val, nums[i + 1]) + max_val = max(max_val, nums[i]) + i += 2 + + return [min_val, max_val]