diff --git a/find-all-numbers-disappeared.py b/find-all-numbers-disappeared.py new file mode 100644 index 00000000..819c7066 --- /dev/null +++ b/find-all-numbers-disappeared.py @@ -0,0 +1,18 @@ + +# time O(n), space O(1) +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + + for i in range(len(nums)): + idx = abs(nums[i])-1 + if nums[idx] > 0: + nums[idx]*=-1 + + res = [] + for i in range(len(nums)): + if nums[i] < 0: + nums[i]*=-1 + else: + res.append(i+1) + + return res \ No newline at end of file diff --git a/game-of-life.py b/game-of-life.py new file mode 100644 index 00000000..e95398d4 --- /dev/null +++ b/game-of-life.py @@ -0,0 +1,33 @@ + +# time O(n*m), space O(1) + +class Solution: + def gameOfLife(self,board: List[List[int]]): + + for r in range(len(board)): + for c in range(len(board[0])): + cnt = self.countAlive(board, r,c) + if board[r][c] == 1 and (cnt < 2 or cnt > 3): + board[r][c]=2 + elif board[r][c] == 0 and cnt == 3: + board[r][c]=3 + + for r in range(len(board)): + for c in range(len(board[0])): + if board[r][c] == 2: + board[r][c]=0 + elif board[r][c] == 3: + board[r][c]=1 + return board + + + def countAlive(self,board, r,c): + dirs = [(0,1),(0,-1),(-1,0),(1,0),(-1,-1),(-1,1),(1,-1),(1,1)] + cntAlive=0 + for d in dirs: + nr = r+d[0] + nc = c+d[1] + if (nr >= 0 and nc >= 0 and nr < len(board) and nc < len(board[0])): + if board[nr][nc] == 1 or board[nr][nc]==2: + cntAlive+=1 + return cntAlive \ No newline at end of file diff --git a/max-and-min.py b/max-and-min.py new file mode 100644 index 00000000..1ab8eb58 --- /dev/null +++ b/max-and-min.py @@ -0,0 +1,11 @@ +def findMinMax(arr): + mn = float('inf') + mx = float('-inf') + + for num in nums: + if num < mn: + mn = num + elif mx > num: + mx = num + + return [mn,mx] \ No newline at end of file