diff --git a/Problem1_ProductOfArrayExceptSelf.py b/Problem1_ProductOfArrayExceptSelf.py new file mode 100644 index 00000000..12bbe366 --- /dev/null +++ b/Problem1_ProductOfArrayExceptSelf.py @@ -0,0 +1,26 @@ +# Time Complexity : O(n) where n is the length of the array +# Space Complexity : O(1) excluding the output array +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Approach: First pass calculates left products (product of all elements to the left of each index). +# Second pass calculates right products and multiplies with left products to get final result. +# This avoids division and achieves O(n) time with O(1) extra space (excluding output array). + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + n = len(nums) + result = [1] * n + + # First pass: Calculate left products + for i in range(1, n): + result[i] = result[i - 1] * nums[i - 1] + + # Second pass: Calculate right products and multiply with left products + right_product = 1 + for i in range(n - 1, -1, -1): + result[i] *= right_product + right_product *= nums[i] + + return result + diff --git a/Problem2_DiagonalTraverse.py b/Problem2_DiagonalTraverse.py new file mode 100644 index 00000000..246b40ac --- /dev/null +++ b/Problem2_DiagonalTraverse.py @@ -0,0 +1,44 @@ +# Time Complexity : O(m * n) where m is rows and n is columns +# Space Complexity : O(1) excluding the output array +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Approach: Traverse diagonals by tracking direction (up-right or down-left) and adjusting row/column indices. +# When hitting boundaries, change direction and adjust indices to move to the next diagonal. +# Continue until all elements are processed. + +class Solution: + def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]: + if not matrix or not matrix[0]: + return [] + + m, n = len(matrix), len(matrix[0]) + result = [] + row, col = 0, 0 + direction = 1 # 1 for up-right, -1 for down-left + + for _ in range(m * n): + result.append(matrix[row][col]) + + # Move in current direction + row -= direction + col += direction + + # Handle boundary cases + if row >= m: + row = m - 1 + col += 2 + direction = -direction + elif col >= n: + col = n - 1 + row += 2 + direction = -direction + elif row < 0: + row = 0 + direction = -direction + elif col < 0: + col = 0 + direction = -direction + + return result + diff --git a/Problem3_SpiralMatrix.py b/Problem3_SpiralMatrix.py new file mode 100644 index 00000000..051e4390 --- /dev/null +++ b/Problem3_SpiralMatrix.py @@ -0,0 +1,43 @@ +# Time Complexity : O(m * n) where m is rows and n is columns +# Space Complexity : O(1) excluding the output array +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Approach: Use four boundaries (top, bottom, left, right) to track the spiral traversal. +# Traverse right, down, left, up in sequence, updating boundaries after each direction. +# Continue until all elements are processed. + +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + if not matrix or not matrix[0]: + return [] + + m, n = len(matrix), len(matrix[0]) + result = [] + top, bottom, left, right = 0, m - 1, 0, n - 1 + + while top <= bottom and left <= right: + # Traverse right + for j in range(left, right + 1): + result.append(matrix[top][j]) + top += 1 + + # Traverse down + for i in range(top, bottom + 1): + result.append(matrix[i][right]) + right -= 1 + + # Traverse left (if still valid) + if top <= bottom: + for j in range(right, left - 1, -1): + result.append(matrix[bottom][j]) + bottom -= 1 + + # Traverse up (if still valid) + if left <= right: + for i in range(bottom, top - 1, -1): + result.append(matrix[i][left]) + left += 1 + + return result +