Skip to content
Open
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
63 changes: 63 additions & 0 deletions find_all_disappeared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'''
Docstring for find_all_disappeared
This is pretty straight forward soilution, and it is very optimal as well, the only caveat is, it does run with extra space, that's it.
'''

class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:

res = []
for i in range(len(nums)):

index = abs(nums[i])-1

index_num = abs(nums[index])*-1

nums[index] = index_num


for i in range(len(nums)):

if nums[i]>0:
res.append(i+1)
return res

'''
This is tricky, it involves making changes to the given array, and making it multiply with -1

At the end, you will figure out the elements which are greater than 0, and that's when you realize those are the elements you want.

You will want to put this logic on the paper for sure, for clarity.

If the input also contains negative integers, you solve it using a offset.




'''


class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:

res = []
for i in range(len(nums)):

index = abs(nums[i])-1

index_num = abs(nums[index])*-1

nums[index] = index_num


for i in range(len(nums)):

if nums[i]>0:
res.append(i+1)
return res






30 changes: 30 additions & 0 deletions game_of_life.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""

directions =[[-1,0],[1,0],[0,1],[0,-1],[-1,-1],[1,1],[-1,1],[1,-1]]

for i in range(len(board)):
for j in range(len(board[0])):
count = 0
for r,c in directions:
if(i+r in range(len(board)) and j+c in range(len(board[0]))):
if board[i+r][c+j]==2 or board[i+r][c+j]==1:
count+=1
if(count ==3 and board[i][j]==0):
board[i][j]=3

elif( (count<2 or count>3) and board[i][j]==1):
board[i][j]=2

for i in range(len(board)):
for j in range(len(board[0])):

if board[i][j]==2:
board[i][j] = 0

elif board[i][j]==3:
board[i][j]=1

49 changes: 49 additions & 0 deletions n-2_comparisons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'''

Inorder to find out the max and min of the array, we generally go over the array.
While going through the array, lets say array have n elements.
To make min and max comparison at every element, we make 2*n comparisons.

In the current approach.

We are taking two elements in one go, that is we. re reducing the number of comparisons
to n/2.

and at each step we are making 3 comparisons, which is totally equal to 3*n/2=1.5n

which means we are reducing the total comparison from 2*n to 1.5 n.

If the length is odd, we are making two extra comparison, which is 1.5n+2

Although the time complexity remains at O(n), we are reducing the comparisons to 1.5n+2

'''

## Example :[-12,4,-18,6,25]

# nums =[-12,4,-18,6,25]


nums=[-12, 4, -18, 6, 25, -204]



min_num = float('inf')
max_num = float('-inf')

for i in range(0, len(nums)-1, 2):
if nums[i]<nums[i+1]:
min_num = min(min_num, nums[i])
max_num = max(max_num, nums[i+1])
else:
min_num = min(min_num, nums[i+1])
max_num = max(max_num, nums[i])


if(len(nums)%2==1):
min_num = min(nums[-1], min_num)
max_num = max(nums[-1], max_num)

print(min_num, max_num)