From 19e10f34f88114123dd1605cffec719b91ebbdc6 Mon Sep 17 00:00:00 2001 From: Kattuvila Milkiyas Date: Sat, 6 Dec 2025 13:26:52 -0800 Subject: [PATCH] array-1 completed --- diagnol-traversal.py | 54 ++++++++++++++++++++++++++++++++++++++++++ product-except-self.py | 33 ++++++++++++++++++++++++++ spiral-matrix.py | 44 ++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 diagnol-traversal.py create mode 100644 product-except-self.py create mode 100644 spiral-matrix.py diff --git a/diagnol-traversal.py b/diagnol-traversal.py new file mode 100644 index 00000000..e373d01b --- /dev/null +++ b/diagnol-traversal.py @@ -0,0 +1,54 @@ +# Find the number of elements in mat and assign an arr too that length. Keep the flag true +# at first. Loop throught the elelmts in mat. +# If flag is true: +# If row is 0 and col is not the last col, increment the col and make flag=false, +# if col is last col, increment the row anf turn flag to false else decrement row and increment col +# If flag is false: +# If col is 0 and row is not the last row, increment the row and make flag=true, +# if row is last row, increment the col anf turn flag to true else increment row and decrement col + + + + +class Solution(object): + def findDiagonalOrder(self, mat): + """ + :type mat: List[List[int]] + :rtype: List[int] + """ + row = len(mat) + col = len(mat[0]) + n = (row)*(col) + r=0 + c=0 + arr=[None for i in range(n)] + flag = True + for i in range(n): + print(r,c) + arr[i]=mat[r][c] + if flag: + if r==0 and c!=col-1: + c+=1 + flag = False + elif c==col-1: + r+=1 + flag=False + else: + r-=1 + c+=1 + else: + if c==0 and r!=row-1: + r+=1 + flag=True + elif r==row-1: + c+=1 + flag=True + else: + r+=1 + c-=1 + return arr + + + + + \ No newline at end of file diff --git a/product-except-self.py b/product-except-self.py new file mode 100644 index 00000000..56b89a03 --- /dev/null +++ b/product-except-self.py @@ -0,0 +1,33 @@ +# take s=1, left_prod array with length of nums. Loop through nums and multiply each +# element with s and update s to the new value. Store each product value in left_prod array. +# Assign s = 1 and loop through left_prod from reverse. update left_prod[i] with +# left_prod[i]*s ans update s to s*nums[i] + +# Time complexity: O(n) +# Space complexity O(n) + +class Solution(object): + def productExceptSelf(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + s=1 + left_prod=[] + l=len(nums) + i=0 + while i=0: + left_prod[i]=left_prod[i]*s + s=s*nums[i] + i-=1 + return left_prod + + + diff --git a/spiral-matrix.py b/spiral-matrix.py new file mode 100644 index 00000000..f2eec31e --- /dev/null +++ b/spiral-matrix.py @@ -0,0 +1,44 @@ + +# Find the no. of rows and col. Assign top=0, left=0, down = r-1 and right = c-1. +# find the total number of elemtns in the matrix as n. Assign an empty array arr. while left<=rigth and top<=down +# print the elemtns from left to right and then increment top +# print elemts from top to down and decrement right +# print elemts from right to left and decrement down +# print elemetns from down to top and increment left + +# Time complexity: O(row*col) +# Space complexity: O(1) + + +class Solution(object): + def spiralOrder(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: List[int] + """ + r=len(matrix) + c=len(matrix[0]) + top = 0 + left = 0 + down = r-1 + right = c-1 + n = r*c + arr=[] + while left<=right and top<=down: + + for i in range(left,right+1): + arr.append(matrix[top][i]) + top+=1 + + for i in range(top,down+1): + arr.append(matrix[i][right]) + right-=1 + if top<=down: + for i in range(right,left-1,-1): + arr.append(matrix[down][i]) + down-=1 + if left<=right: + for i in range(down,top-1,-1): + arr.append(matrix[i][left]) + left+=1 + return arr \ No newline at end of file