From 282e90cbe72dc5a5b895610b11129f6f7fe9c6e2 Mon Sep 17 00:00:00 2001 From: praxpk <23168694+praxpk@users.noreply.github.com> Date: Wed, 7 Jan 2026 23:30:34 -0500 Subject: [PATCH] Array-2 --- game_of_life.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++ missing_numbers.py | 19 +++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 game_of_life.py create mode 100644 missing_numbers.py diff --git a/game_of_life.py b/game_of_life.py new file mode 100644 index 00000000..3883ac30 --- /dev/null +++ b/game_of_life.py @@ -0,0 +1,51 @@ +""" +time - o(n) +space - o(1) +we follow the rules and change 0 to 1 and 1 to 0, however, this causes information +loss. So we represent 0 -> 1 change as 2 and 1 -> 0 change as 3 +""" + +from typing import List + + +class Solution: + def get_score(self, board, i, j): + direction = [ + (0, -1), + (-1, -1), + (-1, 0), + (-1, 1), + (0, 1), + (1, 1), + (1, 0), + (1, -1), + ] + score = 0 + for d in direction: + if 0 <= i + d[0] < len(board) and 0 <= j + d[1] < len(board[0]): + val = board[i + d[0]][j + d[1]] + if val < 2: + score += val + elif val == 3: + score += 1 + return score + + def gameOfLife(self, board: List[List[int]]) -> None: + """ + Do not return anything, modify board in-place instead. + """ + # 0 -> 1 = 2 and 1 -> 0 = 3 + for i in range(len(board)): + for j in range(len(board[0])): + score = self.get_score(board, i, j) + if (score < 2 or score > 3) and board[i][j] == 1: + board[i][j] = 3 + elif score == 3 and board[i][j] == 0: + board[i][j] = 2 + + for i in range(len(board)): + for j in range(len(board[0])): + if board[i][j] == 2: + board[i][j] = 1 + if board[i][j] == 3: + board[i][j] = 0 diff --git a/missing_numbers.py b/missing_numbers.py new file mode 100644 index 00000000..0df182a5 --- /dev/null +++ b/missing_numbers.py @@ -0,0 +1,19 @@ +""" +time - o(n) +space - o(1) +we take each number in the array and head to the array[number] (using the number as an index) +we make this number as negative, any number that is missing will have array[number] as positive +""" + +from typing import List + + +class Solution: + def findDisappearedNumbers(self, nums: List[int]) -> List[int]: + for i in range(len(nums)): + nums[abs(nums[i]) - 1] *= -1 if nums[abs(nums[i]) - 1] > 0 else 1 + result = [] + for i in range(len(nums)): + if nums[i] > 0: + result.append(i + 1) + return result