From f37802ccfa59d89b8f9184cb8cc34c7e7247d37f Mon Sep 17 00:00:00 2001 From: allurkarsneha Date: Thu, 13 Aug 2026 18:30:29 -0500 Subject: [PATCH] Completed Problem 2, leetcode 289 and 448 --- Problem1-Leetcode448.py | 21 +++++++++++++++++++++ Problem2-MinAndMaxOfArray.py | 31 +++++++++++++++++++++++++++++++ Problem3-Leetcode289.py | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 Problem1-Leetcode448.py create mode 100644 Problem2-MinAndMaxOfArray.py create mode 100644 Problem3-Leetcode289.py diff --git a/Problem1-Leetcode448.py b/Problem1-Leetcode448.py new file mode 100644 index 00000000..e747ebba --- /dev/null +++ b/Problem1-Leetcode448.py @@ -0,0 +1,21 @@ +#Time Complexity: O(n) +#Space Complexity: O(1) + +class Solution(object): + def findDisappearedNumbers(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + result = [] + for i in range(len(nums)): + index = abs(nums[i]) - 1 + if nums[index] > 0: + nums[index] *= -1 + + for i in range(len(nums)): + if nums[i] > 0: + result.append(i + 1) + + return result + \ No newline at end of file diff --git a/Problem2-MinAndMaxOfArray.py b/Problem2-MinAndMaxOfArray.py new file mode 100644 index 00000000..d187d6f0 --- /dev/null +++ b/Problem2-MinAndMaxOfArray.py @@ -0,0 +1,31 @@ +#Time Complexity: O(n) +#Space Complexity: O(1) + +class Solution: + + def findMinAndMax(self, nums): + n = len(nums) + i = 0 + + if n % 2 == 0: + if nums[0] < nums[1]: + minimum = nums[0] + maximum = nums[1] + else: + minimum = nums[1] + maximum = nums[0] + i = 2 + else: + minimum = maximum = nums[0] + i = 1 + + while i < n - 1: + if nums[i] < nums[i + 1]: + minimum = min(minimum, nums[i]) + maximum = max(maximum, nums[i + 1]) + else: + minimum = min(minimum, nums[i + 1]) + maximum = max(maximum, nums[i]) + i += 2 + + return [minimum, maximum] \ No newline at end of file diff --git a/Problem3-Leetcode289.py b/Problem3-Leetcode289.py new file mode 100644 index 00000000..fee71941 --- /dev/null +++ b/Problem3-Leetcode289.py @@ -0,0 +1,36 @@ +#Time Complexity: O(m*n) +#Space Complexity: O(1) + +class Solution(object): + def gameOfLife(self, board): + """ + :type board: List[List[int]] + :rtype: 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 + \ No newline at end of file