From 09a59e4da46ab73fb4116638fa01332876688549 Mon Sep 17 00:00:00 2001 From: Purva Date: Thu, 27 Nov 2025 00:53:15 -0600 Subject: [PATCH] done --- problem1.py | 25 +++++++++++++++++++++++++ problem2.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 problem1.py create mode 100644 problem2.py diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..41527851 --- /dev/null +++ b/problem1.py @@ -0,0 +1,25 @@ +""" +We check for every index (nums[i] + 1) if the element present at that index is negative, +if its negative, mark it as negative i.e. it is present, and in the second pass, return all elements that remain positive +time is o(n) and space is o(1) +""" + +class Solution(object): + def findDisappearedNumbers(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + res = [] + n = len(nums) + for i in range(n): + index = abs(nums[i]) - 1 #to traverse through the array at each index and + if nums[index] > 0: #check if number is not negative, make it negative + nums[index] *= -1 + + for i in range(n): #second pass, check if any elem is positive, return that index + 1 as missing number + if nums[i] > 0: + res.append(i+1) + return res + + \ No newline at end of file diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..7d0fef7f --- /dev/null +++ b/problem2.py @@ -0,0 +1,42 @@ +""" +We use the coundary conditions to moodify the pointers and apply the games of rule accordingly, usee the temporary state change method to analyse the board +then count the changes that were made through counting the number of variations we see (2 and 3 for dead and resurrected accordingly) +TC is O(m*n) and SC is o(n) +""" + + +class Solution(object): + def gameOfLife(self, board): + """ + :type board: List[List[int]] + :rtype: None Do not return anything, modify board in-place instead. + """ + m = len(board) + n = len(board[0]) + dirs = [(-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)] + def countAlive(i, j): + count = 0 + for dr, dc in dirs: + nr, nc = i + dr, j + dc + if nr >= 0 and nc >= 0 and nr < m and nc < n: + if board[nr][nc] == 1 or board[nr][nc] == 2: #2 for originally alive, but marked to die later on + count += 1 + return count + + for i in range(m): + for j in range(n): + count = countAlive(i,j) + if board[i][j] == 1 and (count > 3 or count < 2): + board[i][j] = 2 + if board[i][j] == 0 and count == 3: + board[i][j] = 3 + + for i in range(m): + for j in range(n): + if board[i][j] == 3: + board[i][j] = 1 + if board[i][j] == 2: + board[i][j] = 0 + + + \ No newline at end of file