From d86d3a4766fa24b38a29d48d1330f1d5767abb7c Mon Sep 17 00:00:00 2001 From: Anushri Date: Thu, 27 Nov 2025 14:32:22 -0500 Subject: [PATCH 1/5] bruteforce approach --- game_of_life.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 game_of_life.py diff --git a/game_of_life.py b/game_of_life.py new file mode 100644 index 00000000..459dbeca --- /dev/null +++ b/game_of_life.py @@ -0,0 +1 @@ +# https://leetcode.com/problems/game-of-life/ \ No newline at end of file From d00f4e9a6f00b591dada344993aef9fd53f96555 Mon Sep 17 00:00:00 2001 From: Anushri Date: Thu, 27 Nov 2025 14:33:46 -0500 Subject: [PATCH 2/5] bruteforce approach --- number_disappeared_in_an_array.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 number_disappeared_in_an_array.py diff --git a/number_disappeared_in_an_array.py b/number_disappeared_in_an_array.py new file mode 100644 index 00000000..db411d2a --- /dev/null +++ b/number_disappeared_in_an_array.py @@ -0,0 +1,23 @@ +# https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ + +# brutforce solution tc- O(n2) sc- (1) +# such types of flags only stored temporarily inside the loop iteration. it does not presists outside the loop +class Solution: + def findDisappearedNumbers(self, nums: list[int]) -> list[int]: + # this is an output space and it's not counted in complexity + result = [] + n = len(nums) + # this loop is giving an index so we started from 1 + for i in range(1, n+1): + flag = False + # this loop is checking number with that index + for j in range(n): + if nums[j] == i: + flag = True + break + # default flag = false condition + if not flag: + result.append(i) + return result +solution =Solution() +print(solution.findDisappearedNumbers([4,3,2,7,8,2,3,1])) From 368383c790dc67ca834a7f38b1f4468418f5b153 Mon Sep 17 00:00:00 2001 From: Anushri Date: Thu, 27 Nov 2025 15:09:00 -0500 Subject: [PATCH 3/5] optimised solution --- number_disappeared_in_an_array.py | 36 +++++++++++++++++-------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/number_disappeared_in_an_array.py b/number_disappeared_in_an_array.py index db411d2a..7be75cd2 100644 --- a/number_disappeared_in_an_array.py +++ b/number_disappeared_in_an_array.py @@ -1,23 +1,27 @@ # https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ -# brutforce solution tc- O(n2) sc- (1) -# such types of flags only stored temporarily inside the loop iteration. it does not presists outside the loop +# In this problem as the range started from 1 so index is used to find the missing number. If any index is positive means that number was missing from the array. So to find the missing number we are adding 1 to the index to get the number. +# Time Complexity- O(n) Space Complexity- O(1) + class Solution: def findDisappearedNumbers(self, nums: list[int]) -> list[int]: - # this is an output space and it's not counted in complexity - result = [] n = len(nums) - # this loop is giving an index so we started from 1 - for i in range(1, n+1): - flag = False - # this loop is checking number with that index - for j in range(n): - if nums[j] == i: - flag = True - break - # default flag = false condition - if not flag: - result.append(i) + + for i in range(n): + # index starts from 0, so we are finding the index of the number by subracting 1 because nums is starting from 1. and the absolute condition is to hangle the duplicate numbers + index = abs(nums[i]) - 1 + # if the index is positive make it negative if it's already negative then don't multiply because it will make it positive, where positive stands for missing number + if nums[index] > 0: + nums[index] *= -1 + + result = [] + for i in range(n): + # as we marked the number to negative so missing number is positive checking that condition + if nums[i] > 0: + # if index is found then to get the number add 1 and append into the result set + result.append(i + 1) return result + solution =Solution() -print(solution.findDisappearedNumbers([4,3,2,7,8,2,3,1])) +print(solution.findDisappearedNumbers([4,3,2,7,8,2,3,1])) + From 69a7a68ad59f05dc6da5100a0a6df3be340df329 Mon Sep 17 00:00:00 2001 From: Anushri Date: Thu, 27 Nov 2025 17:06:09 -0500 Subject: [PATCH 4/5] optimised solution --- game_of_life.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/game_of_life.py b/game_of_life.py index 459dbeca..da8cf1f3 100644 --- a/game_of_life.py +++ b/game_of_life.py @@ -1 +1,49 @@ -# https://leetcode.com/problems/game-of-life/ \ No newline at end of file +# https://leetcode.com/problems/game-of-life/ + +# In this program to update the board without using the extra space two temporary notations used '2' for alive and '3' for dead. Checked every cell and count how manylive neighbors it has. Then eventually turn the 2 and 3 into the original form. +# Time Complexity- O(m * n) Space Complexity- (1) + +class Solution: + def gameOfLife(self, board)-> list[int]: + directions = [(-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)] + m, n = len(board), len(board[0]) + + def getaAliveCount(i, j): + count = 0 + # iterating on directions array to find the all eight directions of the current row and column index + for dx, dy in directions: + # to find the new row and column of the neighbor + r, c = i + dx, j + dy + # checking the boundires if the new row formed is greater than zero and less than total rows and new column formed is greater than zero and less than n then only consider it + if 0 <= r < m and 0 <= c < n: + # as checking for only alive counts so give the conditions originally alive and changed to dead which was original alive + if board[r][c] == 1 or board[r][c] == 2: + count += 1 + # once we are done with the all directions return the count + return count + + for i in range(m): + for j in range(n): + # calling the function to get the count of the live cells + cnt = getaAliveCount(i, j) + # if the cell is dead(board[i][j] == 0) then checking the 4th rule which says dead cell with 3 dead neighbors becomes alive + if board[i][j] == 0 and cnt == 3: + # mark it 3 which shows intially it was dead now it's alive + board[i][j] = 3 + # if the cell is live(board[i][j] == 1) then checking the 2 give rules first one is live cell fewer than 2 live neighbors or live cell more than 3 live neighbors becomes dead + elif board[i][j] == 1 and (cnt < 2 or cnt > 3): + # mark it 2 which shows intially it was alive now it's dead + board[i][j] = 2 + + for i in range(m): + for j in range(n): + # change the updated cell in it's original state + if board[i][j] == 2: + board[i][j] = 0 + # change the updated cell in it's original state + elif board[i][j] == 3: + board[i][j] = 1 + return board + +solution = Solution() +print(solution.gameOfLife([[0,1,0],[0,0,1],[1,1,1],[0,0,0]])) \ No newline at end of file From 317609306ea59d4022da87287dbfa2b309d660fd Mon Sep 17 00:00:00 2001 From: Anushri Date: Thu, 27 Nov 2025 17:48:36 -0500 Subject: [PATCH 5/5] optimised solution --- max_and_min.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 max_and_min.py diff --git a/max_and_min.py b/max_and_min.py new file mode 100644 index 00000000..4d3c6abf --- /dev/null +++ b/max_and_min.py @@ -0,0 +1,43 @@ +# Problmep- An array of numbers of length N is given , you need to find the minimum and maximum. try doing this in less than 2* (N-2) comparisons + +# To reduce the comparisions, comapring the pairs of the array. At first, fiund out the min and max from the first pair and comparing that with the min and max element of the next pair. Accordingly incrementing the pointer by 2. + +# Time Complexity- O(n) Space Complexity- O(1) + +class Solution: + def findMinAndMax(self, nums): + n = len(nums) + i = 0 + # check if the nums have even numbers + if n % 2 == 0: + # comapare first and second and find the min and max from them + if nums[0] < nums[1]: + min_val = nums[0] + max_val = nums[1] + else: + min_val = nums[1] + max_val = nums[0] + # change the pointer to 2nd position + i = 2 + # if the nums have odd numbers set first element as min and max + else: + min_val = max_val = nums[0] + i = 1 + # below part reduces the comparision by comparing pairs at a time which is nums[i] and nums[i + 1] + while i < n - 1: + # find out the min and max from the next pair + if nums[i] < nums[i + 1]: + # if the first element is small + min_val = min(min_val, nums[i]) + max_val = max(max_val, nums[i + 1]) + # if the second element is small + else: + min_val = min(min_val, nums[i + 1]) + max_val = max(max_val, nums[i]) + # increment the pointer by 2 for next pair + i += 2 + + return [min_val, max_val] + +solution = Solution() +print(solution.findMinAndMax([3,2,6,5,1,7,9])) \ No newline at end of file