diff --git a/find-all-numbers-disappeared-in-array.py b/find-all-numbers-disappeared-in-array.py new file mode 100644 index 00000000..f17d4f25 --- /dev/null +++ b/find-all-numbers-disappeared-in-array.py @@ -0,0 +1,26 @@ +''' Time Complexity : O(n) + Space Complexity : O(1) ; + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No + + + + Approach : Inplace marking method : traverse the array and mark the indexes negative for each element +''' + +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + result = [] + n = len(nums) + for i in range(0,n): + idx = abs(nums[i]) - 1 + if nums[idx] > 0: + nums[idx] *= -1 + print(nums) + for i in range(n): + if nums[i] < 0: + nums[i] *= -1 + else: + result.append(i+1) + return result + \ No newline at end of file diff --git a/game-of-life.py b/game-of-life.py new file mode 100644 index 00000000..c65bee36 --- /dev/null +++ b/game-of-life.py @@ -0,0 +1,36 @@ +''' Time Complexity : O(m * n) + Space Complexity : O(1) ; + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No + + Approach : Temporary mark hthe change 1 -> 0 : 2 and 0 -> 1 : 3 +''' + +class Solution: + def gameOfLife(self, board: List[List[int]]) -> None: + """ + Do not return anything, modify board in-place instead. + """ + def getLiveCount(board, i , j): + # + count = 0 + dirs = [(i,j+1),(i,j-1),(i+1,j),(i-1,j),(i -1,j+1),(i+1,j+1),(i+1,j-1),(i-1,j-1)] + for r, c in dirs: + if (0<=r 3)): + board[i][j] = 2 + for i in range(rows): + for j in range(cols): + if board[i][j] == 2: + board[i][j] = 0 + if board[i][j] == 3: + board[i][j] = 1 \ No newline at end of file