From 3bafecb56f5d96cbfe0adf89176a54da7384f53d Mon Sep 17 00:00:00 2001 From: Navami Bhat <127547240+Nav0708@users.noreply.github.com> Date: Fri, 29 May 2026 18:41:18 -0700 Subject: [PATCH] Done Array 1 --- Problem-1.py | 29 +++++++++++++++++++++++++++++ Problem-2.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ Problem-3.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 Problem-1.py create mode 100644 Problem-2.py create mode 100644 Problem-3.py diff --git a/Problem-1.py b/Problem-1.py new file mode 100644 index 00000000..299ea028 --- /dev/null +++ b/Problem-1.py @@ -0,0 +1,29 @@ +# Time Complexity : O(2n) +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : Yes +# Three line explanation of solution in plain english: +"""1. Optimizing brute force approach of O(n^2) buy calculating running product from left to right and right to left +2. In this solution the time complexity O(n) can be achived by using extra array of space O(n) +3. we use one for loop to find the running product from left to right and store in the res[] array then multiply this array with running product from right to left""" + +# Your code here along with comments explaining your approach +class Solution(object): + def productExceptSelf(self, nums): + #initializing a result array with 1's + res=[1]*len(nums) + #variable to count running product at each iteration + #rp will store the running products of integers in the array so far + rp=1 + #first for loop to calculate a running product of the given array from left to right + for i in range(1,len(nums)): + rp=rp*nums[i-1] + res[i]=rp + print(res) + #now resetting the running product variable for right running product + rp=1 + #iterating from right to left to calculate running product from teh right + for j in range(len(nums)-2,-1,-1): + rp=rp*nums[j+1] + #product of current running product with the number in resultant i.e left running product + res[j]=rp*res[j] + return res \ No newline at end of file diff --git a/Problem-2.py b/Problem-2.py new file mode 100644 index 00000000..7ef0cc31 --- /dev/null +++ b/Problem-2.py @@ -0,0 +1,45 @@ +# Time Complexity : O(m * n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Three line explanation of solution in plain english: +# 1. We can use two pointers i and j to traverse the diagonals of the matrix one for row and one for column. +# 2. We can use a boolean variable to keep track of the direction of traversal (up or down). +# 3. We can append the elements to the result list based on the current direction and adjust the pointers to go in diagonal directions and until we have traversed all elements in the matrix. +# Your code here along with comments explaining your approach +class Solution(object): + def findDiagonalOrder(self, mat): + if not mat or not mat[0]: + return [] + + m, n = len(mat), len(mat[0]) + res = [] + + i = j = 0 + up = True +# we will traverse the matrix until we have traversed all the elements in the matrix + for k in range(m * n): + res.append(mat[i][j]) + # setting directions for pointers based on which direction to traverse first we start with up direction + if up: + if j == n - 1: + i += 1 + up = False + elif i == 0: + j += 1 + up = False + else: + i -= 1 + j += 1 + # if we are traversing in down direction then we will check for out of bound condition for row and column + else: + if i == m - 1: + j += 1 + up = True + elif j == 0: + i += 1 + up = True + else: + i += 1 + j -= 1 + + return res diff --git a/Problem-3.py b/Problem-3.py new file mode 100644 index 00000000..1df92f0d --- /dev/null +++ b/Problem-3.py @@ -0,0 +1,43 @@ +# Time Complexity : O(m * n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Three line explanation of solution in plain english: +# We will create four pointers up, down, left and right to keep track of the boundaries of the matrix. +# We will traverse the matrix in a spiral manner by first traversing from left to right, then from top to bottom, then from right to left and finally from bottom to top. +# We will keep updating the pointers after each traversal to shrink the boundaries of the matrix and continue the traversal until we have traversed all the elements in the matrix. + + +# Your code here along with comments explaining your approach +class Solution(object): + def spiralOrder(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: List[int] + """ + m,n=len(matrix),len(matrix[0]) + up,down=0,m-1 + left,right=0,n-1 + res=[] + #shrinking the spiral and making sure that we stay within the boundary without overlapping the cells + while up<=down and left<=right: + #collecting the matrix values from left to right + # j because we are iterating through columns + for j in range(left, right+1): + res.append(matrix[up][j]) + #since we have finished the one row updating up to indicate we are shrinking the row from up + up+=1 + for i in range(up, down+1): + res.append(matrix[i][right]) + #since we have finished the one column updating right to indicate we are shrinking the column from right + right-=1 + # if up is less than or equal to down then only we will traverse from right to left because if up is greater than down then it means we have already traversed all the rows and we should not traverse again + if up <= down: + for j in range(right, left - 1, -1): + res.append(matrix[down][j]) + down -= 1 + #now if left is less than or equal to right then only we will traverse from bottom to top because if left is greater than right then it means we have already traversed all the columns and we should not traverse again + if left <= right: + for i in range(down, up - 1, -1): + res.append(matrix[i][left]) + left += 1 + return res \ No newline at end of file