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
26 changes: 26 additions & 0 deletions Problem1_FindAllNumbersDisappearedInArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Time Complexity : O(n) where n is the length of the array
# Space Complexity : O(1) excluding the output array
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Approach: Use the array itself as a hashmap by marking indices as negative.
# For each number, mark the index (number - 1) as visited by making it negative.
# After processing, indices with positive values indicate missing numbers.

class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
# Mark indices as visited by making values negative
for num in nums:
# Get the absolute value to handle already negated numbers
index = abs(num) - 1
# Mark as visited by making it negative
if nums[index] > 0:
nums[index] = -nums[index]

# Find all indices with positive values (these are missing numbers)
result = []
for i in range(len(nums)):
if nums[i] > 0:
result.append(i + 1)

return result
47 changes: 47 additions & 0 deletions Problem2_FindMinMax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Time Complexity : O(n) where n is the length of the array
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Approach: Process elements in pairs, comparing within each pair first, then with min/max.
# This reduces comparisons to approximately 3n/2 instead of 2n by comparing pairs first.
# For each pair, compare elements to each other, then compare smaller to min and larger to max.

class Solution:
def findMinMax(self, nums: List[int]) -> tuple:
if not nums:
return None, None

n = len(nums)

# Initialize min and max
if n % 2 == 0:
# Even number of elements: compare first two
if nums[0] < nums[1]:
min_val, max_val = nums[0], nums[1]
else:
min_val, max_val = nums[1], nums[0]
start = 2
else:
# Odd number: first element is both min and max initially
min_val = max_val = nums[0]
start = 1

# Process remaining elements in pairs
for i in range(start, n, 2):
if i + 1 < n:
# Compare pair first
if nums[i] < nums[i + 1]:
# nums[i] is smaller, nums[i+1] is larger
min_val = min(min_val, nums[i])
max_val = max(max_val, nums[i + 1])
else:
# nums[i+1] is smaller, nums[i] is larger
min_val = min(min_val, nums[i + 1])
max_val = max(max_val, nums[i])
else:
# Last element if odd length
min_val = min(min_val, nums[i])
max_val = max(max_val, nums[i])

return min_val, max_val
55 changes: 55 additions & 0 deletions Problem3_GameOfLife.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Time Complexity : O(m * n) where m is rows and n is columns
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Approach: Use special values to encode state transitions: 0->1 as 2, 1->0 as 3.
# First pass: count live neighbors and mark transitions using special values.
# Second pass: convert special values to final states (2->1, 3->0).

class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
if not board or not board[0]:
return

m, n = len(board), len(board[0])

# Directions for 8 neighbors
directions = [(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)]

def countLiveNeighbors(row, col):
count = 0
for dr, dc in directions:
r, c = row + dr, col + dc
if 0 <= r < m and 0 <= c < n:
# Count original live cells (1 or 3 which means was alive)
if board[r][c] == 1 or board[r][c] == 3:
count += 1
return count

# First pass: mark transitions
# 0 -> 1: mark as 2 (dead becomes alive)
# 1 -> 0: mark as 3 (alive becomes dead)
for i in range(m):
for j in range(n):
live_neighbors = countLiveNeighbors(i, j)

if board[i][j] == 1: # Currently alive
if live_neighbors < 2 or live_neighbors > 3:
board[i][j] = 3 # Will die
else: # Currently dead
if live_neighbors == 3:
board[i][j] = 2 # Will become alive

# Second pass: convert special values to final states
for i in range(m):
for j in range(n):
if board[i][j] == 2:
board[i][j] = 1 # Dead becomes alive
elif board[i][j] == 3:
board[i][j] = 0 # Alive becomes dead