From 5088516f8ddee1f606a1a41e9f8617ed9a313d79 Mon Sep 17 00:00:00 2001 From: Tanmay Nalawade Date: Mon, 27 Oct 2025 19:10:15 -0700 Subject: [PATCH 1/2] 289 complete --- 289. Game of Life.py | 54 +++++++++++++ ...ind All Numbers Disappeared in an Array.py | 75 +++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 289. Game of Life.py create mode 100644 448. Find All Numbers Disappeared in an Array.py diff --git a/289. Game of Life.py b/289. Game of Life.py new file mode 100644 index 00000000..06704daa --- /dev/null +++ b/289. Game of Life.py @@ -0,0 +1,54 @@ +# Time: O(m*n) +# Space: O(1) +# Description => According to the given conditions update either 1 to 0 or 0 to 1 +# Have a seperate function to count the ones around a particular element +# Then in the first pass on the matrix make the changes but don't directy change it to either 0 or 1 because we want to compare everything with the earlier state of the matrix +# So instead change it to 4 if it's turned from 1 to 0 and 5 if it's 0 to 1 (Remember to count the 4's too in the counter function) +# In the next pass change 4 and 5 to 0 nd 1 respectively + +class Solution: + def gameOfLife(self, board: List[List[int]]) -> None: + """ + Do not return anything, modify board in-place instead. + """ + m = len(board) + n = len(board[0]) + + for i in range(m): + for j in range(n): + count_alives = self.count_alive(board,i,j) + if board[i][j] == 1: + if count_alives < 2: + board[i][j] = 4 + elif count_alives > 3: + board[i][j] = 4 + elif board[i][j] == 0: + if count_alives == 3: + board[i][j] = 5 + + for i in range(m): + for j in range(n): + if board[i][j] == 4: + board[i][j] = 0 + elif board[i][j] == 5: + board[i][j] = 1 + + def count_alive(self,board,i,j): + directions = [ + (0, 1), # Up + (0, -1), # Down + (1, 0), # Right + (-1, 0), # Left + (1, 1), # Up-Right + (1, -1), # Down-Right + (-1, 1), # Up-Left + (-1, -1) # Down-Left + ] + count = 0 + for direc in directions: + new_row = i + direc[0] + new_col = j + direc[1] + if new_row >= 0 and new_col >= 0 and new_row < len(board) and new_col < len(board[0]): + if board[new_row][new_col] == 1 or board[new_row][new_col] == 4: + count += 1 + return count \ No newline at end of file diff --git a/448. Find All Numbers Disappeared in an Array.py b/448. Find All Numbers Disappeared in an Array.py new file mode 100644 index 00000000..118c3991 --- /dev/null +++ b/448. Find All Numbers Disappeared in an Array.py @@ -0,0 +1,75 @@ +# Time: O(n^2) +# Space: O(1) +# Using nested loops check if the number is there +# have a i that loops from 1 to the length of nums + 1 so that every number is covered +# Then looping through the elements in nums check if i is present +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + result = [] + for i in range(1,len(nums)+1): + is_there = False + for num in nums: + if i == num: + is_there = True + + if not is_there: + result.append(i) + return result + + +# Time: O(nlogn) +# Space: O(1) +# First sort the array then loop from 1 to len of the array and if the curr element is greater than the prev element+1 then add all the elements missing +# Consider the first and last element edge cases +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + my_arr = sorted(nums) + result = [] + if my_arr[0] > 1: + for x in range(1, my_arr[0]): + result.append(x) + for i in range(1,len(my_arr)): + if my_arr[i] == my_arr[i-1]: + continue + if my_arr[i] > my_arr[i-1]+1: + for j in range(my_arr[i-1] + 1,my_arr[i]): + result.append(j) + if my_arr[-1] < len(my_arr): + for x in range(my_arr[-1] + 1, len(my_arr) + 1): + result.append(x) + return result + +# Time: O(n) +# Space: O(n) +# First add every element in the hashset +# Then loop through i from 1 to number of elements and find if that i is there in hashset if not append to result +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + my_set = set() + result = [] + + for num in nums: + my_set.add(num) + for i in range(1,len(nums)+1): + if i not in my_set: + result.append(i) + return result + +# Time: O(n) +# Space: O(1) +# TEMPORARY STATE CHANGE +# First while looping through the numbers make the indexes associated with the number negative if not already negative +# Then in a second pass make all the negative elements positive and return the numbers that are associated with positive indexes +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + for num in nums: + idx = abs(num) + if nums[idx-1] > 0: + nums[idx-1] *= -1 + result = [] + for i in range(len(nums)): + if nums[i] < 0: + nums[i] *= -1 + else: + result.append(i+1) + return result \ No newline at end of file From b8e673f088a70af0a801ab8d65f1e76fbd1754b1 Mon Sep 17 00:00:00 2001 From: Tanmay Nalawade Date: Tue, 28 Oct 2025 11:53:42 -0700 Subject: [PATCH 2/2] min max in minimum comparisons --- ...nd Min using minimum no. of comparisons.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Max and Min using minimum no. of comparisons.py diff --git a/Max and Min using minimum no. of comparisons.py b/Max and Min using minimum no. of comparisons.py new file mode 100644 index 00000000..28efca13 --- /dev/null +++ b/Max and Min using minimum no. of comparisons.py @@ -0,0 +1,23 @@ +# In brute force we make 2 comparisons per number i.e. if it's max or min +# When we think of the numbers in pair by checking if nums[i] < nums[i+1] or the other way. +# We are making 3 comparisons for 2 elements instead of 4 with 2 min and max comparisons and 1 number comparison in if statement +# Comparisons: 3n/2 +class Solution: + def findMinMax(self, nums: list[int]) -> list[int]: + mini = float('inf') + maxi = float('-inf') + i = 0 + while i < len(nums) - 1: + if nums[i] < nums[i+1]: + mini = min(mini,nums[i]) + maxi = max(maxi,nums[i+1]) + else: + mini = min(mini,nums[i + 1]) + maxi = max(maxi,nums[i]) + i += 2 + + if len(nums) % 2 != 0: + mini = min(nums[-1],mini) + maxi = max(nums[-1],maxi) + + return [mini, maxi] \ No newline at end of file