From 397200277c740ecf44a930cdd41bf421df96199f Mon Sep 17 00:00:00 2001 From: Vansh Virani <124015720+vvirani1908@users.noreply.github.com> Date: Tue, 7 Oct 2025 16:49:55 -0500 Subject: [PATCH] Done --- Problem1.py | 27 ++++++++++++++++++++++++ Problem2.py | 34 ++++++++++++++++++++++++++++++ Problem3.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 Problem1.py create mode 100644 Problem2.py create mode 100644 Problem3.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..41470076 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,27 @@ +# Time Complexity : O(n) # We traverse array twice +# Space Complexity : O(1) # Ignoring the result array, we only use variables in-place +# Did this code successfully run on Leetcode : Yes +# Approach : +# 1. Use the values as indices: mark the index (num-1) as negative to say "this number exists". +# 2. After marking, indices that remain positive were never visited. +# 3. Those indices + 1 are the missing numbers. + +from typing import List + +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + n = len(nums) + + # First pass: mark numbers as seen by making their index negative + for i in range(n): + idx = abs(nums[i]) - 1 # map number to index (1 → 0, 2 → 1, ... n → n-1) + if nums[idx] > 0: # only flip if it's not already negative + nums[idx] *= -1 # mark index as visited + + # Second pass: collect missing numbers + result = [] + for i in range(n): + if nums[i] > 0: # if still positive → index never marked → number missing + result.append(i + 1) # convert back to 1-based number + + return result diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..3f43a49c --- /dev/null +++ b/Problem2.py @@ -0,0 +1,34 @@ +# Time Complexity : O(n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Approach : +# 1. Compare elements in pairs, reducing comparisons by half. +# 2. First compare the two elements to decide smaller/larger. +# 3. Update global min and max accordingly. + +def getMinMax(arr): + n = len(arr) + + # If array has even number of elements + if n % 2 == 0: + if arr[0] < arr[1]: + mn, mx = arr[0], arr[1] + else: + mn, mx = arr[1], arr[0] + i = 2 + else: + # If odd, initialize with first element + mn = mx = arr[0] + i = 1 + + # Process pairs + while i < n - 1: + if arr[i] < arr[i+1]: + mn = min(mn, arr[i]) + mx = max(mx, arr[i+1]) + else: + mn = min(mn, arr[i+1]) + mx = max(mx, arr[i]) + i += 2 + + return mn, mx diff --git a/Problem3.py b/Problem3.py new file mode 100644 index 00000000..87e9d3b7 --- /dev/null +++ b/Problem3.py @@ -0,0 +1,60 @@ +# Time Complexity : O(m * n) +# Space Complexity : O(1) # done in-place, no extra grid used +# Did this code successfully run on LeetCode : Yes +# Any problem you faced while coding this : +# Needed to carefully handle transitional states (1->0 and 0->1) + + +class Solution: + def gameOfLife(self, board: List[List[int]]) -> None: + """ + Do not return anything, modify board in-place instead. + """ + + # All 8 possible directions around a cell + dirs = [(-1, -1), (-1, 0), (-1, 1),(0, -1),(0, 1),(1, -1), (1, 0), (1, 1)] + + # m = number of rows, n = number of columns + m, n = len(board), len(board[0]) + + + # Helper function to count live neighbors for cell (i, j) + def getCount(i, j): + count = 0 + for dx, dy in dirs: + r, c = i + dx, j + dy + # Check if neighbor is within grid boundaries + if 0 <= r < m and 0 <= c < n: + # Count cells that are currently alive (1) + # or were alive but are marked to die (2) + if board[r][c] == 1 or board[r][c] == 2: + count += 1 + return count + + # First pass: determine next state for each cell + # Encode transitions without disturbing neighbors: + # 1 -> 0 becomes 2 (was live, will die) + # 0 -> 1 becomes 3 (was dead, will become live) + + for i in range(m): + for j in range(n): + cnt = getCount(i, j) + + # Rule 4: Dead cell with exactly 3 live neighbors -> becomes live + if board[i][j] == 0 and cnt == 3: + board[i][j] = 3 + # Rule 1 & 3: Live cell with <2 or >3 live neighbors -> dies + elif board[i][j] == 1 and (cnt < 2 or cnt > 3): + board[i][j] = 2 + + + # Second pass: finalize the board by decoding temporary states + + for i in range(m): + for j in range(n): + # 3 means dead -> live, set to 1 + if board[i][j] == 3: + board[i][j] = 1 + # 2 means live -> dead, set to 0 + elif board[i][j] == 2: + board[i][j] = 0