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 findAll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Time Complexity : O(n)
# Space Complexity : O(1) (excluding output)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach: Use index marking.
# Iterate through the array and treat each value as an index (value - 1).
# Mark the corresponding index as negative to indicate the number exists.
# After marking, iterate again and collect indices that remain positive,
# as they represent the missing numbers.




class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
idx = -1
res = []
for i in nums:
idx = abs(i)
if nums[idx - 1] > 0:
nums[idx - 1] = nums[idx - 1] * -1

for i in range(len(nums)):
if nums[i] > 0:
res.append(i + 1)
return res
45 changes: 45 additions & 0 deletions game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Time Complexity : O(m * n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach: In-place state encoding.
# Traverse the board and count live neighbors for each cell.
# Use temporary states:
# 2 → was alive (1) → becomes dead (0)
# 3 → was dead (0) → becomes alive (1)
# This allows us to preserve original state while updating.
# In the second pass, convert 2 → 0 and 3 → 1 to finalize the board.



class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
dirs = [(-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)]
m, n = len(board), len(board[0])

def getCount(i, j):
count = 0
for dx, dy in dirs:
r, c = i + dx, j + dy
if 0 <= r < m and 0 <= c < n:
if board[r][c] == 1 or board[r][c] == 2:
count += 1
return count

for i in range(m):
for j in range(n):
cnt = getCount(i, j)
if board[i][j] == 0 and cnt == 3:
board[i][j] = 3
elif board[i][j] == 1 and (cnt < 2 or cnt > 3):
board[i][j] = 2

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