diff --git a/game_of_life.py b/game_of_life.py new file mode 100644 index 00000000..da8cf1f3 --- /dev/null +++ b/game_of_life.py @@ -0,0 +1,49 @@ +# 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 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 diff --git a/number_disappeared_in_an_array.py b/number_disappeared_in_an_array.py new file mode 100644 index 00000000..7be75cd2 --- /dev/null +++ b/number_disappeared_in_an_array.py @@ -0,0 +1,27 @@ +# https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ + +# 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]: + n = len(nums) + + 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])) +