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

'''
Mark seen indices by negating values.
Return indices with positive values as missing numbers.
'''
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
missing = []

# Set idx to negative if we have seen element (idx+1)
for i in range(len(nums)):
idx = abs(nums[i])-1
if nums[idx] > 0:
nums[idx] *= -1

# Check which indices are still positive = missing elements
for i in range(len(nums)):
if nums[i] > 0:
missing.append(i+1)
else:
nums[i] *= -1

return missing
44 changes: 44 additions & 0 deletions FindMaxMinWithMinComparisons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import List
import math

# Time Complexity : O(n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

'''
Problem:
Given an array of numbers of length N, find both the minimum and maximum.
Follow up : Can you do it using less than 2 * (N - 2) comparisons?
'''

'''
Iterate in pairs instead of going 1 by 1.
'''
def findMaxMin(nums: List[int]) -> (int, int):
i = 0
maxVal = float('-inf')
minVal = float('inf')

# Check if next element is available
while i+1 < len(nums):
maxVal = max(maxVal, max(nums[i], nums[i+1]))
minVal = min(minVal, min(nums[i], nums[i+1]))

i+=2

if i < len(nums):
maxVal = max(maxVal, nums[i])
minVal = min(minVal, nums[i])

return minVal, maxVal

# Test
a1 = [3,1,5,2,8,9,4,3,23]
a2 [1]
a3 = []
a4 = [3,3,3]
print(a1, findMaxMin(a1))
print(a2, findMaxMin(a2))
print(a3, findMaxMin(a3))
print(a4, findMaxMin(a4))
53 changes: 53 additions & 0 deletions GameOfLife.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Time Complexity : O(m*n) where m is number of rows and n is number of colums in the board.
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

'''
Iterate over the board, keeping flags for when element goes from live to dead OR dead to live.
Iterate again, setting it back to current state: dead 0 or alive 1.
'''
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
for i in range(len(board)):
for j in range(len(board[0])):
liveCount = self.countLiveNeighbors(board, i, j)
print(1, j, liveCount)

if board[i][j] == 1 or board[i][j] == 2:
if liveCount < 2 or liveCount > 3:
board[i][j] = 2

if board[i][j] == 0 or board[i][j] == 3:
if liveCount == 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

def countLiveNeighbors(self, board: List[List[int]], i: int, j: int) -> int:
# top, t-right, right, b-right, bottom, b-left, left, t-left
dirs = [(-1,0), (-1,1), (0,1), (1,1), (1,0), (1,-1), (0,-1), (-1,-1)]

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

live = 0

for offset in dirs:
n_i = offset[0]+i
n_j = offset[1]+j
if n_i < m and n_i >= 0 and n_j < n and n_j >= 0:
if board[n_i][n_j] == 1 or board[n_i][n_j] == 2:
live += 1

return live