From 9adbbe13c4585e63ee00bad6e4a392518555b1d9 Mon Sep 17 00:00:00 2001 From: Tanya Sreenagesh Date: Sun, 19 Oct 2025 14:26:17 -0700 Subject: [PATCH] submission --- FindDisappearedNumbers.py | 27 +++++++++++++++++ FindMaxMinWithMinComparisons.py | 44 +++++++++++++++++++++++++++ GameOfLife.py | 53 +++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 FindDisappearedNumbers.py create mode 100644 FindMaxMinWithMinComparisons.py create mode 100644 GameOfLife.py diff --git a/FindDisappearedNumbers.py b/FindDisappearedNumbers.py new file mode 100644 index 00000000..83c07693 --- /dev/null +++ b/FindDisappearedNumbers.py @@ -0,0 +1,27 @@ +# Time Complexity : O(n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +''' +Mark seen indices by negating values. +Return indices with positive values as missing numbers. +''' +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + missing = [] + + # Set idx to negative if we have seen element (idx+1) + for i in range(len(nums)): + idx = abs(nums[i])-1 + if nums[idx] > 0: + nums[idx] *= -1 + + # Check which indices are still positive = missing elements + for i in range(len(nums)): + if nums[i] > 0: + missing.append(i+1) + else: + nums[i] *= -1 + + return missing diff --git a/FindMaxMinWithMinComparisons.py b/FindMaxMinWithMinComparisons.py new file mode 100644 index 00000000..ba7ff8cc --- /dev/null +++ b/FindMaxMinWithMinComparisons.py @@ -0,0 +1,44 @@ +from typing import List +import math + +# Time Complexity : O(n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +''' +Problem: +Given an array of numbers of length N, find both the minimum and maximum. +Follow up : Can you do it using less than 2 * (N - 2) comparisons? +''' + +''' +Iterate in pairs instead of going 1 by 1. +''' +def findMaxMin(nums: List[int]) -> (int, int): + i = 0 + maxVal = float('-inf') + minVal = float('inf') + + # Check if next element is available + while i+1 < len(nums): + maxVal = max(maxVal, max(nums[i], nums[i+1])) + minVal = min(minVal, min(nums[i], nums[i+1])) + + i+=2 + + if i < len(nums): + maxVal = max(maxVal, nums[i]) + minVal = min(minVal, nums[i]) + + return minVal, maxVal + +# Test +a1 = [3,1,5,2,8,9,4,3,23] +a2 [1] +a3 = [] +a4 = [3,3,3] +print(a1, findMaxMin(a1)) +print(a2, findMaxMin(a2)) +print(a3, findMaxMin(a3)) +print(a4, findMaxMin(a4)) \ No newline at end of file diff --git a/GameOfLife.py b/GameOfLife.py new file mode 100644 index 00000000..0e2c866a --- /dev/null +++ b/GameOfLife.py @@ -0,0 +1,53 @@ +# Time Complexity : O(m*n) where m is number of rows and n is number of colums in the board. +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +''' +Iterate over the board, keeping flags for when element goes from live to dead OR dead to live. +Iterate again, setting it back to current state: dead 0 or alive 1. +''' +class Solution: + def gameOfLife(self, board: List[List[int]]) -> None: + """ + Do not return anything, modify board in-place instead. + """ + for i in range(len(board)): + for j in range(len(board[0])): + liveCount = self.countLiveNeighbors(board, i, j) + print(1, j, liveCount) + + if board[i][j] == 1 or board[i][j] == 2: + if liveCount < 2 or liveCount > 3: + board[i][j] = 2 + + if board[i][j] == 0 or board[i][j] == 3: + if liveCount == 3: + board[i][j] = 3 + + for i in range(len(board)): + for j in range(len(board[0])): + if board[i][j] == 2: + board[i][j] = 0 + if board[i][j] == 3: + board[i][j] = 1 + + def countLiveNeighbors(self, board: List[List[int]], i: int, j: int) -> int: + # top, t-right, right, b-right, bottom, b-left, left, t-left + dirs = [(-1,0), (-1,1), (0,1), (1,1), (1,0), (1,-1), (0,-1), (-1,-1)] + + m = len(board) + n = len(board[0]) + + live = 0 + + for offset in dirs: + n_i = offset[0]+i + n_j = offset[1]+j + if n_i < m and n_i >= 0 and n_j < n and n_j >= 0: + if board[n_i][n_j] == 1 or board[n_i][n_j] == 2: + live += 1 + + return live + + \ No newline at end of file