Skip to content
Open

Array-2 #1870

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Find Disappeared Number.py
Original file line number Diff line number Diff line change
@@ -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

44 changes: 44 additions & 0 deletions GameOfLife.py
Original file line number Diff line number Diff line change
@@ -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 <m and 0<= col<n:
if board[row][col]==2 or board[row][col]==1:
re = re + 1
return re

for i in range(m):
for j in range(n):
if board[i][j]==0 and helper(i,j)==3:
board[i][j]=3
elif board[i][j]==1 and helper(i,j)<2:
board[i][j]=2
elif board[i][j]==1 and helper(i,j)>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



26 changes: 26 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -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]