From ec01f13273487acaecf99b85b8646f4020832c92 Mon Sep 17 00:00:00 2001 From: Paneri Patel Date: Wed, 20 Aug 2025 20:07:44 -0400 Subject: [PATCH] Done Array-1 --- 1.py | 35 +++++++++++++++++++++++++++ 2.py | 58 +++++++++++++++++++++++++++++++++++++++++++++ 3.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Sample.java | 6 ----- 4 files changed, 161 insertions(+), 6 deletions(-) create mode 100644 1.py create mode 100644 2.py create mode 100644 3.py delete mode 100644 Sample.java diff --git a/1.py b/1.py new file mode 100644 index 00000000..7a251b0d --- /dev/null +++ b/1.py @@ -0,0 +1,35 @@ +''' +Array-1 + +Problem 1 + +Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. + +Example: + +Input: [1,2,3,4] Output: [24,12,8,6] Note: Please solve it without division and in O(n). + +Follow up: Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.) +''' + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: # type: ignore + if nums == None or len(nums) == 0: + return [] + n = len(nums) + rp = 1 + result = [1 for i in range(n)] + #result[0] = 1 + #left prod + for i in range(1, n): + rp = rp * nums[i-1] + result[i] = rp + + #right prod, multiply right prod with value in arr + rp = 1 + + for i in range(n-2, -1, -1): + rp = rp * nums[i+1] + result[i] = result[i] * rp + + return result \ No newline at end of file diff --git a/2.py b/2.py new file mode 100644 index 00000000..e77c8928 --- /dev/null +++ b/2.py @@ -0,0 +1,58 @@ +''' +Problem 2 + +Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image. + +Example: + +Input: + +[ + +[ 1, 2, 3 ], + +[ 4, 5, 6 ], + +[ 7, 8, 9 ] + +] + +Output: [1,2,4,7,5,3,6,8,9] +''' + +class Solution: + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: # type: ignore + if mat == None or len(mat) == 0: + return [] + m = len(mat) + n = len(mat[0]) + result = [0 for i in range(m*n)] + index = 0 #on result arr + r = 0 + c = 0 + flag = 1 + while index < m*n: + result[index] = mat[r][c] + index = index+1 + if flag == 1: + if c == n-1: + r = r+1 + flag = 0 + elif r == 0: + c = c+1 + flag = 0 + else: + r = r-1 + c = c+1 + else: + if r == m-1: + c = c+1 + flag = 1 + elif c == 0: + r = r+1 + flag = 1 + else: + r = r+1 + c = c-1 + return result + diff --git a/3.py b/3.py new file mode 100644 index 00000000..9fa7d590 --- /dev/null +++ b/3.py @@ -0,0 +1,68 @@ +''' +Problem 3 + +Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. + +Example 1: + +Input: + +[ + +[ 1, 2, 3 ], + +[ 4, 5, 6 ], + +[ 7, 8, 9 ] + +] Output: [1,2,3,6,9,8,7,4,5] Example 2: + +Input: + +[ + +[1, 2, 3, 4], + +[5, 6, 7, 8], + +[9,10,11,12] + +] Output: [1,2,3,4,8,12,11,10,9,5,6,7] +''' + +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: # type: ignore + m = len(matrix) + n = len(matrix[0]) + + result = [] + top = 0 + bottom = m-1 + left = 0 + right = n-1 + + while top <= bottom and left <= right: + #top row + if top <= bottom and left <= right: + for j in range(left, right+1): + result.append(matrix[top][j]) + top = top+1 + + #right col + if top <= bottom and left <= right: + for i in range(top, bottom+1): + result.append(matrix[i][right]) + right = right -1 + + #bottom row + if top <= bottom and left <= right: + for j in range(right, left-1, -1): + result.append(matrix[bottom][j]) + bottom = bottom -1 + + #left col + if top <= bottom and left <= right: + for i in range(bottom, top-1, -1): + result.append(matrix[i][left]) + left = left + 1 + return result diff --git a/Sample.java b/Sample.java deleted file mode 100644 index f10cd8d1..00000000 --- a/Sample.java +++ /dev/null @@ -1,6 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Three line explanation of solution in plain english - -// Your code here along with comments explaining your approach \ No newline at end of file