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
25 changes: 25 additions & 0 deletions 31. Disappeared numbers in an Array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
# Iterating through every number in nums
for n in nums:
# Converting the number into its index (since numbers are 1..n, subtract 1)
i = abs(n) - 1

# Marking the number at index i as negative (if not already negative)
# This is showing that the number (i+1) exists in the array
nums[i] = -1 * abs(nums[i])

# Creating an empty list to collect missing numbers
result = []

# Iterating through nums again with index i and value n
for i, n in enumerate(nums):
# If a number is still positive, it means its index+1 was never seen
if n > 0:
result.append(i + 1) # Adding the missing number to result

# Returning the final list of missing numbers
return result

# Time Complexity: O(n) - We traverse the list a constant number of times.
# Space Complexity: O(1) - We use no extra space that scales with input size
45 changes: 45 additions & 0 deletions 32. Min and Max.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Solution:
# getMinMax is taking an array and returning a tuple (min, max)
# Iterating through the array and updating min and max accordingly
def getMinMax(self, arr: list) -> tuple:
n = len(arr)
# Handling edge case when array is empty
if n == 0:
return None, None

# Initializing min and max
if n == 1:
# If only one element, returning it as both min and max
return arr[0], arr[0]

if arr[0] > arr[1]:
min_val = arr[1]
max_val = arr[0]
else:
min_val = arr[0]
max_val = arr[1]

# Iterating from the third element to the end
for i in range(2, n):
if arr[i] > max_val:
# Updating max if current element is greater
max_val = arr[i]
elif arr[i] < min_val:
# Updating min if current element is smaller
min_val = arr[i]

# Returning min and max as a tuple
return min_val, max_val

# Example usage
if __name__ == "__main__":
arr = [1000, 11, 445, 1, 330, 3000]
# Creating an instance of Solution
sol = Solution()
# Calling getMinMax and unpacking result
min_elem, max_elem = sol.getMinMax(arr)
print("Minimum element is", min_elem)
print("Maximum element is", max_elem)

# Time Complexity (TC): O(n), where n is the number of elements in arr
# Space Complexity (SC): O(1), as only constant extra space is being used
43 changes: 43 additions & 0 deletions 33. Game of Life.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
# Getting the number of rows and columns in the board
rows, cols = len(board), len(board[0])

def countLiveNeighbors(r, c):
# Counting the number of live neighbors for cell (r, c)
live_neighbors = 0
for i in range(r - 1, r + 2):
for j in range(c - 1, c + 2):
# Skipping the cell itself and out-of-bound indices
if (i == r and j == c) or i < 0 or j < 0 or i >= rows or j >= cols:
continue
# Checking if the neighbor is currently alive (1 or -1)
if abs(board[i][j]) == 1:
live_neighbors += 1
return live_neighbors

# Iterating through each cell to determine its next state
for r in range(rows):
for c in range(cols):
live_neighbors = countLiveNeighbors(r, c)

# Marking live cells that should die as -1
if board[r][c] == 1 and (live_neighbors < 2 or live_neighbors > 3):
board[r][c] = -1

# Marking dead cells that should become alive as 2
if board[r][c] == 0 and live_neighbors == 3:
board[r][c] = 2

# Updating the board to the next state by converting temporary markers
for r in range(rows):
for c in range(cols):
# Setting cells to alive if their value is positive
if board[r][c] > 0:
board[r][c] = 1
# Setting cells to dead otherwise
else:
board[r][c] = 0

# Time Complexity (TC): O(m * n), where m and n are the number of rows and columns, respectively.
# Space Complexity (SC): O(1), since the board is being updated in-place without using extra space.