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
13 changes: 13 additions & 0 deletions find-all-numbers-disappeared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
res = []
for i in range(len(nums)):
index = abs(nums[i])
if nums[index-1] > 0:
nums[index-1] = -1 * nums[index-1]
for i in range(len(nums)):
if nums[i] > 0:
res.append(i+1)
else:
nums[i] = -1 * nums[i]
return res
24 changes: 24 additions & 0 deletions find_max_min.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Time Complexity: O(n)
# Space Complexity: O(1)
class Solution:
def findMaxMin(self, nums: List[int]) -> List[int]:
if not nums:
return []
max_num = nums[0]
min_num = nums[0]
# Consider a window of size 2
# Compare elements in pairs to reduce the number of comparisons
# This approach makes about 3n/2 comparisons in the worst case, instead of 2n comparisons
# Iterate through the list in steps of 2, comparing pairs, and updating max and min accordingly
for i in range(0, len(nums) - 1, 2):
if nums[i] > nums[i + 1]:
max_num = max(max_num, nums[i])
min_num = min(min_num, nums[i + 1])
else:
max_num = max(max_num, nums[i + 1])
min_num = min(min_num, nums[i])
# If there's an odd element out, compare it separately
if len(nums) % 2 != 0:
max_num = max(max_num, nums[-1])
min_num = min(min_num, nums[-1])
return [max_num, min_num]
39 changes: 39 additions & 0 deletions game-of-life.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Time Complexity: O(m*n)
# Space Complexity: O(1)
# This problem can be solved in place by using the following encoding:
# 0 -> 0 : dead cell remains dead
# 1 -> 1 : live cell remains live
# 1 -> 0 : live cell becomes dead (encoded as 2)
# 0 -> 1 : dead cell becomes live (encoded as 3)
# Encoding the state changes in this way allows us to keep track of both the current and next states of each cell without using additional space.
# We iterate through each cell in the board, counting the number of live neighbors.
# Based on the rules of the Game of Life, we update the cell's state using our encoding. This ensures that we do not interfere with the original state of the cells while determining the next state.
# After processing all cells, we can update the board by converting 2 to 0 and 3 to 1.
# This approach ensures that we do not interfere with the original state of the cells while determining the next state.
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
dir = [[0,1],[1,0],[0,-1],[-1,0],[1,1],[-1,-1],[1,-1],[-1,1]]
def countLiveCell(i,j):
live = 0
for r,c in dir:
if 0 <= i+r < len(board) and 0 <= j+c < len(board[0]):
if board[i+r][j+c] == 1 or board[i+r][j+c] == 2:
live += 1
return live

for i in range(len(board)):
for j in range(len(board[0])):
livecell = countLiveCell(i,j)
if board[i][j] == 1:
if livecell < 2: board[i][j] = 2
if livecell > 3: board[i][j] = 2
if board[i][j] == 0:
if livecell == 3: board[i][j] = 3

for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == 2: board[i][j] = 0
if board[i][j] == 3: board[i][j] = 1