diff --git a/28. Product of Array Except Self.py b/28. Product of Array Except Self.py new file mode 100644 index 00000000..24570b3a --- /dev/null +++ b/28. Product of Array Except Self.py @@ -0,0 +1,26 @@ +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + # Initializing multipliers for left and right products as 1 + left_multiplier = 1 + right_multiplier = 1 + n = len(nums) + # Creating arrays to store left and right products + left_arr = [0] * n + right_arr = [0] * n + + # Iterating through the array to fill left_arr and right_arr + for i in range(n): + j = -i -1 + # Storing product of all elements to the left of index i + left_arr[i] = left_multiplier + # Storing product of all elements to the right of index j + right_arr[j] = right_multiplier + # Updating left and right multipliers + left_multiplier *= nums[i] + right_multiplier *= nums[j] + + # Combining left and right products for the final result + return [l*r for l, r in zip(left_arr, right_arr)] + +# Time Complexity (TC): O(n), since iterating through the array twice. +# Space Complexity (SC): O(n), due to the use of left_arr and right_arr. \ No newline at end of file diff --git a/29. Diagonal Traverse.py b/29. Diagonal Traverse.py new file mode 100644 index 00000000..01b68ead --- /dev/null +++ b/29. Diagonal Traverse.py @@ -0,0 +1,50 @@ +class Solution: + def findDiagonalOrder(self, mat): + # Getting the dimensions of the matrix + m, n = len(mat), len(mat[0]) + # Initializing the result array to store the traversal + result = [0] * (m * n) + # Setting the starting position + row = col = 0 + # Using a flag to keep track of the current direction (True for up-right, False for down-left) + direction = True + + # Iterating through each element in the matrix + for i in range(m * n): + # Adding the current element to the result + result[i] = mat[row][col] + + # Checking if moving in up-right direction + if direction: + # If at the last column, moving down and changing direction + if col == n - 1: + row += 1 + direction = False + # If at the first row, moving right and changing direction + elif row == 0: + col += 1 + direction = False + # Otherwise, moving up and right + else: + row -= 1 + col += 1 + # Checking if moving in down-left direction + else: + # If at the last row, moving right and changing direction + if row == m - 1: + col += 1 + direction = True + # If at the first column, moving down and changing direction + elif col == 0: + row += 1 + direction = True + # Otherwise, moving down and left + else: + row += 1 + col -= 1 + + # Returning the diagonal traversal result + return result + +# Time Complexity (TC): O(m * n), since visiting each element once +# Space Complexity (SC): O(m * n), for storing the result array \ No newline at end of file diff --git a/30. Spiral Matrix.py b/30. Spiral Matrix.py new file mode 100644 index 00000000..6ee31f64 --- /dev/null +++ b/30. Spiral Matrix.py @@ -0,0 +1,44 @@ +class Solution: + def spiralOrder(self, matrix): + # Getting the number of rows (m) and columns (n) + m = len(matrix) + n = len(matrix[0]) + + # Initializing the boundaries for traversal + top, bottom, left, right = 0, m - 1, 0, n - 1 + + # Creating a list to store the spiral order + result = [] + + # Iterating while the boundaries are valid + while top <= bottom and left <= right: + + # Traversing from left to right along the top row + for i in range(left, right + 1): + result.append(matrix[top][i]) + top += 1 # Moving the top boundary down + + # Traversing from top to bottom along the right column + for i in range(top, bottom + 1): + result.append(matrix[i][right]) + right -= 1 # Moving the right boundary left + + # Checking if there are rows remaining + if top <= bottom: + # Traversing from right to left along the bottom row + for i in range(right, left - 1, -1): + result.append(matrix[bottom][i]) + bottom -= 1 # Moving the bottom boundary up + + # Checking if there are columns remaining + if left <= right: + # Traversing from bottom to top along the left column + for i in range(bottom, top - 1, -1): + result.append(matrix[i][left]) + left += 1 # Moving the left boundary right + + # Returning the spiral order traversal + return result + +# Time Complexity (TC): O(m * n), where m is the number of rows and n is the number of columns. +# Space Complexity (SC): O(m * n), it counting the result. If not counting the result, then O(1).