diff --git a/Find Disappeared Number.py b/Find Disappeared Number.py new file mode 100644 index 00000000..b5cf1247 --- /dev/null +++ b/Find Disappeared Number.py @@ -0,0 +1,18 @@ +# Time Complexity --> O(n) +# Space Complxity --> O(1) +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + n = len(nums) + re = [] + + for i in range(n): + index = abs(nums[i])-1 + if nums[index]>0: + nums[index] = -1*nums[index] + + for i in range(n): + if nums[i]>0: + re.append(i+1) + nums[i] = abs(nums[i]) + return re + diff --git a/GameOfLife.py b/GameOfLife.py new file mode 100644 index 00000000..22811cb2 --- /dev/null +++ b/GameOfLife.py @@ -0,0 +1,44 @@ +# Time Complexity --> O(m*n) +# Space Complexity --> O(1) +class Solution: + def gameOfLife(self, board: List[List[int]]) -> None: + """ + Do not return anything, modify board in-place instead. + """ + # 1 --> 0 then 2 + # 0 --> 1 then 3 + m = len(board) + n = len(board[0]) + + def helper(r, c): + dim = [[0,1],[1,0],[-1,0],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]] + + re = 0 + for i in dim: + row = r+i[0] + col = c+i[1] + if 0<= row 3: + board[i][j]=2 + else: + pass + + for i in range(m): + for j in range(n): + if board[i][j]==2: + board[i][j]=0 + elif board[i][j]==3: + board[i][j]=1 + + + diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..3e984d25 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,26 @@ +# Time Comkplexity --> O(n) +# Space Complexity --> O(1) +# Total Comparisons --> 1.5*n. We are comparing a pair of numbers at a time and then comparing the minimum of them with existing min value and maximum of them with existing max value +def findMinAndMax(self, nums): + n = len(nums) + i = 0 + + ## Different initializations depending on whether the length of array is even or odd + if n % 2 == 0: + min_val = min(nums[0], nums[1]) + max_val = max(nums[0], nums[1]) + i = 2 + else: + min_val = max_val = nums[0] + i = 1 + + while i < n - 1: + if nums[i] < nums[i + 1]: + min_val = min(min_val, nums[i]) + max_val = max(max_val, nums[i + 1]) + else: + min_val = min(min_val, nums[i + 1]) + max_val = max(max_val, nums[i]) + i += 2 + + return [min_val, max_val]