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
23 changes: 23 additions & 0 deletions DisappearedNumbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Time Complexity : O(n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach : Mark corresponding index of each number negative.
# If any index remains positive then that number is missing from array.

class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
for i, num in enumerate(nums):
idx = abs(num) - 1

if nums[idx] > 0:
nums[idx] *= -1

res = []

for i, num in enumerate(nums):
if num > 0:
res.append(i+1)

return res

41 changes: 41 additions & 0 deletions GameOfLife.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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 : At each cell, count number os live neighbors.
# Mark transitions with values: 2 (alive -> dead) and 3 (dead -> alive).
# in the final result, convert 2 -> 0 and 3 -> 1.


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)]
rows, cols = 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 < rows and 0 <= c < cols:
if board[r][c] == 1 or board[r][c] == 2:
count += 1
return count

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

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

36 changes: 36 additions & 0 deletions MinAndMax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Time Complexity : O(n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach : Scan the array in pairs.


class Solution:
def getMinMax(self, nums):
# code here
n = len(nums)
i = 0

if n % 2 == 0:
if nums[0] < nums[1]:
min_val = nums[0]
max_val = nums[1]
else:
min_val = nums[1]
max_val = nums[0]
i = 2
else:
min_val = max_val = nums[0]
i = 1


while i < n - 1:
if nums[i] < nums[i + 1]:
min_val = min(min_val, nums[i])
max_val = max(max_val, nums[i + 1])
else:
min_val = min(min_val, nums[i + 1])
max_val = max(max_val, nums[i])
i += 2

return [min_val, max_val]