diff --git a/Problem-1.java b/Problem-1.java new file mode 100644 index 00000000..29b14ba3 --- /dev/null +++ b/Problem-1.java @@ -0,0 +1,29 @@ +// Time Complexity :O(n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode : yes +// Three line explanation of solution in plain english + +// Your code here along with comments explaining your approach +// do a forward pass to multiply all the elements from start +// again do a backward pass and multiply all the elements after that +// now multiply both elements + +class Solution { + public int[] productExceptSelf(int[] nums) { + int n = nums.length; + int[] output = new int[n]; + int prod = 1; + for(int i=0; i < n; i++){ + output[i] = prod; + prod *= nums[i]; + } + prod = 1; + for(int i = n-1;i>-1;i-- ){ + output[i] *= prod; + prod *= nums[i]; + } + return output; + + + } +} \ No newline at end of file diff --git a/Problem-1.py b/Problem-1.py new file mode 100644 index 00000000..fce28adf --- /dev/null +++ b/Problem-1.py @@ -0,0 +1,22 @@ +# Time Complexity :O(n) +# Space Complexity :O(1) +# Did this code successfully run on Leetcode : yes +# Three line explanation of solution in plain english + +# Your code here along with comments explaining your approach +# do a forward pass to multiply all the elements from start +# again do a backward pass and multiply all the elements after that +# now multiply both elements + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + output = [] + prod =1 + for i in range (len(nums)): + output.append(prod) + prod *= nums[i] + prod = 1 + for i in range(len(nums)-1,-1,-1): + output[i] *= prod + prod *= nums[i] + return output \ No newline at end of file diff --git a/Problem-2.java b/Problem-2.java new file mode 100644 index 00000000..6952078a --- /dev/null +++ b/Problem-2.java @@ -0,0 +1,46 @@ +// Time Complexity : O(mn) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode :yes +// Three line explanation of solution in plain english + +// Your code here along with comments explaining your approach +// Traverse the matrix diagonally, switching direction on each diagonal. +// Move upward-right when the flag is true, and downward-left when the flag is false. +// When you hit matrix boundaries, adjust the row or column and flip the direction to continue correctly. +class Solution { + public int[] findDiagonalOrder(int[][] mat) { + int m = mat.length; + int n = mat[0].length; + boolean flag = true; + int[] output = new int[m*n]; + int r =0; + int c =0; + for( int i = 0; i < m*n; i++){ + output[i] = mat[r][c]; + if (flag){ + if (c == n-1){ + r++; + flag = false; + }else if(r == 0){ + c++; + flag = false; + }else{ + r--; + c++; + } + }else{ + if (r == m-1){ + c++; + flag = true; + }else if(c == 0){ + r++; + flag = true; + }else{ + r++; + c--; + } + } + } + return output; + } +} \ No newline at end of file diff --git a/Problem-2.py b/Problem-2.py new file mode 100644 index 00000000..8b7d13c6 --- /dev/null +++ b/Problem-2.py @@ -0,0 +1,49 @@ +# Time Complexity : O(mn) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode :yes +# Three line explanation of solution in plain english + +# Your code here along with comments explaining your approach +# Traverse the matrix diagonally, switching direction on each diagonal. +# Move upward-right when the flag is true, and downward-left when the flag is false. +# When you hit matrix boundaries, adjust the row or column and flip the direction to continue correctly. + +class Solution: + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: + m = len(mat) + n = len(mat[0]) + output =[] + flag = True #up + r = 0 + c = 0 + for i in range(m*n): + output.append(mat[r][c]) + if flag: + if c == n-1: + r += 1 + flag = False + elif r == 0: + c+= 1 + flag = False + else: + r-= 1 + c += 1 + else: + if r == m-1: + c += 1 + flag = True + elif c == 0: + r += 1 + flag = True + else: + r += 1 + c -= 1 + return output + + + + + + + + \ No newline at end of file diff --git a/Problem-3.java b/Problem-3.java new file mode 100644 index 00000000..3db59ece --- /dev/null +++ b/Problem-3.java @@ -0,0 +1,53 @@ +// Time Complexity :O(mn) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :yes +// Three line explanation of solution in plain english + +// Your code here along with comments explaining your approach +//Traverse the matrix layer by layer in a spiral order, moving top row → right column → bottom row → left column. +// After completing each side, update the boundaries (top++, bottom--, left++, right--) to move inward. +// put four pointers. Repeat until all elements are added to the output list. + +class Solution { + public List spiralOrder(int[][] matrix) { + List output = new ArrayList<>(); + if (matrix == null || matrix.length == 0) return output; + + int m = matrix.length; + int n = matrix[0].length; + int top = 0, bottom = m - 1; + int left = 0, right = n - 1; + + while (left <= right && top <= bottom) { + // Top row + for (int i = left; i <= right; i++) { + output.add(matrix[top][i]); + } + top++; + + // Right column + for (int i = top; i <= bottom; i++) { + output.add(matrix[i][right]); + } + right--; + + // Bottom row + if (top <= bottom) { + for (int i = right; i >= left; i--) { + output.add(matrix[bottom][i]); + } + bottom--; + } + + // Left column + if (left <= right) { + for (int i = bottom; i >= top; i--) { + output.add(matrix[i][left]); + } + left++; + } + } + + return output; + } +} diff --git a/Problem-3.py b/Problem-3.py new file mode 100644 index 00000000..e2158624 --- /dev/null +++ b/Problem-3.py @@ -0,0 +1,35 @@ +# Time Complexity :O(mn) +# Space Complexity :O(1) +# Did this code successfully run on Leetcode :yes +# Three line explanation of solution in plain english + +# Your code here along with comments explaining your approach +# Traverse the matrix layer by layer in a spiral order, moving top row → right column → bottom row → left column. +# After completing each side, update the boundaries (top++, bottom--, left++, right--) to move inward. +# put four pointers. Repeat until all elements are added to the output list. + +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + m = len(matrix) + n = len(matrix[0]) + top = 0 + bottom = m-1 + left = 0 + right =n-1 + output = [] + while (left <= right and top <= bottom): + for i in range(left,right+1): + output.append(matrix[top][i]) + top += 1 + for i in range(top,bottom+1): + output.append(matrix[i][right]) + right -= 1 + if top <= bottom: + for i in range(right, left-1, -1): + output.append(matrix[bottom][i]) + bottom -= 1 + if left <= right: + for i in range(bottom,top-1,-1): + output.append(matrix[i][left]) + left += 1 + return output \ No newline at end of file