diff --git a/FindAllNumbersDisappearedInAnArray.py b/FindAllNumbersDisappearedInAnArray.py new file mode 100644 index 00000000..4b6d0d37 --- /dev/null +++ b/FindAllNumbersDisappearedInAnArray.py @@ -0,0 +1,29 @@ +''' +To solve this problem in O(n) time and without any extra space, I changed the state of the elements temporarily to negative. +Initially I caluclated the index and iterated through the array to change the state at that index +while iterating again through the state changed array, if the elemet is positive I stored the index+1 value which is the missing elemnt. +If the element is negative changed it to positive. +''' +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + result = [] + n = len(nums) + # finding the index and changing the state + for i in range(n): + index = abs(nums[i]) - 1 + if nums[index] > 0: + nums[index] *= -1 + # adding the missing elements to the array and changing the state back + for i in range(n): + if nums[i] > 0: + result.append(i+1) + else: + nums[i] *= -1 + return result + +''' +Time Complexity: O(n) +Since we iterating on the same array twice, the time complexity would be O(2n) -> O(n) average time complexity +Space Complexity: O(1) +We are not using any extra space, we are just temporarity modifying the same array and changing it back to its original state +''' \ No newline at end of file diff --git a/GameOfLife.py b/GameOfLife.py new file mode 100644 index 00000000..009983cc --- /dev/null +++ b/GameOfLife.py @@ -0,0 +1,45 @@ +''' +In this problem, for the given 4 conditions, using my countAlive() helper function, I checked all the neighbors of each cells interaction. +For every time if the state of the cell changes from alive to dead I represented it with 2 and for dead to alive I represnted it with 3. +Finally after checking the neighbors of each cell, I replaced my represented values with actual values. +''' +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): + countAlives = self.countAlive(board, i, j, m, n) + if board[i][j] == 1 and (countAlives < 2 or countAlives > 3): + # 1 -> 0 => 2 + board[i][j] = 2 + if board[i][j] == 0 and countAlives == 3: + # 0 -> 1 => 3 + board[i][j] = 3 + + for i in range(m): + for j in range(n): + if board[i][j] == 2: + board[i][j] = 0 + if board[i][j] == 3: + board[i][j] = 1 + + def countAlive(self, board, i, j, m, n) -> int: + dirs = [(-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)] + count = 0 + for dx, dy in dirs: + newRow = i + dx + newCol = j + dy + if(newRow >= 0 and newCol >= 0 and newRow < m and newCol < n and (board[newRow][newCol] == 1 or board[newRow][newCol] == 2)): + count += 1 + + return count +''' +Time Complexity: O(m*n) +Here we are iterattig on the board of size m*n twice, so the average time taken m*n +Space Complexity: O(1) +I modified same board without taking any extra space so the space is O(1) +''' \ No newline at end of file